Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. var args = process.argv.splice(process.execArgv.length + 2);
  2. if(args.length != 1) {
  3. console.log('invalid argument length');
  4. process.exit(1);
  5. }
  6.  
  7. var request = require('request'),
  8. cheerio = require('cheerio'),
  9. events = require('events'),
  10. path = require('path'),
  11. fs = require('fs');
  12.  
  13. function ensureDirectoryExistence(filePath) {
  14. var dirname = path.dirname(filePath);
  15. if (directoryExists(dirname)) {
  16. return true;
  17. }
  18. ensureDirectoryExistence(dirname);
  19. fs.mkdirSync(dirname);
  20. }
  21.  
  22. function directoryExists(path) {
  23. try {
  24. return fs.statSync(path).isDirectory();
  25. }
  26. catch (err) {
  27. return false;
  28. }
  29. }
  30.  
  31. var KMS = function(seriesURL) {
  32. this.seriesURL = seriesURL;
  33. }
  34.  
  35. KMS.prototype = new events.EventEmitter;
  36.  
  37. KMS.prototype.getPage = function(pURL) {
  38. var self = this;
  39. var cNum = pURL.substring(pURL.lastIndexOf('c'), pURL.lastIndexOf('/'));
  40. var pNum = '1';
  41. if(pURL.slice(pURL.lastIndexOf('/')).length != 1) pNum = pURL.substring(pURL.lastIndexOf('/')+1, pURL.lastIndexOf('.'));
  42. request(pURL, function(error, response, body) {
  43. if(!error && response.statusCode == 200) {
  44. var $ = cheerio.load(body);
  45. var imgUrl = $('#image').attr('src');
  46. var filePath = cNum + '/p' + pNum + '.jpg';
  47. ensureDirectoryExistence(filePath);
  48. console.log('Getting Page: ' + filePath.slice(0, -4));
  49. request(imgUrl).pipe(fs.createWriteStream(filePath));
  50.  
  51. if($('#image').parent().attr('href') != 'javascript:void(0);') self.emit('new page', $('#image').parent().attr('href'));
  52. } else {
  53. console.log('Eror: ' + error);
  54. }
  55. });
  56. };
  57.  
  58. KMS.prototype.run = function() {
  59. var self = this;
  60. request(self.seriesURL, function(error, response, body) {
  61. if(!error && response.statusCode==200) {
  62. var $ = cheerio.load(body);
  63.  
  64. $('.detail_list > ul:nth-child(3) > li').each(function(i, elm) {
  65. self.emit('new page', ($(this).children('span:first-child').children('a').attr('href')));
  66. });
  67. } else {
  68. console.log('Error: ' + error);
  69. }
  70. });
  71. }
  72.  
  73. var kms = new KMS(args[0]);
  74.  
  75. kms.on('new page', kms.getPage);
  76. kms.on('err page', kms.getPage);
  77.  
  78. kms.run();
  79.  
  80. node mhs.js http://www.mangahere.co/manga/gantz/
  81.  
  82. Error: undefined is not a valid uri or options object.
  83. at request (D:DocumentsDev WorkspacesJavaScriptMangaScrapernode_modulesrequestindex.js:46:11)
  84. at Request._callback (D:DocumentsDev WorkspacesJavaScriptMangaScrapermhs.js:49:5)
  85. at Request.self.callback (D:DocumentsDev WorkspacesJavaScriptMangaScrapernode_modulesrequestrequest.js:199:22)
  86. at emitTwo (events.js:87:13)
  87. at Request.emit (events.js:172:7)
  88. at Request.<anonymous> (D:DocumentsDev WorkspacesJavaScriptMangaScrapernode_modulesrequestrequest.js:1036:10)
  89. at emitOne (events.js:82:20)
  90. at Request.emit (events.js:169:7)
  91. at IncomingMessage.<anonymous> (D:DocumentsDev WorkspacesJavaScriptMangaScrapernode_modulesrequestrequest.js:963:12)
  92. at emitNone (events.js:72:20)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement