forked from mvolz/node-open-graph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
215 lines (177 loc) · 4.68 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
var http = require('http'),
https = require('https'),
cheerio = require('cheerio');
var shorthandProperties = {
"image": "image:url",
"video": "video:url",
"audio": "audio:url"
}
var iconv = require('iconv-lite'),
jschardet = require("jschardet");
exports = module.exports = function(url, cb, options){
exports.getHTML(url, function(err, html){
if (err) return cb(err);
var $ = cheerio.load(html);
cb(null, exports.parse($, options));
})
}
exports.getHTML = function(url, cb){
var purl = require('url').parse(url);
if (!purl.protocol)
purl = require('url').parse("http://"+url);
var httpModule = purl.protocol === 'https:'
? https
: http;
url = require('url').format(purl);
try{
var client = httpModule.get(url, function(res){
var chunks = [];
var matched = undefined;
if (res.headers['content-type']){
matched = res.headers['content-type'].match(/charset=(.*)/i);
}
var encode = undefined;
if (matched){
encode = matched[1];
}
res.on('data', function(data){
try{
chunks.push(data);
}catch(ex){
}
});
res.on('end', function(){
if (res.statusCode >= 300 && res.statusCode < 400)
{
exports.getHTML(res.headers.location, cb);
}
else
{
var html = "";
try{
html = iconv.decode(Buffer.concat(chunks), jschardet.detect(Buffer.concat(chunks))['encoding']);
}catch(ex){
try{
html = iconv.decode(Buffer.concat(chunks), 'utf-8');
}catch(ex){}
}
cb(null, html);
}
});
});
client.on('error', function(err){
cb(err);
});
}catch(ex){
cb(ex);
}
}
exports.parse = function($, options){
options = options || {};
// Check for xml namespace
var namespace,
$html = $('html');
if ($html.length)
{
var attribKeys = Object.keys($html[0].attribs);
attribKeys.some(function(attrName){
var attrValue = $html.attr(attrName);
if (attrValue.toLowerCase() === 'http://opengraphprotocol.org/schema/'
&& attrName.substring(0, 6) == 'xmlns:')
{
namespace = attrName.substring(6);
return false;
}
})
}
else if (options.strict)
return null;
if (!namespace)
// If no namespace is explicitly set..
if (options.strict)
// and strict mode is specified, abort parse.
return null;
else
// and strict mode is not specific, then default to "og"
namespace = "og";
var meta = {},
metaTags = $('meta');
meta.og = {};
meta.twitter = {};
var ogNamespace = "og";
var twitterNamespace = 'twitter';
metaTags.each(function() {
var element = $(this);
propertyAttr = element.attr('property'),
nameAttr = element.attr('name');
var ptr;
namespace = "";
// Standard og namespace
// <meta property="og:XXXX" content="" />
if (propertyAttr && propertyAttr.substring(0, ogNamespace.length) == ogNamespace){
namespace = ogNamespace;
ptr = meta.og;
}
// Standard twitter namespace
// <meta name="twitter:XXXX" content="" />
if (nameAttr && nameAttr.substring(0, twitterNamespace.length) == twitterNamespace){
namespace = twitterNamespace;
propertyAttr = nameAttr;
ptr = meta.twitter;
}
// This is fix for <meta name="og:XXXX" content="" />
if (nameAttr && nameAttr.substring(0, ogNamespace.length) == ogNamespace){
namespace = ogNamespace;
propertyAttr = nameAttr;
ptr = meta.og;
}
// If meta element isn't an "og:" property, skip it
if ( namespace === ""){
if (nameAttr){
meta[nameAttr] = element.attr('content');
}
return;
}
var property = propertyAttr.substring(namespace.length+1),
content = element.attr('content');
// If property is a shorthand for a longer property,
// Use the full property
property = shorthandProperties[property] || property;
var keys = property.split(':');
// we want to leave one key to assign to so we always use references
// as long as there's one key left, we're dealing with a sub-node and not a value
extractMeta(ptr, keys, content);
});
return meta;
}
function extractMeta(ptr, keys, content){
var key, tmp;
while (keys.length > 1) {
key = keys.shift();
if (Array.isArray(ptr[key])) {
// the last index of ptr[key] should become
// the object we are examining.
tmp = ptr[key].length-1;
ptr = ptr[key];
key = tmp;
}
if (typeof ptr[key] === 'string') {
// if it's a string, convert it
ptr[key] = { '': ptr[key] };
} else if (ptr[key] === undefined) {
// create a new key
ptr[key] = {};
}
// move our pointer to the next subnode
ptr = ptr[key];
}
// deal with the last key
key = keys.shift();
if (ptr[key] === undefined) {
ptr[key] = content;
} else if (Array.isArray(ptr[key])) {
ptr[key].push(content);
} else {
ptr[key] = [ ptr[key], content ];
}
}