Skip to content

Commit

Permalink
Add auto discovery support
Browse files Browse the repository at this point in the history
  • Loading branch information
ndaidong committed May 7, 2024
1 parent 8ad2a6f commit de94e3e
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 0 deletions.
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
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()
})
})
74 changes: 74 additions & 0 deletions test-data/bitchute.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>2023 Praemium Imperiale White House Program</title>
<meta name="viewport" content="initial-scale=1.0, width=device-width" />
<link id="canonical" rel="canonical" href="https://www.bitchute.com/video/8hXWnkvA8Ao/" />
<link rel="apple-touch-icon" sizes="57x57" href="/static/1.4.0/images/apple-icon-57x57.png">
<link rel="apple-touch-icon" sizes="60x60" href="/static/1.4.0/images/apple-icon-60x60.png">
<link rel="apple-touch-icon" sizes="72x72" href="/static/1.4.0/images/apple-icon-72x72.png">
<link rel="apple-touch-icon" sizes="76x76" href="/static/1.4.0/images/apple-icon-76x76.png">
<link rel="apple-touch-icon" sizes="114x114" href="/static/1.4.0/images/apple-icon-114x114.png">
<link rel="apple-touch-icon" sizes="120x120" href="/static/1.4.0/images/apple-icon-120x120.png">
<link rel="apple-touch-icon" sizes="144x144" href="/static/1.4.0/images/apple-icon-144x144.png">
<link rel="apple-touch-icon" sizes="152x152" href="/static/1.4.0/images/apple-icon-152x152.png">
<link rel="apple-touch-icon" sizes="180x180" href="/static/1.4.0/images/apple-icon-180x180.png">
<link rel="icon" type="image/png" sizes="192x192" href="/static/1.4.0/images/android-icon-192x192.png">
<link rel="icon" type="image/png" sizes="32x32" href="/static/1.4.0/images/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="96x96" href="/static/1.4.0/images/favicon-96x96.png">
<link rel="icon" type="image/png" sizes="16x16" href="/static/1.4.0/images/favicon-16x16.png">
<link rel="manifest" href="/static/1.4.0/manifest.json">
<meta name="msapplication-TileColor" content="#ffffff">
<meta name="msapplication-TileImage" content="/static/1.4.0/images/ms-icon-144x144.png">
<meta name="theme-color" content="#ffffff">
<meta name="duration" content="35:28">
<meta name="description" content="First Lady Jill Biden hosts an event at the White House to celebrate the 2023 Praemium Imperiale Laureates. The event will feature remarks from First Lady Jill Biden and former Secretary of State Hillary Rodham Clinton.
The Praemium Imperiale is a …" />
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:url" content="https://static-3.bitchute.com/live/cover_images/zWsYVmCOu4JA/8hXWnkvA8Ao_640x360.jpg">
<meta name="twitter:title" content="2023 Praemium Imperiale White House Program">
<meta name="twitter:description" content="First Lady Jill Biden hosts an event at the White House to celebrate the 2023 Praemium Imperiale Laureates. The event will feature remarks from First Lady Jill Biden and former Secretary of State Hillary Rodham Clinton.
The Praemium Imperiale is a …">
<meta name="twitter:site" content="@bitchute">
<meta name="twitter:domain" content="bitchute.com">
<meta name="twitter:image:src" content="https://static-3.bitchute.com/live/cover_images/zWsYVmCOu4JA/8hXWnkvA8Ao_640x360.jpg">
<meta name="twitter:image:width" content="640">
<meta name="twitter:image:height" content="360">
<meta name="twitter:creator" content="@bitchute">
<meta property="og:site_name" content="BitChute" />
<meta property="og:url" content="https://www.bitchute.com/video/8hXWnkvA8Ao/" />
<meta property="og:title" content="2023 Praemium Imperiale White House Program" />
<meta property="og:description" content="First Lady Jill Biden hosts an event at the White House to celebrate the 2023 Praemium Imperiale Laureates. The event will feature remarks from First Lady Jill Biden and former Secretary of State Hillary Rodham Clinton.
The Praemium Imperiale is a …" />
<meta property="og:image" content="https://static-3.bitchute.com/live/cover_images/zWsYVmCOu4JA/8hXWnkvA8Ao_640x360.jpg" />
<meta property="og:image:secure_url" content="https://static-3.bitchute.com/live/cover_images/zWsYVmCOu4JA/8hXWnkvA8Ao_640x360.jpg" />
<meta property="og:image:type" content="image/jpeg" />
<meta property="og:image:width" content="640" />
<meta property="og:image:height" content="360" />
<meta property="og:type" content="video.other" />
<link id="oembed" rel="alternate" type="application/json+oembed" href="https://www.bitchute.com/oembed/?url=https://www.bitchute.com/video/8hXWnkvA8Ao/&amp;format=json">
</head>
<body>
<div id="auth_modal" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="fas fa-times fa-fw"></i></button>
<a href="/"><img class="modal-logo logo-full" src="/static/1.4.0/images/logo-full-day.png" alt="BitChute"></a>
</div>
<div class="modal-body">
<ul class="nav nav-tabs">
<li class="active" id="auth_login_tab">
<a data-toggle="tab" href="#auth_login">Login</a>
</li>
<li id="auth_reset_tab">
<a data-toggle="tab" href="#auth_reset">Password Reset</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</body>
</html>
1 change: 1 addition & 0 deletions test-data/bitchute.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"title": "2023 Praemium Imperiale White House Program", "provider_url": "https://www.bitchute.com/", "author_name": "TheWhiteHouse", "thumbnail_url": "https://static-3.bitchute.com/live/cover_images/zWsYVmCOu4JA/8hXWnkvA8Ao_320x180.jpg", "html": "<iframe width=\"459\" height=\"344\" src=\"https://www.bitchute.com/embed/8hXWnkvA8Ao/?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>", "author_url": "https://www.bitchute.com/channel/zWsYVmCOu4JA/", "height": "344", "thumbnail_height": "180", "version": "1.0", "type": "video", "thumbnail_width": "320", "provider_name": "BitChute", "width": "459"}

0 comments on commit de94e3e

Please sign in to comment.