Advertisement
Guest User

Untitled

a guest
Aug 8th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. const { readOPF } = require('open-packaging-format')
  2. const fs = require('graceful-fs');
  3. const opfs = './opf/';
  4. const slug = require('slugg');
  5. const moment = require('moment');
  6. const toMarkdown = require('to-markdown');
  7. const htmlToText = require('html-to-text');
  8. const uuidv4 = require('uuid/v4');
  9.  
  10. const mysql = require('mysql');
  11.  
  12. var con = mysql.createConnection({
  13. host: "****",
  14. user: "*****",
  15. password: "*****",
  16. database: "*****"
  17. });
  18.  
  19. con.connect(function(err) {
  20. if (err) throw err
  21. console.log('success');
  22. });
  23.  
  24. function urlify(str) {
  25. return str.replace(/\s+/g, '-').toLowerCase();
  26. }
  27.  
  28.  
  29.  
  30.  
  31. fs.readdir(opfs, (erz, files) => {
  32. for (var i = 0, len = files.length; i < len; i++) {
  33. file = files[i]
  34. fileid = file.slice(0, -4)
  35. readOPF('./opf/'+file)
  36. .then((opf) => {
  37. author = opf.authors[0].fileAs.replace(/\,$/, '');
  38.  
  39.  
  40. var dir = './books/' + slug(author);
  41.  
  42. if (!fs.existsSync(dir)){
  43. fs.mkdirSync(dir);
  44. }
  45.  
  46. if( opf.description ) {
  47. var shortdescription = htmlToText.fromString(opf.description, {
  48. wordwrap: false,
  49. ignoreHref: true,
  50. ignoreImage: true,
  51. singleNewLineParagraphs: true
  52. }).replace(/"/g, '&quot;');
  53. var mddescription = toMarkdown(opf.description).replace(/"/g, '&quot;');
  54. }
  55.  
  56.  
  57. var uuid = uuidv4();
  58. var written = 0;
  59.  
  60.  
  61. 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) {
  62. if (err) throw err;
  63. console.log(mysqlist);
  64. console.log('file written');
  65.  
  66. var remoteids = '';
  67. for (const id of opf.identifiers) {
  68. remoteids += JSON.stringify(id).replace(/[{}]/g, "").replace(/^"(.*?)"/, '$1').replace('uuid','book_uuid').toLowerCase() + "\n";
  69. }
  70. var remoteids = remoteids.replace(/^\s+|\s+$/g, '');
  71. if(mysqlist[0].seriesindex) {
  72. 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";
  73. } else {
  74. 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";
  75. }
  76. fs.writeFileSync(dir+ "/" + slug(opf.title) + '-' + fileid + '.md', header + mddescription);
  77.  
  78. written = 1;
  79. });
  80.  
  81. if(written == 1) {console.log('yay')}
  82.  
  83. })
  84. .catch(console.error);
  85. };
  86. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement