Skip to content

Commit

Permalink
Merge pull request #185 from extractus/4.0.5
Browse files Browse the repository at this point in the history
v4.0.5
  • Loading branch information
ndaidong authored May 7, 2024
2 parents cd08c33 + de94e3e commit 0edfc1f
Show file tree
Hide file tree
Showing 21 changed files with 295 additions and 44 deletions.
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ yarn add @extractus/oembed-extractor
```

```ts
// es6 module
import { extract } from '@extractus/oembed-extractor'

const result = await extract('https://www.youtube.com/watch?v=x2bqscVkGxk')
Expand All @@ -39,10 +38,6 @@ console.log(result)
### Deno

```ts
// deno < 1.28
import { extract } from 'https://esm.sh/@extractus/oembed-extractor'

// deno > 1.28
import { extract } from 'npm:@extractus/oembed-extractor'
```

Expand Down
5 changes: 0 additions & 5 deletions deno.json

This file was deleted.

4 changes: 2 additions & 2 deletions examples/browser-oembed-parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"start": "node server"
},
"dependencies": {
"express": "^4.18.2",
"got": "^13.0.0"
"express": "latest",
"got": "latest"
}
}
4 changes: 2 additions & 2 deletions examples/bun-oembed-parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"start": "bun run index.ts"
},
"devDependencies": {
"bun-types": "^0.6.13"
"bun-types": "latest"
},
"dependencies": {
"hono": "^3.2.7",
"hono": "latest",
"@extractus/oembed-extractor": "latest"
}
}
2 changes: 1 addition & 1 deletion examples/deno-oembed-parser/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { serve } from 'https://deno.land/std/http/server.ts'

import { Hono } from 'https://deno.land/x/hono@v3.2.7/mod.ts'
import { Hono } from 'https://deno.land/x/hono/mod.ts'

import { extract } from 'npm:@extractus/oembed-extractor'

Expand Down
2 changes: 1 addition & 1 deletion examples/node-oembed-parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"start": "node index.js"
},
"dependencies": {
"express": "^4.18.2",
"express": "latest",
"@extractus/oembed-extractor": "latest"
}
}
4 changes: 2 additions & 2 deletions examples/tsnode-oembed-parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
"start": "node dist/index.js"
},
"devDependencies": {
"typescript": "^5.1.6"
"typescript": "latest"
},
"dependencies": {
"express": "^4.18.2",
"express": "latest",
"@extractus/oembed-extractor": "latest"
}
}
15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "4.0.4",
"version": "4.0.5",
"name": "@extractus/oembed-extractor",
"description": "Get oEmbed data from given URL.",
"homepage": "https://github.com/extractus/oembed-extractor",
Expand All @@ -11,10 +11,12 @@
"main": "./src/main.js",
"type": "module",
"imports": {
"cross-fetch": "./src/deno/cross-fetch.js"
"cross-fetch": "./src/deno/cross-fetch.js",
"linkedom": "https://deno.land/x/[email protected]/deno-dom-wasm.ts"
},
"browser": {
"cross-fetch": "./src/deno/cross-fetch.js"
"cross-fetch": "./src/deno/cross-fetch.js",
"linkedom": "./src/browser/linkedom.js"
},
"types": "./index.d.ts",
"engines": {
Expand All @@ -30,11 +32,12 @@
"reset": "node reset"
},
"dependencies": {
"cross-fetch": "^4.0.0"
"cross-fetch": "^4.0.0",
"linkedom": "^0.16.11"
},
"devDependencies": {
"eslint": "^9.1.1",
"globals": "^15.0.0",
"eslint": "^9.2.0",
"globals": "^15.1.0",
"https-proxy-agent": "^7.0.4",
"jest": "^29.7.0",
"nock": "^13.5.4"
Expand Down
1 change: 1 addition & 0 deletions src/browser/linkedom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const DOMParser = window.DOMParser
9 changes: 4 additions & 5 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// main.js

import { isValid as isValidURL } from './utils/linker.js'
import extractWithDiscovery from './utils/autoDiscovery.js'
import fetchEmbed from './utils/fetchEmbed.js'

import { getEndpoint } from './utils/provider.js'
Expand All @@ -10,12 +11,10 @@ export const extract = async (url, params = {}, options = {}) => {
throw new Error('Invalid input URL')
}
const endpoint = getEndpoint(url)
if (!endpoint) {
throw new Error(`No provider found with given url "${url}"`)
}

const data = await fetchEmbed(url, params, endpoint, options)
return data
return endpoint
? fetchEmbed(url, params, endpoint, options)
: extractWithDiscovery(url, params, options)
}

export {
Expand Down
23 changes: 23 additions & 0 deletions src/utils/autoDiscovery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// utils -> autoDiscovery.js

import { DOMParser } from 'linkedom'

import { getHtml, getJson } from './retrieve.js'

export default async (url, params = {}, options = {}) => {
const html = await getHtml(url, options)
const doc = new DOMParser().parseFromString(html, 'text/html')
const elm = doc.querySelector('link[type="application/json+oembed"]')
const href = elm.getAttribute('href')
const q = new URL(href)
const { origin, pathname, searchParams } = q
Object.keys(params).forEach((key) => {
if (!searchParams.has(key)) {
searchParams.append(key, params[key])
}
})
const link = `${origin}${pathname}?${searchParams.toString()}`
const body = await getJson(link, options)
body.method = 'auto-discovery'
return body
}
53 changes: 53 additions & 0 deletions src/utils/autoDiscovery.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// autoDiscovery.test
/* eslint-env jest */

import nock from 'nock'

import autoDiscovery from './autoDiscovery.js'

const parseUrl = (url) => {
const re = new URL(url)
return {
baseUrl: `${re.protocol}//${re.host}`,
path: re.pathname,
}
}

describe('test if autoDiscovery() works correctly', () => {
const url = 'https://www.bitchute.com/video/8hXWnkvA8Ao/'
test(`check fetchEmbed("${url}")`, async () => {
const htmlFile = './test-data/bitchute.html'
const jsonFile = './test-data/bitchute.json'

const { baseUrl, path } = parseUrl(url)
const scope = nock(baseUrl)
scope.get(path)
.replyWithFile(200, htmlFile, {
'Content-Type': 'text/html',
})

const endpoint = 'https://www.bitchute.com/oembed/'
const { baseUrl: endpointBaseUrl, path: endpointPath } = parseUrl(endpoint)

const params = {
maxwidth: 600,
maxheight: 400,
}

const jsonScope = nock(endpointBaseUrl, { encodedQueryParams: true })
const queries = new URLSearchParams({
url: 'https://www.bitchute.com/video/8hXWnkvA8Ao/',
format: 'json',
...params,
})
jsonScope.get(endpointPath)
.query(queries)
.replyWithFile(200, jsonFile, {
'Content-Type': 'application/json',
})

const result = await autoDiscovery(url, params)
expect(result).toBeTruthy()
nock.cleanAll()
})
})
5 changes: 3 additions & 2 deletions src/utils/fetchEmbed.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// utils -> fetchEmbed

import retrieve from './retrieve.js'
import { getJson } from './retrieve.js'
import { getDomain } from './linker.js'

const isFacebookGraphDependent = (url) => {
Expand Down Expand Up @@ -34,6 +34,7 @@ export default async (url, params = {}, endpoint = '', options = {}) => { // esl

const queryParams = new URLSearchParams(query).toString()
const link = endpoint + '?' + queryParams
const body = retrieve(link, options)
const body = await getJson(link, options)
body.method = 'provider-api'
return body
}
20 changes: 19 additions & 1 deletion src/utils/providers.latest.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// provider data, synchronized at 2024-04-26T08:46:02.055Z
// provider data, synchronized at 2024-05-07T10:41:10.221Z

/* eslint-disable */

Expand Down Expand Up @@ -198,6 +198,12 @@ export const providers = [
],
"e": "www.behance.net/services/oembed"
},
{
"s": [
"cloud\\.biqapp\\.com/*"
],
"e": "biqapp.com/api/v1/video/oembed"
},
{
"s": [
"blackfire\\.io/profiles/*/graph",
Expand Down Expand Up @@ -277,6 +283,12 @@ export const providers = [
],
"e": "img.catbo.at/oembed.json"
},
{
"s": [
"embeds\\.celero\\.io/*"
],
"e": "api.celero.io/api/oembed"
},
{
"s": [
"view\\.ceros\\.com/*"
Expand Down Expand Up @@ -816,6 +828,12 @@ export const providers = [
],
"e": "www.hulu.com/api/oembed.json"
},
{
"s": [
"oembed\\.ideamapper\\.com/*"
],
"e": "oembed.ideamapper.com"
},
{
"s": [
"*\\.idomoo\\.com/*"
Expand Down
42 changes: 42 additions & 0 deletions src/utils/providers.orginal.json
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,19 @@
}
]
},
{
"provider_name": "biqnetwork",
"provider_url": "https://biqapp.com/",
"endpoints": [
{
"schemes": [
"https://cloud.biqapp.com/*"
],
"url": "https://biqapp.com/api/v1/video/oembed",
"discovery": true
}
]
},
{
"provider_name": "Blackfire.io",
"provider_url": "https://blackfire.io",
Expand Down Expand Up @@ -591,6 +604,22 @@
}
]
},
{
"provider_name": "Celero",
"provider_url": "https://www.celero.io",
"endpoints": [
{
"schemes": [
"https://embeds.celero.io/*"
],
"url": "https://api.celero.io/api/oembed",
"discovery": true,
"formats": [
"json"
]
}
]
},
{
"provider_name": "Ceros",
"provider_url": "http://www.ceros.com/",
Expand Down Expand Up @@ -1698,6 +1727,19 @@
}
]
},
{
"provider_name": "Ideamapper",
"provider_url": "http://oembed.ideamapper.com",
"endpoints": [
{
"schemes": [
"http://oembed.ideamapper.com/*"
],
"url": "http://oembed.ideamapper.com",
"discovery": true
}
]
},
{
"provider_name": "Idomoo",
"provider_url": "https://idomoo.com/",
Expand Down
Loading

0 comments on commit 0edfc1f

Please sign in to comment.