Skip to content

Commit

Permalink
Move memoize token into an external dependency (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats authored Sep 19, 2018
1 parent 32fc3dd commit 96b3bcc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 30 deletions.
1 change: 1 addition & 0 deletions packages/metascraper-media-provider/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"got": "~9.2.0",
"lodash": "~4.17.10",
"memoize-one": "~4.0.2",
"memoize-token": "~1.0.0",
"youtube-dl": "~1.12.2"
},
"devDependencies": {
Expand Down
3 changes: 2 additions & 1 deletion packages/metascraper-media-provider/src/get-media/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ const { protocol } = require('@metascraper/helpers')
const { isEmpty, reduce } = require('lodash')
const memoizeOne = require('memoize-one')

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

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

const getInfo = async url => {
if (!isTwitterUrl(url)) return getMedia(url)
Expand Down
55 changes: 26 additions & 29 deletions packages/metascraper-media-provider/src/get-media/twitter-info.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const { chain } = require('lodash')
const assert = require('assert')
const memoizeToken = require('memoize-token')
const { get, chain } = require('lodash')
const { URL } = require('url')
const got = require('got')

Expand All @@ -23,40 +23,26 @@ const getTweetId = url => url.split('/').reverse()[0]

const MAX_API_CALLS_GUEST_ACTIVATE = 180

const memoize = (fn, { max } = {}) => {
assert(max, 'max required')
let count
let value
return async (...args) => {
if (++count < max) return value
value = await fn(...args)
count = 0
return value
}
const getGuestToken = async url => {
const { body } = await got.post(
'https://api.twitter.com/1.1/guest/activate.json',
{
headers: { Authorization: TWITTER_BEARER_TOKEN, Referer: url },
json: true
}
)
return get(body, 'guest_token')
}

const getGuestToken = memoize(
async url => {
const { body } = await got.post(
'https://api.twitter.com/1.1/guest/activate.json',
{
headers: { Authorization: TWITTER_BEARER_TOKEN, Referer: url },
json: true
}
)
return body.guest_token
},
{ max: MAX_API_CALLS_GUEST_ACTIVATE }
)

const getTwitterInfo = async url => {
const getTwitterInfo = ({ getToken }) => async url => {
const tweetId = getTweetId(url)
const apiUrl = `https://api.twitter.com/2/timeline/conversation/${tweetId}.json?tweet_mode=extended`
const guestToken = await getToken(url)
const { body } = await got(apiUrl, {
json: true,
headers: {
authorization: TWITTER_BEARER_TOKEN,
'x-guest-token': await getGuestToken(url)
'x-guest-token': guestToken
}
})

Expand All @@ -69,4 +55,15 @@ const getTwitterInfo = async url => {
.value()
}

module.exports = { getTwitterInfo, isTwitterUrl }
module.exports = opts => {
const getToken = memoizeToken(getGuestToken, {
max: MAX_API_CALLS_GUEST_ACTIVATE,
key: 'twitter',
...opts
})

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

0 comments on commit 96b3bcc

Please sign in to comment.