Skip to content

Instantly share code, notes, and snippets.

@ahmadawais
Created June 21, 2020 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmadawais/2ad10c1fcd20c183af119787b79d73d7 to your computer and use it in GitHub Desktop.
Save ahmadawais/2ad10c1fcd20c183af119787b79d73d7 to your computer and use it in GitHub Desktop.
Scrape a twitter image with JavaScript.
const cheerio = require('cheerio')
const got = require('got')
// https://dev.twitter.com/basics/user-profile-images-and-banners
const REGEX_IMG_MODIFIERS = /_(?:bigger|mini|normal)\./
const ORIGINAL_IMG_SIZE = '_400x400'
const getAvatarUrl = url =>
url.replace(REGEX_IMG_MODIFIERS, `${ORIGINAL_IMG_SIZE}.`)
module.exports = async username => {
const { body } = await got(`https://mobile.twitter.com/${username}`)
const $ = cheerio.load(body)
const el = $('.avatar img').attr('src')
return getAvatarUrl(el)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment