UNPKG

twdl

Version:

Downloads image and video tweets from Twitter

45 lines 1.79 kB
import { CookieJar, MemoryCookieStore } from 'tough-cookie'; import { getUsername } from '../util.js'; import { gotInstance, loadCheerio } from '../api.js'; function getIdSecond(username) { function getResponse(response) { const body = response.body; return body.includes('error') ? undefined : body; } return gotInstance.post('https://tweeterid.com/ajax.php', { body: `input=${username}` }).then(getResponse); } export function getId(tweetData) { function getCsrfToken() { function parseToken(jq) { const csrfToken = jq('meta[name="csrf-token"]'); return csrfToken.length > 0 ? csrfToken.attr('content') : undefined; } return gotInstance.get('https://www.codeofaninja.com/tools/find-twitter-id', { cookieJar: cookieJar }) .then(loadCheerio) .then(parseToken); } const username = getUsername(tweetData.finalUrl); const store = new MemoryCookieStore(); const cookieJar = new CookieJar(store, { looseMode: true }); function getIdWithToken(csrfToken) { return gotInstance.post('https://www.codeofaninja.com/tools/find-twitter-id-answer', { form: { _token: csrfToken, username: username }, cookieJar: cookieJar, }); } function parseHtml(jq) { const match = jq('div').text().match(/Twitter Numeric ID: ([0-9]+)/); if (match) { return Promise.resolve(match[1]); } return getIdSecond(username); } function trySecondSite(err) { return getIdSecond(username); } return getCsrfToken() .then(getIdWithToken) .then(loadCheerio) .then(parseHtml, trySecondSite); } //# sourceMappingURL=id.js.map