forked from 1000ch/rog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
72 lines (57 loc) · 2.37 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
'use strict';
const got = require('got');
const isHTML = require('is-html');
const cheerio = require('cheerio');
const jschardet = require('jschardet');
const iconv = require('iconv-lite');
module.exports = (url, options) => {
options = options || {};
options.encoding = null;
return got(url, options).then(response => {
let result = jschardet.detect(response.body);
let body;
if (!result || !result.encoding || (result.confidence || 0) < 0.99) {
let head = response.body.toString('ascii').match(/<head[\s>]([\s\S]*?)<\/head>/i);
if (head) {
let charset = head[1].match(/<meta[^>]*[\s;]+charset\s*=\s*["']?([\w\-_]+)["']?/i);
if (charset) {
body = iconv.decode(response.body, charset[1].trim());
} else {
body = response.body.toString('utf8');
}
} else {
body = response.body.toString('utf8');
}
} else {
body = iconv.decode(response.body, result.encoding);
}
if (!isHTML(body)) {
return Promise.reject('Response is not HTML');
}
let $ = cheerio.load(body);
let title = $('meta[property="og:title"]').attr('content') ||
$('meta[name="twitter:title"]').attr('content') ||
$('meta[name="title"]').attr('content');
let type = $('meta[property="og:type"]').attr('content');
let url = $('meta[property="og:url"]').attr('content') ||
$('meta[name="twitter:url"]').attr('content') ||
$('link[rel="canonical"]').attr('href');
let image = $('meta[property="og:image"]').attr('content') ||
$('meta[name="twitter:image"]').attr('content');
let site = $('meta[property="og:site_name"]').attr('content') ||
$('meta[name="twitter:site"]').attr('content');
let description = $('meta[property="og:description"]').attr('content') ||
$('meta[name="twitter:description"]').attr('content') ||
$('meta[name="description"]').attr('content');
let locale = $('meta[property="og:locale"]').attr('content');
return Promise.resolve({
title : title || '',
type : type || '',
url : url || '',
image : image || '',
site : site || '',
description : description || '',
locale : locale || ''
});
});
};