-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparseWebtoons.js
49 lines (38 loc) · 1.17 KB
/
parseWebtoons.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
const axios = require('axios');
const { parse } = require('node-html-parser');
const loadAndParseWebtoon = async( url ) => {
const { data } = await axios( url );
const parsed = parse( data );
const images = parsed.querySelectorAll('._images')
.map( img => img.rawAttributes['data-url'] );
return { images };
};
const loadAndParse네이버 = async( url ) => {
const { data } = await axios( url );
const parsed = parse( data );
const images = parsed.querySelectorAll('img')
.reduce( ( reduced, img ) => {
if ( img.id && img.id.includes('content_image') ) {
reduced.push( img.rawAttributes.src );
}
return reduced;
}, [] );
return { images };
};
const loadAndParse = ( r, { url } ) => {
const domain = url.match( /^(?:https?:\/\/)?(?:[^@\n]+@)?(?:www\.)?([^:\/\n?]+)/i );
if ( domain.length < 2 ) {
throw new Error('Invalid url received');
}
switch ( domain[ 1 ] ) {
case 'comic.naver.com':
return loadAndParse네이버( url );
case 'webtoons.com':
return loadAndParseWebtoon( url );
default:
throw new Error('Type not supported');
}
};
module.exports = {
loadAndParse
};