-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from JorgenEvens/bug/twitter
[network][twitter] Fix twitter redirect
- Loading branch information
Showing
1 changed file
with
16 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,28 @@ | ||
const fetch = require('lib/fetch'); | ||
const cheerio = require('cheerio'); | ||
|
||
const twitterRegexp = /^https?:\/\/(www\.)?twitter\.com\/[^\/]+\/profile_image.*/; | ||
|
||
async function followRedirect(username, { req }) { | ||
async function profileScrape(username) { | ||
username = encodeURIComponent(username); | ||
const size = encodeURIComponent(req.query.size || 'original'); | ||
|
||
let url = `https://twitter.com/${username}/profile_image?size=${size}`; | ||
const res = await fetch(`https://www.twitter.com/${username}/`); | ||
const html = await res.text(); | ||
const doc = cheerio.load(html); | ||
|
||
while (url && twitterRegexp.test(url)) { | ||
const res = await fetch(url, { | ||
headers: { | ||
'user-agent': req.get('user-agent') | ||
}, | ||
redirect: 'manual' | ||
}); | ||
const profilePicture = doc('a.profile-picture'); | ||
const image = profilePicture.attr('data-resolved-url-large') || | ||
profilePicture.attr('data-url') || | ||
profilePicture.attr('href'); | ||
|
||
if (![302, 301].includes(res.status)) | ||
return null; | ||
if (!image || typeof image != 'string') | ||
return null; | ||
|
||
url = res.headers.get('location'); | ||
} | ||
// Strip size parameter | ||
// Ex: https://pbs.twimg.com/profile_images/1184774364702617600/2DmGoqCz_400x400.jpg | ||
const original = image.replace(/_([^.\/]+)\.([a-z]+)$/i, '.$2'); | ||
|
||
return url; | ||
return original; | ||
} | ||
|
||
module.exports = [ | ||
followRedirect | ||
profileScrape | ||
]; |