Skip to content

Commit

Permalink
Merge pull request #77 from ndaidong/v1.4.2
Browse files Browse the repository at this point in the history
v1.4.2
  • Loading branch information
ndaidong authored Oct 31, 2020
2 parents 0a2a8ea + db6c72e commit b1527c5
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: node_js
node_js:
- 15
- 14
- 13
- 10.14.2
git:
depth: 1
Expand Down
31 changes: 28 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@
Extract eEmbed content from given URL.

[![NPM](https://badge.fury.io/js/oembed-parser.svg)](https://badge.fury.io/js/oembed-parser)
[![Build Status](https://travis-ci.org/ndaidong/oembed-parser.svg?branch=master)](https://travis-ci.org/ndaidong/oembed-parser)
[![Coverage Status](https://coveralls.io/repos/github/ndaidong/oembed-parser/badge.svg?branch=master&updated=1)](https://coveralls.io/github/ndaidong/oembed-parser?branch=master)
[![Build Status](https://travis-ci.org/ndaidong/oembed-parser.svg)](https://travis-ci.org/ndaidong/oembed-parser)
[![Coverage Status](https://coveralls.io/repos/github/ndaidong/oembed-parser/badge.svg)](https://coveralls.io/github/ndaidong/oembed-parser)


### Important note:

- [Changes with Instagram](#changes-with-instagram)



## Demo

Expand Down Expand Up @@ -77,11 +84,29 @@ For the expected format, see the
[default list](https://raw.githubusercontent.com/ndaidong/oembed-parser/master/src/utils/providers.json).


#### Provider list
### Provider list

List of resource providers is a clone of [oembed.com](http://oembed.com/providers.json) and available [here](https://raw.githubusercontent.com/ndaidong/oembed-parser/master/src/utils/providers.json).


## Changes with Instagram

Since October 24 2020, Facebook have deprecated their legacy urls and applied a new Facebook oEmbed endpoints. Please update your `oembed-parser` version to v1.4.2 to be able to extract Instagram links.

Technically, now we have to use Facebook Graph API, with the access token from a valid and live Facebook app. By default, `oembed-parser` build Graph API endpoint using a pre-existing access token. Althrough it should work in almost cases. However, we recommend to add your own ones.


```
export FACEBOOK_APP_ID=your_app_id
export FACEBOOK_CLIENT_TOKEN=your_client_token
```

For more info, please refer:

- [Facebook oEmbed](https://developers.facebook.com/docs/plugins/oembed)


## Test

```bash
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.4.1",
"version": "1.4.2",
"name": "oembed-parser",
"description": "Get oEmbed data from given URL.",
"homepage": "https://www.npmjs.com/package/oembed-parser",
Expand All @@ -21,12 +21,12 @@
"reset": "node reset"
},
"dependencies": {
"node-fetch": "^2.6.0"
"node-fetch": "^2.6.1"
},
"devDependencies": {
"coveralls": "^3.1.0",
"eslint-config-goes": "^1.1.8",
"jest": "^26.4.0"
"jest": "^26.6.1"
},
"keywords": [
"oembed",
Expand Down
25 changes: 25 additions & 0 deletions src/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ const PhotoTypeKeys = [
...required,
];

const InstagramKeys = [
'html',
'width',
...optional,
...required,
];

const hasProperty = (obj, key) => {
return Object.prototype.hasOwnProperty.call(obj, key);
};
Expand All @@ -48,6 +55,12 @@ const hasPhotoKeys = (o) => {
});
};

const hasInstagramKeys = (o) => {
return InstagramKeys.every((k) => {
return hasProperty(o, k);
});
};

(() => {
const badSamples = [
'',
Expand Down Expand Up @@ -107,6 +120,18 @@ test(`test extract Flickr link with params`, async () => {
}
});


test(`test extract Instagram link`, async () => {
try {
const url = 'https://www.instagram.com/p/ic7kRDqOlt/';
const result = await extract(url);
expect(hasInstagramKeys(result)).toBe(true);
} catch (err) {
expect(err).toBe(null);
}
});


test(`test .hasProvider() method`, () => {
expect(hasProvider('https://www.youtube.com/watch?v=zh9NgGf3cxU')).toBe(true);
expect(hasProvider('https://trello.com/b/BO3bg7yn/notes')).toBe(false);
Expand Down
23 changes: 19 additions & 4 deletions src/utils/fetchEmbed.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,28 @@

const fetch = require('node-fetch').default;

const isInstagram = (provider) => {
return provider.provider_name === 'Instagram';
};

const getInstGraphUrl = (query) => {
const baseUrl = 'https://graph.facebook.com/v8.0/instagram_oembed';
const env = process.env || {};
const appId = env.FACEBOOK_APP_ID || '365101066946402';
const clientToken = env.FACEBOOK_CLIENT_TOKEN || 'a56861eb5b787f9e9a18e4e09ea5c873';
return `${baseUrl}?${query}&access_token=${appId}|${clientToken}`;
};

const getRegularUrl = (query, basseUrl) => {
return basseUrl.replace(/\{format\}/g, 'json') + '?' + query;
};

const fetchEmbed = async (url, provider, params = {}) => {
const {
provider_name, // eslint-disable-line camelcase
provider_url, // eslint-disable-line camelcase
url: resourceUrl,
} = provider;

const baseUrl = resourceUrl.replace(/\{format\}/g, 'json');

const queries = [
'format=json',
`url=${encodeURIComponent(url)}`,
Expand All @@ -27,8 +40,10 @@ const fetchEmbed = async (url, provider, params = {}) => {
if (maxheight > 0) {
queries.push(`maxheight=${maxheight}`);
}
const query = queries.join('&');

const link = `${baseUrl}?${queries.join('&')}`;
const link = isInstagram(provider) ? getInstGraphUrl(query) : getRegularUrl(query, provider.url);
console.log(link);
const res = await fetch(link, {mode: 'no-cors'});
const json = await res.json();
json.provider_name = provider_name; // eslint-disable-line camelcase
Expand Down
125 changes: 125 additions & 0 deletions src/utils/providers.json
Original file line number Diff line number Diff line change
Expand Up @@ -3696,5 +3696,130 @@
"discovery": true
}
]
},
{
"provider_name": "Lumiere",
"provider_url": "https://latd.com",
"endpoints": [
{
"schemes": [
"https://*.lumiere.is/v/*"
],
"url": "https://admin.lumiere.is/api/services/oembed",
"discovery": true
}
]
},
{
"provider_name": "NoPaste",
"provider_url": "https://nopaste.ml",
"endpoints": [
{
"schemes": [
"https://nopaste.ml/*"
],
"url": "https://oembed.nopaste.ml",
"discovery": false
}
]
},
{
"provider_name": "Observable",
"provider_url": "https://observablehq.com",
"endpoints": [
{
"schemes": [
"https://observablehq.com/@*/*",
"https://observablehq.com/d/*",
"https://observablehq.com/embed/*"
],
"url": "https://api.observablehq.com/oembed",
"formats": [
"json"
]
}
]
},
{
"provider_name": "rcvis",
"provider_url": "https://www.rcvis.com/",
"endpoints": [
{
"schemes": [
"https://www.rcvis.com/v/*",
"https://www.rcvis.com/visualize=*",
"https://www.rcvis.com/ve/*",
"https://www.rcvis.com/visualizeEmbedded=*"
],
"url": "https://animatron.com/oembed",
"discovery": true
}
]
},
{
"provider_name": "Saooti",
"provider_url": "https://octopus.saooti.com",
"endpoints": [
{
"schemes": [
"https://octopus.saooti.com/main/pub/podcast/*"
],
"url": "https://octopus.saooti.com/oembed"
}
]
},
{
"provider_name": "Subscribi",
"provider_url": "https://subscribi.io/",
"endpoints": [
{
"schemes": [
"https://subscribi.io/api/oembed*"
],
"url": "https://subscribi.io/api/oembed",
"discovery": true
}
]
},
{
"provider_name": "TourHero",
"provider_url": "http://www.tourhero.com",
"endpoints": [
{
"schemes": [
"https://www.tourhero.com/*"
],
"url": "https://oembed.tourhero.com/",
"discovery": true,
"formats": [
"json"
]
}
]
},
{
"provider_name": "Tumblr",
"provider_url": "https://www.tumblr.com",
"endpoints": [
{
"schemes": [
"https://*.tumblr.com/post/*"
],
"url": "https://www.tumblr.com/oembed/1.0"
}
]
},
{
"provider_name": "Wolfram Cloud",
"provider_url": "https://www.wolframcloud.com",
"endpoints": [
{
"schemes": [
"https://*.wolframcloud.com/*"
],
"url": "https://www.wolframcloud.com/oembed",
"discovery": true
}
]
}
]

0 comments on commit b1527c5

Please sign in to comment.