Skip to content

Commit

Permalink
Merge pull request #55 from ndaidong/dev
Browse files Browse the repository at this point in the history
v1.3.6
  • Loading branch information
ndaidong committed Nov 1, 2019
2 parents 579a661 + 2a9c3de commit 87f24e2
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 26 deletions.
42 changes: 25 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
### Installation

```bash
npm i oembed-parser
npm install oembed-parser
```

### Usage
Expand All @@ -20,32 +20,40 @@ import {

const url = 'https://www.youtube.com/watch?v=8jPQjjsBbIc';

// Promise style
extract(url).then((data) => {
console.log(data);
extract(url).then((oembed) => {
console.log(oembed);
}).catch((err) => {
console.log(err);
console.trace(err);
});
```

### APIs

#### .extract(String url [, Object params])

Extract oEmbed data from specified url.
Return: a Promise

// async/await style
const getArticle = async (link) => {
Optional argument `params` is an object with it we can set `maxwidth` and/or `maxheight` those are used to scale embed size to fit your container size. Please refer [oEmbed/Full Spec/Consumer Request](https://oembed.com/#section2) for more info.

Here is how we can use `oembed-parser` in async/await style:

```js
import {
extract
} from 'oembed-parser';

const getOembed = async (url) => {
try {
let data = await extract(link);
return data;
const oembed = await extract(url);
return oembed;
} catch (err) {
return err;
console.trace(err);
}
}
};

console.log(getArticle(url));
```

### APIs

#### .extract(String URL)

Return a Promise object.

#### .hasProvider(String URL)

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.3.5",
"version": "1.3.6",
"name": "oembed-parser",
"description": "Get oEmbed data from given URL.",
"homepage": "https://www.npmjs.com/package/oembed-parser",
Expand All @@ -26,7 +26,7 @@
"devDependencies": {
"bellajs": "^8.0.1",
"eslint-config-goes": "^1.1.8",
"tap": "^14.8.2"
"tap": "^14.9.1"
},
"keywords": [
"oembed",
Expand Down
2 changes: 1 addition & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const {
const defaultProviderList = require('./utils/providers.json');
let providers = providersFromList(defaultProviderList);

const extract = async (url, params) => {
const extract = async (url, params = {}) => {
if (!isValidURL(url)) {
throw new Error('Invalid input URL');
}
Expand Down
25 changes: 19 additions & 6 deletions src/utils/fetchEmbed.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,33 @@

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

const fetchEmbed = async (url, provider, params) => {
let {
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');

resourceUrl = resourceUrl.replace(/\{format\}/g, 'json');
const queries = [
'format=json',
`url=${encodeURIComponent(url)}`,
];

let link = `${resourceUrl}?format=json&url=${encodeURIComponent(url)}`;
link = params && params.maxwidth ? `${link}&maxwidth=${params.maxwidth}` : link;
link = params && params.maxheight ? `${link}&maxheight=${params.maxheight}` : link;
const {
maxwidth = 0,
maxheight = 0,
} = params;

if (maxwidth > 0) {
queries.push(`maxwidth=${maxwidth}`);
}
if (maxheight > 0) {
queries.push(`maxheight=${maxheight}`);
}

const link = `${baseUrl}?${queries.join('&')}`;
const res = await fetch(link, {mode: 'no-cors'});
const json = await res.json();
json.provider_name = provider_name; // eslint-disable-line camelcase
Expand Down
117 changes: 117 additions & 0 deletions src/utils/providers.backup.json
Original file line number Diff line number Diff line change
Expand Up @@ -3030,5 +3030,122 @@
"url": "https://play.typecast.ai/oembed"
}
]
},
{
"provider_name": "Audioboom",
"provider_url": "https://audioboom.com",
"endpoints": [
{
"schemes": [
"https://audioboom.com/channel/*",
"https://audioboom.com/posts/*"
],
"url": "https://audioboom.com/publishing/oembed/v4.{format}",
"formats": [
"json",
"xml"
]
}
]
},
{
"provider_name": "Blogcast",
"provider_url": "https://blogcast.host/",
"endpoints": [
{
"schemes": [
"https://blogcast.host/embed/*",
"https://blogcast.host/embedly/*"
],
"url": "https://blogcast.host/oembed",
"discovery": true
}
]
},
{
"provider_name": "Facebook",
"provider_url": "https://www.facebook.com/",
"endpoints": [
{
"schemes": [
"https://www.facebook.com/*/posts/*",
"https://www.facebook.com/photos/*",
"https://www.facebook.com/*/photos/*",
"https://www.facebook.com/photo.php*",
"https://www.facebook.com/photo.php",
"https://www.facebook.com/*/activity/*",
"https://www.facebook.com/permalink.php",
"https://www.facebook.com/media/set?set=*",
"https://www.facebook.com/questions/*",
"https://www.facebook.com/notes/*/*/*"
],
"url": "https://www.facebook.com/plugins/post/oembed.json",
"discovery": true
},
{
"schemes": [
"https://www.facebook.com/*/videos/*",
"https://www.facebook.com/video.php"
],
"url": "https://www.facebook.com/plugins/video/oembed.json",
"discovery": true
}
]
},
{
"provider_name": "OZ",
"provider_url": "https://www.oz.com/",
"endpoints": [
{
"schemes": [
"https://www.oz.com/*/video/*"
],
"url": "https://core.oz.com/oembed",
"formats": [
"json",
"xml"
]
}
]
},
{
"provider_name": "Polaris Share",
"provider_url": "https://www.polarishare.com/",
"endpoints": [
{
"schemes": [
"https://www.polarishare.com/*/*"
],
"url": "https://api.polarishare.com/rest/api/oembed",
"discovery": true
}
]
},
{
"provider_name": "Typlog",
"provider_url": "https://typlog.com",
"endpoints": [
{
"url": "https://typlog.com/oembed",
"discovery": true
}
]
},
{
"provider_name": "uStudio, Inc.",
"provider_url": "https://www.ustudio.com",
"endpoints": [
{
"schemes": [
"https://*.ustudio.com/embed/*",
"https://*.ustudio.com/embed/*/*"
],
"url": "https://app.ustudio.com/api/v2/oembed",
"discovery": true,
"formats": [
"json"
]
}
]
}
]

0 comments on commit 87f24e2

Please sign in to comment.