Skip to content

Commit

Permalink
fix(audio): only consider http URLs (#678)
Browse files Browse the repository at this point in the history
* refactor(media-provider): remove unnecessary dependency

* fix(audio): only consider http URLs
  • Loading branch information
Kikobeats authored Dec 20, 2023
1 parent eca5a38 commit 3f03005
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/metascraper-audio/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"p-reflect": "~2.1.0"
},
"devDependencies": {
"async-listen": "latest",
"ava": "5"
},
"engines": {
Expand Down
2 changes: 1 addition & 1 deletion packages/metascraper-audio/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ module.exports = ({ getIframe = _getIframe } = {}) => {
audio: audioRules.concat(
async ({ htmlDom: $, url }) => {
const srcs = [
...new $('iframe')
...new $('iframe[src^="http"], iframe[src^="/"]')
.map((_, element) => $(element).attr('src'))
.get()
.map(src => normalizeUrl(url, src))
Expand Down
57 changes: 57 additions & 0 deletions packages/metascraper-audio/test/iframe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'use strict'

const { default: listen } = require('async-listen')
const { createServer } = require('http')
const { promisify } = require('util')
const test = require('ava')

const closeServer = server => promisify(server.close)

const createMetascraper = (...args) =>
require('metascraper')([require('../src')(...args)])

test('absolute http', async t => {
const server = createServer((_, res) => {
res.setHeader('Content-Type', 'text/html')
res.end(
'<meta property="og:audio" content="https://cdn.microlink.io/file-examples/sample.mp3">'
)
})

t.teardown(() => closeServer(server))
const url = (await listen(server, { port: 0, host: '0.0.0.0' })).toString()
const html = `<iframe src="${url}">`
const metascraper = createMetascraper()
const metadata = await metascraper({ html, url })
t.is(metadata.audio, 'https://cdn.microlink.io/file-examples/sample.mp3')
})

test('relative http', async t => {
const server = createServer((_, res) => {
res.setHeader('Content-Type', 'text/html')
res.end('<meta property="og:audio" content="/file-examples/sample.mp3">')
})

t.teardown(() => closeServer(server))
const url = (await listen(server, { port: 0, host: '0.0.0.0' })).toString()
const html = '<iframe src="/">'
const metascraper = createMetascraper()
const metadata = await metascraper({ html, url })
t.is(metadata.audio, url + 'file-examples/sample.mp3')
})

test('ignore non http urls', async t => {
const server = createServer((_, res) => {
res.setHeader('Content-Type', 'text/html')
res.end(
'<meta property="og:audio" content="tg://join?invite=n3gS0R7pjFJhMWM0">'
)
})

t.teardown(() => closeServer(server))
const url = (await listen(server, { port: 0, host: '0.0.0.0' })).toString()
const html = `<iframe src="${url}">`
const metascraper = createMetascraper()
const metadata = await metascraper({ html, url })
t.is(metadata.audio, null)
})
1 change: 0 additions & 1 deletion packages/metascraper-media-provider/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"debug-logfmt": "~1.0.4",
"got": "~11.8.6",
"lodash": "~4.17.21",
"p-do-whilst": "~1.1.0",
"p-reflect": "~2.1.0",
"p-retry": "~4.6.1",
"p-timeout": "~4.1.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const debug = require('debug-logfmt')(
const { serializeError } = require('serialize-error')
const youtubedl = require('youtube-dl-exec')
const { get, constant } = require('lodash')
const pDoWhilst = require('p-do-whilst')
const pTimeout = require('p-timeout')

const RE_UNSUPORTED_URL = /Unsupported URL/
Expand Down Expand Up @@ -53,16 +52,19 @@ module.exports = ({
const userAgent = get(gotOpts, 'headers.user-agent')

const task = async () => {
await pDoWhilst(async () => {
do {
try {
const proxy = getProxy({ url, retryCount: retryCount++ })
const flags = getFlags({ url, proxy, userAgent, cacheDir })
data = await youtubedl(url, flags, { timeout, ...props })
} catch (error) {
if (condition()) { debug('getInfo:error', { retryCount }, serializeError(error)) }
if (condition()) {
debug('getInfo:error', { retryCount }, serializeError(error))
}
isSupportedURL = !RE_UNSUPORTED_URL.test(error.stderr)
console.log('catch', { isSupportedURL })
}
}, condition)
} while (condition())

return data
}
Expand Down

0 comments on commit 3f03005

Please sign in to comment.