-
Notifications
You must be signed in to change notification settings - Fork 43
/
index.js
224 lines (200 loc) · 6.75 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
216
217
218
219
220
221
222
223
224
/**
* 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
*/
const BBPromise = require( 'bluebird' );
const cheerio = require( 'cheerio' );
const preq = require( 'preq' ); // Promisified Request library
const fs = BBPromise.promisifyAll( require( 'fs' ) );
const 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( ( response ) => index.parseAll( cheerio.load( response.body ) ) ).nodeify( callback );
};
/**
* Exported function that takes html file and
* returns a BBPromise for all available metadata
*
* @param {string} path path Path to HTML file
* @param {Object} [opts] opts Additional options such as encoding
* @param {Function} [callback] Optional callback
* @return {Object} BBPromise for metadata
*/
exports.loadFromFile = function ( path, opts, callback ) {
const defaultEncoding = 'utf-8';
opts = opts || defaultEncoding;
if ( typeof opts === 'function' ) {
callback = opts;
opts = defaultEncoding;
}
return fs.readFileAsync( path, opts ).then(
( html ) => index.parseAll( cheerio.load( html ) ).nodeify( callback )
);
};
/**
* Exported function that takes html string and
* returns a BBPromise for all available metadata
*
* @param {string} html html String HTML of the page
* @param {Function} [callback] Optional callback
* @return {Object} BBPromise for metadata
*/
exports.loadFromString = function ( html, callback ) {
return index.parseAll( cheerio.load( html ) ).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 );
};
/**
* Scrapes PRISM data given html object
*
* @param {Object} chtml html Cheerio object
* @param {Function} [callback] optional callback function
* @return {Object} BBPromise for metadata
*/
exports.parsePrism = function ( chtml, callback ) {
return index.parsePrism( 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;