-
Notifications
You must be signed in to change notification settings - Fork 25
/
index.js
executable file
路100 lines (97 loc) 路 3.32 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const { options } = require('./lib/options');
const { fetch } = require('./lib/fetch');
const { flatten } = require('./lib/utils');
const Promise = require('bluebird');
const limit = require('p-limit')(10);
const MOVIE_DETAIL_PAGE_URI_SELECTOR = '.co_content8 .ulink';
const MOVIE_TITLE_SELECTOR = '#header > div > div.bd2 > div.bd3 > div.bd3r > div.co_area2 > div.title_all > h1 > font';
const MOVIE_IMAGE_SELECTOR = '#Zoom span img';
const MOVIE_DOWNLOAD_LINK_SELECTOR = '#Zoom > span > table > tbody > tr > td > a';
const DEFAULT_INCLUDE = ['title', 'imgUrl', 'desc', 'downloadLink', 'descPageLink'];
const MOVIE_DESC_SELETOR = '#Zoom > span > p:nth-child(1)';
/**
* Get movie detail page links
*
* @param {Object} cheerio
* @returns {Array} movieDetailPageLinks
*/
function handleGetMovieDetailPageLinks (cheerio) {
return cheerio(MOVIE_DETAIL_PAGE_URI_SELECTOR).map((idx, ele) => {
return cheerio(ele).attr('href');
}).get() || [];
}
/**
* Get movie details
*
* @param {Object} cheerio
* @param {Object} config
* @returns {Array} movieDetails
*/
function handleGetMovieDetails (cheerio, uri, config) {
let { include } = config;
if (!include.length) include = DEFAULT_INCLUDE;
let result = {};
include.forEach(item => {
if (item === 'title') {
const title = cheerio(MOVIE_TITLE_SELECTOR).text();
if (title) result[item] = cheerio(MOVIE_TITLE_SELECTOR).text();
else return false;
}
if (item === 'imgUrl') {
const imgUrl = cheerio(MOVIE_IMAGE_SELECTOR).eq(0).map((idx, ele) => {
return cheerio(ele).attr('src');
}).get()[0];
if (imgUrl) result[item] = imgUrl;
else return false;
}
if (item === 'desc') {
const desc = cheerio(MOVIE_DESC_SELETOR).text();
if (desc) result[item] = desc;
else return false;
}
if (item === 'downloadLink') {
const downloadLink = cheerio(MOVIE_DOWNLOAD_LINK_SELECTOR).text();
if (downloadLink) result[item] = downloadLink;
else return false;
}
if (item === 'descPageLink') {
const descPageLink = uri;
if (descPageLink) result[item] = descPageLink;
else return false;
}
});
return JSON.stringify(result) === '{}' ? undefined : result;
}
/**
* Get Dytt movies
*
* @param {Object} config
* @returns {Promise} movieDetails
*/
module.exports = (config) => {
config = config || { page: 1, include: DEFAULT_INCLUDE };
if (!config.page) throw Error('Config.page is required');
let pool = [];
for (let i = 0; i < 1; i++) {
pool.push(limit(() =>
fetch({ ...options(`https://www.dytt8.net/html/gndy/dyzz/list_23_${i + 1}.html`) })
.then($ => handleGetMovieDetailPageLinks($))
.catch(err => err.toString())
));
}
return Promise.all(pool)
.then(movieDetailPageLinks => flatten(movieDetailPageLinks)) // get solo movie link
.then(movieDetailPageLinks => {
pool = [];
for (let i = 0; i < movieDetailPageLinks.length; i++) {
pool.push(limit(() =>
fetch({ ...options(`https://www.dytt8.net${movieDetailPageLinks[i]}`) })
.then($ => handleGetMovieDetails($, `https://www.dytt8.net${movieDetailPageLinks[i]}`, config))
.catch(err => err.toString())
));
}
return pool;
})
.then(pool => Promise.all(pool)) // get solo movie details
.then(result => result.filter(v => v));
};