oggy
Scrape and fusion Open Graph, Twitter and other DOM metadata easily
Install
$ npm install oggy
Usage
const oggy = ; async { const scraper = ; const res = await scraper; // => res = {initialUrl: "...", oggyfied: {...}, og: {...}, twitter: {...}} // => res = {initialUrl: "...", error: {...}} if an error occurs };
API
oggy([options])
options
Type: Object
oggy.scrape(url, [options])
Returns Open Graph, Twitter and Oggyfied metadata.
- oggyfied - Type:
Object
- Unified metadata - og - Type:
Object
- Open Graph metadata - twitter - Type:
Object
- Twitter metadata - initialUrl - Type:
String
- The initial url - time - Type:
Long
- Scrape duration in milliseconds - error - Type:
Object
- If an error occurs (metadata won't be available)
url
Type: string
The url to scrape.
options
Type: Object
context
Type: Object
Default: {}
An optional context available inside hook.beforeRequest
and hook.beforeResponse
.
Be careful not to put sensitive data in this context unless you know what you're doing.
Hooks
Hooks allow to modify request headers and change returned metadata.
module { const name = 'captain-hook'; const handleUrl = { // Check if hook handles this url return /stephanecodes/; }; const beforeRequest = { // Add or override request headers }; const beforeResponse = { // Override result or do something with it // You also have access to initial url, content body, all parsed metadata. }; return name handleUrl beforeRequest beforeResponse;};
Use cases
Forward a token
This hook will add a header with current user login to every request.
module { const name = 'add-user-token'; const handleUrl = true; const beforeRequest = { ifcontextuserToken headers'x-user-token' = contextuserToken; }; return name handleUrl beforeRequest;};
In this case, the user's token is available in the context only if the url is on the same domain.
const hook = ;const scraper =
License
MIT