Skip to content

Commit

Permalink
Exit early if no twitter videos detected
Browse files Browse the repository at this point in the history
This change make possible extract gif from tweets as mp4 video
  • Loading branch information
Kikobeats committed Sep 5, 2018
1 parent 25cec6f commit 17ce77a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
18 changes: 11 additions & 7 deletions packages/metascraper-media-provider/src/get-media/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict'

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

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

const { protocol } = require('@metascraper/helpers')
const { chain } = require('lodash')

// Local cache for successive calls
let cachedVideoInfoUrl
let cachedVideoInfo
Expand All @@ -21,8 +21,11 @@ module.exports = opts => {
getTwitterInfo(url)
])

const formats = chain(videoInfo.formats)
.reduce((acc, format, index) => {
if (isEmpty(twitterVideos)) return videoInfo

const formats = reduce(
videoInfo.formats,
(acc, format, index) => {
const { url } = twitterVideos[index]
const item = {
...format,
Expand All @@ -31,8 +34,9 @@ module.exports = opts => {
extractor_key: 'Twitter'
}
return [...acc, item]
}, [])
.value()
},
[]
)

return { ...videoInfo, formats }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const getTwitterInfo = async url => {
.get(
`globalObjects.tweets.${tweetId}.extended_entities.media.0.video_info.variants`
)
.filter('bitrate')
.orderBy('bitrate', 'asc')
.value()
}
Expand Down
5 changes: 3 additions & 2 deletions packages/metascraper-media-provider/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,11 @@ describe('metascraper-media-provider', () => {
})
})

describe.only('twitter', () => {
describe('twitter', () => {
;[
'https://twitter.com/verge/status/957383241714970624',
'https://twitter.com/telediario_tve/status/1036860275859775488'
'https://twitter.com/telediario_tve/status/1036860275859775488',
'https://twitter.com/Mei_Gui8/status/1037374230785142785'
].forEach(url => {
it(url, async () => {
const metadata = await metascraper({ html: '<title></title>', url })
Expand Down

0 comments on commit 17ce77a

Please sign in to comment.