|
| 1 | +import * as neo4j from 'neo4j-driver' |
| 2 | +import needle from 'needle'; |
| 3 | +import credentials from './credentials/Twitter.js'; |
| 4 | + |
| 5 | +// Globals |
| 6 | +const TOKEN = credentials.auth_tokens.BEARER_TOKEN; |
| 7 | +const GET_ENDPOINT = "https://api.twitter.com/2/tweets"; |
| 8 | +const SEARCH_RECENT_ENDPOINT = "https://api.twitter.com/2/tweets/search/recent"; |
| 9 | + |
| 10 | +// https://twitter.com/briantylercohen/status/1369403905956847618 <- use this!!!! |
| 11 | +// ^ try this one |
| 12 | + |
| 13 | +//////////////////////////////////////////////////////////////////////////////// |
| 14 | +////////////////////////////////// MAIN ////////////////////////////////////// |
| 15 | +//////////////////////////////////////////////////////////////////////////////// |
| 16 | + |
| 17 | +// Startup database |
| 18 | +const driver = neo4j.driver('bolt://localhost:7687', neo4j.auth.basic('neo4j', "password")) |
| 19 | +const session = driver.session() |
| 20 | + |
| 21 | + |
| 22 | +const rootTweetId = "1372316893739163652"; |
| 23 | +const rootTweet = await addTweetThreadToDb(rootTweetId); |
| 24 | + |
| 25 | +// Close database |
| 26 | +await driver.close() |
| 27 | + |
| 28 | +//////////////////////////////////////////////////////////////////////////////// |
| 29 | +//////////////////////////// ENDPOINT FUNCTIONS /////////////////////////////// |
| 30 | +//////////////////////////////////////////////////////////////////////////////// |
| 31 | + |
| 32 | +async function get(endpointURL, params) { |
| 33 | + const res = await needle('get', endpointURL, params, { headers: { |
| 34 | + "authorization": `Bearer ${TOKEN}` |
| 35 | + }}) |
| 36 | + |
| 37 | + if (res.body) { |
| 38 | + const ret = { |
| 39 | + body: res.body, |
| 40 | + headers: res.headers |
| 41 | + } |
| 42 | + return ret; |
| 43 | + } else { |
| 44 | + throw new Error('Unsuccessful request') |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +async function getTweet(id) { |
| 49 | + if (id == null) throw new Error ("ID not specified in getTweets"); |
| 50 | + const params = { |
| 51 | + "ids": id, |
| 52 | + "tweet.fields": "author_id,conversation_id,created_at,entities,lang,referenced_tweets,text", |
| 53 | + } |
| 54 | + return await get(GET_ENDPOINT, params); |
| 55 | +} |
| 56 | + |
| 57 | +async function recentSearch(conversation_id, next_token) { |
| 58 | + if (conversation_id == null) throw new Error ("ID not specified in recentSearch"); |
| 59 | + const params = { |
| 60 | + "query":`conversation_id:${conversation_id}`, |
| 61 | + "max_results": 100, |
| 62 | + "tweet.fields": "author_id,conversation_id,created_at,entities,lang,referenced_tweets,text", |
| 63 | + } |
| 64 | + if(next_token) {params.next_token = next_token} |
| 65 | + |
| 66 | + return await get(SEARCH_RECENT_ENDPOINT, params); |
| 67 | +} |
| 68 | + |
| 69 | +//////////////////////////////////////////////////////////////////////////////// |
| 70 | +//////////////////////////// RETRIEVAL FUNCTIONS ////////////////////////////// |
| 71 | +//////////////////////////////////////////////////////////////////////////////// |
| 72 | + |
| 73 | +async function addTweetThreadToDb(id) { |
| 74 | + // Get first page |
| 75 | + const conversation_id = (await getTweet(id)).body.data[0].conversation_id; |
| 76 | + var page = await recentSearch(conversation_id); |
| 77 | + |
| 78 | + for(const tweet in page.body.data) { |
| 79 | + await addTweetToDatabase(page.body.data[tweet]); |
| 80 | + } |
| 81 | + |
| 82 | + // If more pages... |
| 83 | + while(page.body.meta.next_token) { |
| 84 | + var page = await recentSearch(id, page.body.meta.next_token); |
| 85 | + |
| 86 | + for(const tweet in page.body.data) { |
| 87 | + await addTweetToDatabase(page.body.data[tweet]); |
| 88 | + } |
| 89 | + |
| 90 | + if(page.headers["x-rate-limit-remaining"] <= 0) { |
| 91 | + console.log("I'm rate limited... Pausing execution for " + page.headers["x-rate-limit-reset"] + " seconds.") |
| 92 | + await sleep(page.headers["x-rate-limit-remaining"]*1000) |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +function sleep(ms) { |
| 98 | + return new Promise(resolve => setTimeout(resolve, ms)); |
| 99 | +} |
| 100 | + |
| 101 | +//////////////////////////////////////////////////////////////////////////////// |
| 102 | +//////////////////////////// DATABASE FUNCTIONS /////////////////////////////// |
| 103 | +//////////////////////////////////////////////////////////////////////////////// |
| 104 | +async function addTweetToDatabase(tweet) { |
| 105 | + var query = "MERGE (author:Person {author_id:$author_id}) MERGE (tweet:Tweet {id:$id, text:$text, lang:$lang, created_at:$created_at}) MERGE (conversation:Conversation {id:$conv_id}) MERGE (author) - [:Authored] -> (tweet) MERGE (tweet) - [:In_Conversation] -> (conversation)"; |
| 106 | + |
| 107 | + try { |
| 108 | + const result = await session |
| 109 | + .run(query, { |
| 110 | + author_id: neo4j.int(tweet.author_id), |
| 111 | + id: neo4j.int(tweet.id), |
| 112 | + text: tweet.text, |
| 113 | + conv_id: neo4j.int(tweet.conversation_id), |
| 114 | + lang: tweet.lang, |
| 115 | + created_at: tweet.created_at, |
| 116 | + text: tweet.text, |
| 117 | + }); |
| 118 | + } catch (err) { |
| 119 | + console.log("There was an error in addTweetToDatabase") |
| 120 | + console.log("I was trying to add the following tweet:") |
| 121 | + console.log(tweet) |
| 122 | + console.log(err) |
| 123 | + } |
| 124 | + |
| 125 | + for(const referenced in tweet.referenced_tweets) { |
| 126 | + try { |
| 127 | + const result = await session.run("MERGE (tweet:Tweet {id:$id}) MERGE (ref:Tweet {id:$rid}) MERGE (tweet) - [:References {type:$refType}] -> (ref)", { |
| 128 | + id: neo4j.int(tweet.id), |
| 129 | + rid: neo4j.int(tweet.referenced_tweets[referenced].id), |
| 130 | + refType: tweet.referenced_tweets[referenced].type |
| 131 | + } |
| 132 | + ) |
| 133 | + } catch(err) { |
| 134 | + console.log("There was an error while adding a referenced tweet") |
| 135 | + console.log(err) |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments