Created
June 21, 2020 10:26
-
-
Save ahmadawais/2ad10c1fcd20c183af119787b79d73d7 to your computer and use it in GitHub Desktop.
Scrape a twitter image with JavaScript.
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
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