Advertisement
menixator

MangaHere - Page collector.js

Jul 23rd, 2014
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. if (typeof jQuery === "undefined" || parseFloat(jQuery.fn.jquery,10) < 1.8){
  2.     throw new Error("jQuery v1.8+ must be loaded in the page for the script to work.")
  3. }
  4. var once = function(fn, context){
  5.   var i=0;
  6.   return function(){
  7.     if (i === 0){
  8.         i++;
  9.         return fn.apply(context, arguments);
  10.     }
  11.     throw new Error("Callback has been already called!");
  12.   };
  13. };
  14. var slice = [].slice;
  15. var forever = function(fn, callback, args, context) {
  16.   if (typeof args === "undefined" || args === null){
  17.     args = [];
  18.   }
  19.  
  20.   if (typeof args !== "object" || args.constructor !== Array){
  21.     args = [args];
  22.   }
  23.  
  24.   var i=0;
  25.   var next = function(error){
  26.     i++;
  27.     if (error !== null || error !== undefined && error){
  28.       return callback.apply(context, arguments)
  29.     }
  30.     var others = slice.call(arguments,1); // args to next function.
  31.     return fn.apply(context, [once(next), i].concat(others))
  32.   };
  33.  
  34.   return fn.apply(context, [once(next), i].concat(args))
  35. };
  36. var CHAPTERURL_REGEX = /(http:\/\/|http:\/\/www\.)(mangahere.co\/manga\/[^\/]+\/[^\/]+\/[^\/]+)(?:\/\d+\.html|\/)?$/;
  37.  
  38. var MangaHereURLParser = (function() {
  39.  
  40.   MangaHereURLParser.name = 'MangaHereURLParser';
  41.  
  42.   function MangaHereURLParser(baseURL) {
  43.     this.template = null;
  44.     this.template = this.getTemplate(baseURL);
  45.     this.startPage = this.parse(baseURL);
  46.   }
  47.  
  48.   MangaHereURLParser.prototype.parse = function(str) {
  49.     var match;
  50.     if (!CHAPTERURL_REGEX.test(str)) {
  51.       throw new Error("ChapterURL doesn't match the format.");
  52.     }
  53.     match = CHAPTERURL_REGEX.exec(str);
  54.     if (match[3] === void 0) match[3] = "1";
  55.     return {
  56.       href: "http://www." + match[2] + "/" + match[3] + ".html",
  57.       index: parseInt(match[3], 10) - 1
  58.     };
  59.   };
  60.  
  61.   MangaHereURLParser.prototype.getTemplate = function(str) {
  62.     var match;
  63.     if (!CHAPTERURL_REGEX.test(str)) {
  64.       throw new Error("ChapterURL doesn't match the format.");
  65.     }
  66.     match = CHAPTERURL_REGEX.exec(str);
  67.     return "http://www." + match[2] + "/#{@}.html";
  68.   };
  69.  
  70.   MangaHereURLParser.prototype.getPage = function(n) {
  71.     return {
  72.       index: n,
  73.       href: this.template.replace(/#\{@\}/g, n + 1)
  74.     };
  75.   };
  76.  
  77.   return MangaHereURLParser;
  78.  
  79. })();
  80.  
  81. var urlParser = new MangaHereURLParser("http://www.mangahere.co/manga/ge_good_ending/v13/c136/"); // change url here.
  82.  
  83. var chapter = {name: null, length: null};
  84. var pages = [];
  85. forever(function(next, i, page) {
  86.     $.get(page.href, function(body, status, xhr){
  87.         var jQ = $.parseHTML(body, true);
  88.         if (i===0){
  89.             chapter.length = $(jQ).find("select[onchange='change_page(this)']").eq(0).children("option").length;
  90.             console.log("Chapter length added.");
  91.             chapter.name = $(jQ).find("#top_chapter_list>option:selected").text();
  92.             console.log("Chapter name added.");
  93.         }
  94.         pages.push($(jQ).find("img#image").attr("src"));
  95.         console.log("Page - " +( page.index + 1)+ " added.");
  96.         if (page.index >= (chapter.length-1)){
  97.             var error = new Error("End");
  98.             error.code = 214;
  99.             return next(error);
  100.         }
  101.         pages.push($(jQ).find("script+img").attr("src"));
  102.         console.log("Page - " +( page.index + 2)+ " added.");
  103.         if (page.index >= (chapter.length-2)){
  104.             var error = new Error("End");
  105.             error.code = 214;
  106.             return next(error);
  107.         }
  108.         return next(null, urlParser.getPage(page.index+2));
  109.     });
  110. }, function(error){
  111.     if (error.code === 214){
  112.         return console.log("Finished gathering pages.");
  113.     }
  114.     return console.error(error);
  115. } , urlParser.startPage)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement