1 |
|
2 | var Core, FindType, OnParser, Parser, extname, isAbsolute, join, normalize, ref, replacer, tmpdir,
|
3 | extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
|
4 | hasProp = {}.hasOwnProperty;
|
5 |
|
6 | Core = require('./core');
|
7 |
|
8 | ref = require('path'), extname = ref.extname, join = ref.join, isAbsolute = ref.isAbsolute, normalize = ref.normalize;
|
9 |
|
10 | FindType = require('./extractor').FindType;
|
11 |
|
12 | tmpdir = require('os').tmpdir;
|
13 |
|
14 | replacer = require('./replacer');
|
15 |
|
16 | OnParser = (function() {
|
17 | function OnParser() {}
|
18 |
|
19 | OnParser.prototype.onDownload = function(fn) {
|
20 | this._onDownload = fn;
|
21 | return this;
|
22 | };
|
23 |
|
24 | OnParser.prototype.onExtract = function(fn) {
|
25 | this._onExtract = fn;
|
26 | return this;
|
27 | };
|
28 |
|
29 | OnParser.prototype.parse = function(obj) {
|
30 | if (typeof obj !== 'object') {
|
31 | throw new Error('You need to provide an object or an array');
|
32 | }
|
33 | this._obj = obj;
|
34 | this._parse();
|
35 | return this;
|
36 | };
|
37 |
|
38 | OnParser.prototype._parse = function() {
|
39 | return this._iterate(this._obj instanceof Array ? this._obj : [this._obj]);
|
40 | };
|
41 |
|
42 | OnParser.prototype._iterate = function(obj) {
|
43 | return obj.forEach((function(_this) {
|
44 | return function(i) {
|
45 | return _this._iterateItem(i);
|
46 | };
|
47 | })(this));
|
48 | };
|
49 |
|
50 | OnParser.prototype._iterateItem = function(item) {
|
51 | var attr, containerPromise, directory, execute, extract, filename, filenameMode, find, findIndex, findType, ref1, ref2, ref3, ref4, ref5, ref6, ref7, ref8, ref9, url, useText;
|
52 | url = item.url;
|
53 | find = item.find;
|
54 | findIndex = (ref1 = item.findIndex) != null ? ref1 : 0;
|
55 | findType = FindType[(ref2 = item.findType) != null ? ref2 : 'cheerio'];
|
56 | attr = (ref3 = item.attr) != null ? ref3 : 'href';
|
57 | useText = (ref4 = item.text) != null ? ref4 : false;
|
58 | filename = (ref5 = item.filename) != null ? ref5 : false;
|
59 | filenameMode = (ref6 = item.filenameMode) != null ? ref6 : ['urlBasename', 'contentType'];
|
60 | directory = (ref7 = item.directory) != null ? ref7 : '';
|
61 | execute = (ref8 = item.execute) != null ? ref8 : false;
|
62 | extract = (ref9 = item.extract) != null ? ref9 : false;
|
63 | containerPromise = Core.page(url).find(find);
|
64 | return containerPromise.then((function(_this) {
|
65 | return function(containerInstance) {
|
66 | var attrInstance, downloadOptions, fileInstance;
|
67 | if (findIndex !== 0) {
|
68 | containerInstance = containerInstance.index(findIndex);
|
69 | }
|
70 | if (useText === true) {
|
71 | attrInstance = containerInstance.text();
|
72 | } else {
|
73 | attrInstance = containerInstance.attr(attr);
|
74 | }
|
75 | downloadOptions = {};
|
76 | downloadOptions.filenameMode = filenameMode;
|
77 | downloadOptions.directory = replacer.replace(directory);
|
78 | [].concat(filenameMode).forEach(function(i) {
|
79 | return downloadOptions.filenameMode[i] = true;
|
80 | });
|
81 | if (typeof filename === 'string') {
|
82 | downloadOptions.filenameMode['predefined'] = filename;
|
83 | }
|
84 | fileInstance = attrInstance.download(downloadOptions, _this._onDownload);
|
85 | if (execute !== false) {
|
86 | fileInstance.execute(execute);
|
87 | }
|
88 | if (extract !== false) {
|
89 | if (extract.to != null) {
|
90 | extract.to = replacer.replace(extract.to);
|
91 | }
|
92 | return fileInstance.extract(extract, _this._onExtract);
|
93 | }
|
94 | };
|
95 | })(this));
|
96 | };
|
97 |
|
98 | return OnParser;
|
99 |
|
100 | })();
|
101 |
|
102 | Parser = (function(superClass) {
|
103 | extend(Parser, superClass);
|
104 |
|
105 | function Parser() {
|
106 | return Parser.__super__.constructor.apply(this, arguments);
|
107 | }
|
108 |
|
109 | Parser.prototype._getObj = function(filepath) {
|
110 | var ext, obj, realfilepath;
|
111 | if (typeof filepath === 'object') {
|
112 | obj = filepath;
|
113 | } else {
|
114 | realfilepath = isAbsolute(filepath) ? filepath : join(process.cwd(), filepath);
|
115 | ext = extname(filepath);
|
116 | if (ext === '.json') {
|
117 | obj = require(realfilepath);
|
118 | } else if (ext === '.yml') {
|
119 | obj = YAML.load(realfilepath);
|
120 | }
|
121 | }
|
122 | return obj;
|
123 | };
|
124 |
|
125 | Parser.prototype.parse = function(filepath) {
|
126 | var obj;
|
127 | obj = this._getObj(filepath);
|
128 | return Parser.__super__.parse.call(this, obj);
|
129 | };
|
130 |
|
131 | return Parser;
|
132 |
|
133 | })(OnParser);
|
134 |
|
135 | module.exports = Parser;
|