Skip to content

Commit

Permalink
fix: use onError for capture error (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats authored Oct 28, 2018
1 parent 0ef7ad5 commit dcf9e23
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 21 deletions.
24 changes: 11 additions & 13 deletions packages/metascraper-media-provider/src/get-media/get-media.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,31 @@

const youtubedl = require('youtube-dl')
const { promisify } = require('util')
const { get } = require('lodash')
const { noop } = require('lodash')

const { isTwitterUrl } = require('./twitter-info')

const { PROXY_HOST, PROXY_PORT, PROXY_USER, PROXY_PASS } = process.env

const PROXY_URL =
PROXY_HOST && `http://${PROXY_USER}:${PROXY_PASS}@${PROXY_HOST}:${PROXY_PORT}`

const RE_RATE_LIMIT = /429: Too Many Requests/i

const getInfo = promisify(youtubedl.getInfo)

const isRateLimit = (str = '') => RE_RATE_LIMIT.test(str)

module.exports = ({ cacheDir } = {}) => {
module.exports = ({ cacheDir, onError = noop } = {}) => {
const opts = cacheDir ? [`--cache-dir=${cacheDir}`] : []

return async url => {
let data = {}

try {
data = await getInfo(url, opts)
data = await getInfo(
url,
isTwitterUrl(url) && PROXY_URL
? [...opts, `--proxy=${PROXY_URL}`]
: opts
)
} catch (err) {
if (isRateLimit(get(err, 'message')) && PROXY_URL) {
try {
data = await getInfo(url, [...opts, `--proxy=${PROXY_URL}`])
} catch (err) {}
}
onError(err, url)
}

return data
Expand Down
6 changes: 3 additions & 3 deletions packages/metascraper-media-provider/src/get-media/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
'use strict'

const { protocol } = require('@metascraper/helpers')
const { get, reduce, set } = require('lodash')
const memoizeOne = require('memoize-one')

const createTwitterInfo = require('./twitter-info')
const { createTwitterInfo, isTwitterUrl } = require('./twitter-info')
const { protocol } = require('@metascraper/helpers')
const createGetMedia = require('./get-media')

module.exports = opts => {
const getMedia = createGetMedia(opts)
const { isTwitterUrl, getTwitterInfo } = createTwitterInfo(opts)
const getTwitterInfo = createTwitterInfo(opts)

const getInfo = async url => {
if (!isTwitterUrl(url)) return getMedia(url)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,15 @@ const getTwitterInfo = ({ getToken }) => async url => {
}
}

module.exports = opts => {
const createTwitterInfo = opts => {
const getToken = memoizeToken(getGuestToken, {
max: API_GUEST_ACTIVATE_LIMIT,
expire: API_GUEST_ACTIVATE_EXPIRE,
key: 'media:twitter',
...opts
})

return {
getTwitterInfo: getTwitterInfo({ getToken }),
isTwitterUrl
}
return getTwitterInfo({ getToken })
}

module.exports = { createTwitterInfo, isTwitterUrl }

0 comments on commit dcf9e23

Please sign in to comment.