Last active
December 27, 2019 15:41
-
-
Save ogawa0071/9d89d9088055f93d5cd53e28ebc38c0a to your computer and use it in GitHub Desktop.
import-firebase-auth-from-twitter
This file contains hidden or 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
| import fs from "fs"; | |
| import request from "request-promise"; | |
| const consumerKey = ""; | |
| const consumerSecret = ""; | |
| const bearerTokenCredentials = Buffer.from(`${consumerKey}:${consumerSecret}`); | |
| const base64EncodedBearerTokenCredentials = bearerTokenCredentials.toString( | |
| "base64" | |
| ); | |
| (async () => { | |
| const res = await request.post("https://api.twitter.com/oauth2/token", { | |
| form: { | |
| grant_type: "client_credentials" | |
| }, | |
| headers: { | |
| Authorization: `Basic ${base64EncodedBearerTokenCredentials}`, | |
| "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8" | |
| } | |
| }); | |
| const accessToken = JSON.parse(res).access_token; | |
| const results = await api(accessToken); | |
| await fs.promises.appendFile( | |
| "./followers.json", | |
| `${JSON.stringify(results, null, 2)}` | |
| ); | |
| })(); | |
| async function api(accessToken: string, next?: string): Promise<Array<{}>> { | |
| console.log(`Start ${next}`); | |
| const res = JSON.parse( | |
| await request.get(`https://api.twitter.com/1.1/followers/list.json`, { | |
| qs: { | |
| screen_name: `OOParts_JP`, | |
| cursor: next, | |
| count: "200" | |
| }, | |
| headers: { | |
| Authorization: `Bearer ${accessToken}` | |
| } | |
| }) | |
| ); | |
| const users = res.users; | |
| if (res.next_cursor_str !== "0") { | |
| const users_ = await api(accessToken, res.next_cursor_str); | |
| return users.concat(users_); | |
| } | |
| return users; | |
| } | |
| function sleep(time: number) { | |
| return new Promise(resolve => { | |
| setTimeout(() => { | |
| resolve(); | |
| }, time); | |
| }); | |
| } |
This file contains hidden or 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
| { | |
| "dependencies": { | |
| "request": "^2.88.0", | |
| "request-promise": "^4.2.5" | |
| }, | |
| "devDependencies": { | |
| "@types/request": "^2.48.3", | |
| "@types/request-promise": "^4.1.45" | |
| } | |
| } |
This file contains hidden or 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
| import fs from "fs"; | |
| import request from "request-promise"; | |
| const consumerKey = ""; | |
| const consumerSecret = ""; | |
| const bearerTokenCredentials = Buffer.from(`${consumerKey}:${consumerSecret}`); | |
| const base64EncodedBearerTokenCredentials = bearerTokenCredentials.toString( | |
| "base64" | |
| ); | |
| (async () => { | |
| const res = await request.post("https://api.twitter.com/oauth2/token", { | |
| form: { | |
| grant_type: "client_credentials" | |
| }, | |
| headers: { | |
| Authorization: `Basic ${base64EncodedBearerTokenCredentials}`, | |
| "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8" | |
| } | |
| }); | |
| const accessToken = JSON.parse(res).access_token; | |
| const results = await api(accessToken); | |
| await fs.promises.appendFile( | |
| "./search.json", | |
| `${JSON.stringify(results, null, 2)}` | |
| ); | |
| })(); | |
| async function api(accessToken: string, next?: string): Promise<Array<{}>> { | |
| console.log(`Start ${next}`); | |
| const res = await request.post( | |
| `https://api.twitter.com/1.1/tweets/search/fullarchive/ogawa0071.json`, | |
| { | |
| body: { | |
| query: `"RT @OOParts_JP: 📣ベータ版 先行体験ユーザー募集!"`, | |
| next, | |
| maxResults: "500", | |
| fromDate: "201908300200", | |
| toDate: "201909011500" | |
| }, | |
| headers: { | |
| Authorization: `Bearer ${accessToken}` | |
| }, | |
| json: true | |
| } | |
| ); | |
| const results = res.results; | |
| if (res.next) { | |
| await sleep(1000); | |
| const results_ = await api(accessToken, res.next); | |
| return results.concat(results_); | |
| } | |
| return results; | |
| } | |
| function sleep(time: number) { | |
| return new Promise(resolve => { | |
| setTimeout(() => { | |
| resolve(); | |
| }, time); | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment