Created
October 27, 2022 17:36
-
-
Save simonlindgren/f16a752f0a2f84743aecec483bc7db13 to your computer and use it in GitHub Desktop.
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
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"id": "51315249-cd83-4fad-87fc-e119f24db665", | |
"metadata": {}, | |
"source": [ | |
"### r — tweetbotornot2" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "6f36b5b6-62bf-418c-86c8-ba531ba53b16", | |
"metadata": { | |
"tags": [] | |
}, | |
"source": [ | |
"#### Installation and authentication" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "1ec1da99-0338-4694-9050-dcaf54f45936", | |
"metadata": { | |
"tags": [] | |
}, | |
"source": [ | |
"Install `tweetbotornot2` by running this in the R console:\n", | |
"\n", | |
"```\n", | |
"> if (!\"remotes\" %in% installed.packages()) {install.packages(\"remotes\")}\n", | |
"> remotes::install_github(\"mkearney/tweetbotornot2\")\n", | |
"```\n", | |
"We must also downgrade `rtweet` to 0.7 by running this in the R console:\n", | |
"\n", | |
"```\n", | |
"> remove.packages(\"rtweet\")\n", | |
"\n", | |
"> download.file(\n", | |
" \"https://cran.r-project.org/src/contrib/Archive/rtweet/rtweet_0.7.0.tar.gz\",\n", | |
" \"rtweet_0.7.0.tar.gz\"\n", | |
")\n", | |
"\n", | |
"> install.packages(\n", | |
" \"rtweet_0.7.0.tar.gz\", repos=NULL\n", | |
")\n", | |
"\n", | |
"> file.remove(\"rtweet_0.7.0.tar.gz\")\n", | |
"```\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "c97ad7bb-049a-4346-b1ca-d7b867296460", | |
"metadata": {}, | |
"source": [ | |
"Import tbn2 and check the `rtweet` version." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "3a468ab2-62a9-47ed-ae14-c100810d075f", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"library(tweetbotornot2)\n", | |
"library(rtweet)\n", | |
"packageVersion('rtweet')" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "b9a8947a-2b9a-4a36-9b07-dbb9cad48c61", | |
"metadata": {}, | |
"source": [ | |
"Authenticate with the twitter API. \n", | |
"\n", | |
"**Note** *The code in the cell below must be run on local system, i.e. not in cloud, and not within a docker image. Then copy the \\*rds file to the directory of this notebook*." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "0f58f5f6-65f5-4c96-8146-bd6b05dab4cb", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"#library(tweetbotornot2)\n", | |
"#library(rtweet)\n", | |
"\n", | |
"## the names of the keys as on developer.twitter.com\n", | |
"#api_key = '5n5MjdRQCnuVufL5xvlM2sjUn'\n", | |
"#api_key_secret = '9ZV0uojX22Z9EjW3JrrSVu93WrmeyJWNGosH8AKCfwe1QXNgDC'\n", | |
"\n", | |
"#app_name <- \"tweepy_filter\"\n", | |
"#consumer_key <- api_key\n", | |
"#consumer_secret <- api_key_secret\n", | |
"\n", | |
"#token <- create_token(app_name, consumer_key, consumer_secret)\n", | |
"\n", | |
"#saveRDS(token, 'twitter_token.rds')" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "4d3fd1de-340b-4c37-b950-15f8351b7769", | |
"metadata": {}, | |
"source": [ | |
"With *rds file now available, we can authenticate." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "90bd632e-7ef3-4ad5-9c5b-33c743fd995a", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Create env variable TWITTER_PAT (with path to saved token).\n", | |
"env_var <- paste0(\"TWITTER_PAT=\", 'twitter_token.rds')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "fb14192b-f471-4cd6-a3dc-ac499fc07693", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"## save as Renviron file (or append if the file already exists)\n", | |
"cat(env_var, file = file.path(path.expand(\"~\"), \".Renviron\"), fill = TRUE, append = TRUE)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "0fd176ba-cb71-4643-9721-171da87e1874", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"## Refresh .Renviron variables.\n", | |
"readRenviron(\"~/.Renviron\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "0aa92731-c696-4ad5-ba28-0d85524a7af5", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"## test API connection\n", | |
"fds <- get_friends(\"jack\", 4)\n", | |
"fds" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "510314b0-7a35-4cdc-bf23-f45b1f935de2", | |
"metadata": { | |
"tags": [] | |
}, | |
"source": [ | |
"#### Bot scores" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "e57ba145-fd93-4007-b3a9-53fcc47d7855", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"library(tweetbotornot2)\n", | |
"library(rtweet)\n", | |
"packageVersion('rtweet')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "3929dbe0-c223-40d9-ac3b-208eafabf930", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [], | |
"source": [ | |
"# Test bot detection \n", | |
"predict_bot('2941665345')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "86b4609d-bb76-47f8-b8a7-48dff35271f4", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Get user ids to be checked as vector\n", | |
"users <- readLines('checkz.txt')" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "fc8d56ec-fd8f-43aa-81cb-ab9c82c8bb61", | |
"metadata": { | |
"tags": [] | |
}, | |
"source": [ | |
"##### METHOD 1: Append to data table and write csv when all done" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "bfe099f5-5b8e-4d48-9ecd-36690c579c07", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [], | |
"source": [ | |
"library(data.table)\n", | |
"data <- data.table(va=character(), vb=character(), vc=double())\n", | |
"count = 0\n", | |
"\n", | |
"for (u in users)\n", | |
"{\n", | |
" skip_to_next <- FALSE\n", | |
" tryCatch(result <- predict_bot(u), error = function(e) { skip_to_next <<- TRUE})\n", | |
" if(skip_to_next) { next } \n", | |
" data <- rbindlist(list(data, result))\n", | |
" count <- count+1\n", | |
" print(count)\n", | |
"\n", | |
"}\n", | |
"\n", | |
"fwrite(data, \"data.csv\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "8405a231-a135-436e-86d0-ebdcd591b2ca", | |
"metadata": { | |
"tags": [] | |
}, | |
"source": [ | |
"##### METHOD 2: Save individual csv file per user" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "329d3eda-d406-4cc9-aec5-ccd550a36ac9", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [], | |
"source": [ | |
"if (!dir.exists('scores')) {dir.create('scores')}\n", | |
"\n", | |
"for (u in users)\n", | |
"{\n", | |
" skip_to_next <- FALSE\n", | |
" tryCatch(result <- predict_bot(u), error = function(e) { skip_to_next <<- TRUE})\n", | |
" if(skip_to_next) { next } \n", | |
" write.csv(result, paste(\"scores/\",u,\".csv\", sep=\"\"), row.names = FALSE)\n", | |
"\n", | |
"}" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "R", | |
"language": "R", | |
"name": "ir" | |
}, | |
"language_info": { | |
"codemirror_mode": "r", | |
"file_extension": ".r", | |
"mimetype": "text/x-r-source", | |
"name": "R", | |
"pygments_lexer": "r", | |
"version": "4.1.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Glad to hear!
On 28 Oct 2022 at 17:16 +0200, Dennis ***@***.***>, wrote:
@dc1023 commented on this gist.
…________________________________
Thank you so much for the code! It does not work at the first time, but after I restart R it worked!
—
Reply to this email directly, view it on GitHub<https://gist.github.com/f16a752f0a2f84743aecec483bc7db13#gistcomment-4351183> or unsubscribe<https://github.com/notifications/unsubscribe-auth/ADK7MICF64RYV6QNOWZGCT3WFPU3LBFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFQKSXMYLMOVS2I5DSOVS2I3TBNVS3W5DIOJSWCZC7OBQXE5DJMNUXAYLOORPWCY3UNF3GS5DZVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVEYTCOJQGY2TINZVU52HE2LHM5SXFJTDOJSWC5DF>.
You are receiving this email because you authored a thread.
Triage notifications on the go with GitHub Mobile for iOS<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675> or Android<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you so much for the code! It does not work at the first time, but after I restart R it worked!