Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const { readOPF } = require('open-packaging-format')
- const fs = require('graceful-fs');
- const opfs = './opf/';
- const slug = require('slugg');
- const moment = require('moment');
- const toMarkdown = require('to-markdown');
- const htmlToText = require('html-to-text');
- const uuidv4 = require('uuid/v4');
- const mysql = require('mysql');
- var con = mysql.createConnection({
- host: "****",
- user: "*****",
- password: "*****",
- database: "*****"
- });
- con.connect(function(err) {
- if (err) throw err
- console.log('success');
- });
- function urlify(str) {
- return str.replace(/\s+/g, '-').toLowerCase();
- }
- fs.readdir(opfs, (erz, files) => {
- for (var i = 0, len = files.length; i < len; i++) {
- file = files[i]
- fileid = file.slice(0, -4)
- readOPF('./opf/'+file)
- .then((opf) => {
- author = opf.authors[0].fileAs.replace(/\,$/, '');
- var dir = './books/' + slug(author);
- if (!fs.existsSync(dir)){
- fs.mkdirSync(dir);
- }
- if( opf.description ) {
- var shortdescription = htmlToText.fromString(opf.description, {
- wordwrap: false,
- ignoreHref: true,
- ignoreImage: true,
- singleNewLineParagraphs: true
- }).replace(/"/g, '"');
- var mddescription = toMarkdown(opf.description).replace(/"/g, '"');
- }
- var uuid = uuidv4();
- var written = 0;
- con.query("SELECT b.id as id, b.created_at as uploaded, b.series_nbr as seriesindex, b.maturity as maturity, readability, sentences, wordcount, s.name as seriesname FROM books b LEFT JOIN bookstats bs ON b.id = bs.book_id LEFT JOIN series s ON b.series_id = s.id WHERE b.id = "+ fileid +" LIMIT 1", function (err, mysqlist, fields) {
- if (err) throw err;
- console.log(mysqlist);
- console.log('file written');
- var remoteids = '';
- for (const id of opf.identifiers) {
- remoteids += JSON.stringify(id).replace(/[{}]/g, "").replace(/^"(.*?)"/, '$1').replace('uuid','book_uuid').toLowerCase() + "\n";
- }
- var remoteids = remoteids.replace(/^\s+|\s+$/g, '');
- if(mysqlist[0].seriesindex) {
- var header = "---\ntitle: \""+ opf.title +"\"\nauthor: \""+ author +"\"\nuuid: \""+ uuid +"\"\ndescription: \""+ shortdescription +"\"\ndate: \""+ mysqlist[0].uploaded +"\"\npublished: \""+ moment(Date.parse('opf.date')).format("Y-m-d\TH:i:sP") +"\"\nlanguage: \""+ opf.language + "\"\nwords: \""+ mysqlist[0].wordcount +"\"\nmaturity: \""+ mysqlist[0].maturity +"\"\nseries: \""+ mysqlist[0].seriesname +"\"\nseriesid: "+ mysqlist[0].seriesindex +"\noldid: "+ fileid +"\nfeatured: false\ntags: "+opf.subjects+"\n"+remoteids+"---\n\n";
- } else {
- var header = "---\ntitle: \""+ opf.title +"\"\nauthor: \""+ author +"\"\nuuid: \""+ uuid +"\"\ndescription: \""+ shortdescription +"\"\ndate: \""+ mysqlist[0].uploaded +"\"\npublished: \""+ moment(Date.parse('opf.date')).format("Y-m-d\TH:i:sP") +"\"\nlanguage: \""+ opf.language + "\"\nwords: \""+ mysqlist[0].wordcount +"\"\nmaturity: \""+ mysqlist[0].maturity +"\"\noldid: "+ fileid +"\nfeatured: false\n"+opf.subjects+"\n"+remoteids+"---\n\n";
- }
- fs.writeFileSync(dir+ "/" + slug(opf.title) + '-' + fileid + '.md', header + mddescription);
- written = 1;
- });
- if(written == 1) {console.log('yay')}
- })
- .catch(console.error);
- };
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement