forked from wikimedia/html-metadata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
178 lines (159 loc) · 5.21 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
/**
* https://github.com/wikimedia/html-metadata
*
* This file wraps all exportable functions so that they
* can be used either with Promises or with callbacks.
*/
'use strict';
/*
Import modules
*/
var BBPromise = require('bluebird');
var cheerio = require('cheerio');
var preq = require('preq'); // Promisified Request library
var index = require('./lib/index.js');
/**
* Default exported function that takes a url string or
* request library options object and returns a
* BBPromise for all available metadata
*
* @param {Object} urlOrOpts url String or options Object
* @param {Function} [callback] Optional callback
* @return {Object} BBPromise for metadata
*/
exports = module.exports = function(urlOrOpts, callback) {
return preq.get(urlOrOpts
).then(function(response) {
return index.parseAll(cheerio.load(response.body));
}).nodeify(callback);
};
/**
* Returns Object containing all available datatypes, keyed
* using the same keys as in metadataFunctions.
*
* @param {Object} chtml html Cheerio object to parse
* @param {Function} [callback] optional callback function
* @return {Object} BBPromise for metadata
*/
exports.parseAll = function(chtml, callback){
return index.parseAll(chtml).nodeify(callback);
};
/**
* Scrapes BE Press metadata given html object
*
* @param {Object} chtml html Cheerio object
* @param {Function} [callback] optional callback function
* @return {Object} BBPromise for metadata
*/
exports.parseBEPress = function(chtml, callback){
return index.parseBEPress(chtml).nodeify(callback);
};
/**
* Scrapes embedded COinS data given Cheerio loaded html object
*
* @param {Object} chtml html Cheerio object
* @param {Function} [callback] optional callback function
* @return {Object} BBPromise for metadata
*/
exports.parseCOinS = function(chtml, callback){
return index.parseCOinS(chtml).nodeify(callback);
};
/**
* Parses value of COinS title tag
*
* @param {String} title String corresponding to value of title tag in span element
* @param {Function} [callback] Optional callback function
* @return {Object} BBPromise for metadata
*/
exports.parseCOinSTitle = function(title, callback){
return index.parseCOinSTitle(title).nodeify(callback);
};
/**
* Scrapes Dublin Core data given Cheerio loaded html object
*
* @param {Object} chtml html Cheerio object
* @param {Function} [callback] optional callback function
* @return {Object} BBPromise for metadata
*/
exports.parseDublinCore = function(chtml, callback){
return index.parseDublinCore(chtml).nodeify(callback);
};
/**
* Scrapes EPrints data given Cheerio loaded html object
*
* @param {Object} chtml html Cheerio object
* @param {Function} [callback] optional callback function
* @return {Object} BBPromise for metadata
*/
exports.parseEprints = function(chtml, callback){
return index.parseEprints(chtml).nodeify(callback);
};
/**
* Scrapes general metadata terms given Cheerio loaded html object
*
* @param {Object} chtml html Cheerio object
* @param {Function} [callback] optional callback function
* @return {Object} BBPromise for metadata
*/
exports.parseGeneral = function(chtml, callback){
return index.parseGeneral(chtml).nodeify(callback);
};
/**
* Scrapes Highwire Press metadata given html object
*
* @param {Object} chtml html Cheerio object
* @param {Function} [callback] optional callback function
* @return {Object} BBPromise for metadata
*/
exports.parseHighwirePress = function(chtml, callback){
return index.parseHighwirePress(chtml).nodeify(callback);
};
/**
* Retrieves JSON-LD for given html object
*
* @param {Object} chtml html Cheerio object
* @param {Function} [callback] optional callback function
* @return {Object} BBPromise for JSON-LD
*/
exports.parseJsonLd = function(chtml, callback){
return index.parseJsonLd(chtml).nodeify(callback);
};
/**
* Scrapes OpenGraph data given html object
*
* @param {Object} chtml html Cheerio object
* @param {Function} [callback] optional callback function
* @return {Object} BBPromise for metadata
*/
exports.parseOpenGraph = function(chtml, callback){
return index.parseOpenGraph(chtml).nodeify(callback);
};
/**
* Scrapes schema.org microdata given Cheerio loaded html object
*
* @param {Object} chtml html Cheerio object
* @param {Function} [callback] optional callback function
* @return {Object} BBPromise for metadata
*/
exports.parseSchemaOrgMicrodata = function(chtml, callback){
return index.parseSchemaOrgMicrodata(chtml).nodeify(callback);
};
/**
* Scrapes Twitter data given html object
*
* @param {Object} chtml html Cheerio object
* @param {Function} [callback] optional callback function
* @return {Object} BBPromise for metadata
*/
exports.parseTwitter = function(chtml, callback){
return index.parseTwitter(chtml).nodeify(callback);
};
/**
* Global exportable list of scraping promises with string keys
* @type {Object}
*/
exports.metadataFunctions = index.metadataFunctions;
/*
Export the version
*/
exports.version = require('./package').version;