Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix publisher #117

Merged
merged 1 commit into from
Sep 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/metascraper-media-provider/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@metascraper/helpers": "^4.3.0",
"got": "~9.2.0",
"lodash": "~4.17.10",
"memoize-one": "~4.0.2",
"youtube-dl": "~1.12.2"
},
"devDependencies": {
Expand Down
31 changes: 7 additions & 24 deletions packages/metascraper-media-provider/src/get-media/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@

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

const { isTwitterUrl, getTwitterInfo } = require('./twitter-info')
const createGetMedia = require('./get-media')

// Local cache for successive calls
let cachedVideoInfoUrl
let cachedVideoInfo

module.exports = opts => {
const getMedia = createGetMedia(opts)

Expand All @@ -21,36 +18,22 @@ module.exports = opts => {
getTwitterInfo(url)
])

if (isEmpty(twitterVideos)) return videoInfo
const twitterVideo = { ...videoInfo, extractor_key: 'Twitter' }

if (isEmpty(twitterVideos)) return twitterVideo

const formats = reduce(
videoInfo.formats,
(acc, format, index) => {
const { url } = twitterVideos[index]
const item = {
...format,
url,
protocol: protocol(url),
extractor_key: 'Twitter'
}
const item = { ...format, url, protocol: protocol(url) }
return [...acc, item]
},
[]
)

return { ...videoInfo, formats }
return { ...twitterVideo, formats }
}

return async url => {
if (url === cachedVideoInfoUrl) return cachedVideoInfo
cachedVideoInfoUrl = url

try {
cachedVideoInfo = await getInfo(url)
} catch (err) {
cachedVideoInfo = {}
}

return cachedVideoInfo
}
return memoizeOne(getInfo)
}