From f181545f1456e3c43bb5cb1b25dfd1d3cfcccdc1 Mon Sep 17 00:00:00 2001 From: b3yond Date: Sat, 17 Jun 2017 18:25:17 +0200 Subject: [PATCH 1/2] put in more escapes. har har har. --- .gitignore | 2 ++ twitter/main.py | 74 ++++++++++++++++++++++++++++++++++--------------- 2 files changed, 53 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index 8e3698c..f426324 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ *.swp +.idea/ +last_rt ticketfrei.cfg seen_toots.pickle seen_toots.pickle.part diff --git a/twitter/main.py b/twitter/main.py index bddb34d..b9d2a81 100644 --- a/twitter/main.py +++ b/twitter/main.py @@ -1,7 +1,9 @@ #!/usr/bin/env python + __author__ = "b3yond" import twitter +import requests from time import sleep """ @@ -9,7 +11,7 @@ How to get these keys is described in doc/twitter_api.md After you received keys, store them in ../../api_keys, one at a line. """ -with open("../../api_keys", "r") as file: +with open("../appkeys/ticketfrei@twitter.com", "r") as file: keys = file.readlines() for i in keys: print i, @@ -23,32 +25,58 @@ api = twitter.Api(consumer_key = keys[0].strip(), # This counter is needed to keep track which was the last tweet you retweeted -last_rt = "" +with open("../last_rt", "r+") as file: + last_rt = file.read() # Words which have to be included into the tweets for the tweet to get retweeted -with open("../triggerwords.txt", "r") as file: +with open("../triggerwords", "r") as file: triggers = file.readlines() +try: + while 1: + sleep(1) -while 1: - sleep(1) - - # Store all mentions in a list of Status Objects - mentions = api.GetMentions(since_id=last_rt) - print mentions - for i in mentions: - print i.user.name, i.user.id, i.text # debug - - # Is the Text of the Tweet in the triggerlist? - for j in triggers: - if i.text.lower().find(j): - - # Retweet status, save the id so it doesn't get crawled again + # Store all mentions in a list of Status Objects + try: + mentions = api.GetMentions(since_id=last_rt) + except requests.exceptions.ConnectionError: + done = False + while done == False: try: - feedback = api.PostRetweet(i.id) - print feedback - except twitter.error.TwitterError: - print("[ERROR] probably you already retweeted this tweet.") - last_rt = i.id - break \ No newline at end of file + mentions = api.GetMentions(since_id=last_rt) + done = True + except: + sleep(10) + + print mentions + for i in mentions: + print i.user.name, i.user.id, i.text # debug + + # Is the Text of the Tweet in the triggerlist? + for j in triggers: + if i.text.lower().find(j): + + # Retweet status + try: + api.PostRetweet(i.id) + + # This is an Error we need to get rid of. Why are tweets RTed twice? + except twitter.error.TwitterError: + print("[ERROR] probably you already retweeted this tweet.") + except requests.exceptions.ConnectionError: + done = False + while done == False: + try: + api.PostRetweet(i.id) + done = True + except: + sleep(10) + + # save the id so it doesn't get crawled again + last_rt = i.id + break +except: + api.PostDirectMessage("Help! I broke down. restart me pls :$", "801098086005243904", "links_tech") + with open("../last_rt", "w") as file: + file.write(last_rt) \ No newline at end of file From eb811bb1483e965e0395b369ff1272879af95ecc Mon Sep 17 00:00:00 2001 From: b3yond Date: Sat, 17 Jun 2017 19:04:51 +0200 Subject: [PATCH 2/2] finished the standalone twitter bot! --- README.md | 8 +++--- triggerwords | 24 +++++++++++++++++ twitter/main.py | 71 ++++++++++++++++++++++++++----------------------- 3 files changed, 65 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index e4d57a6..8011ed0 100644 --- a/README.md +++ b/README.md @@ -35,15 +35,15 @@ $ pip3 install Mastodon.py pytoml - [x] How to crawl mentions with the mastodon API - [x] How to boost people with the mastodon API - [x] How to access the twitter API -- [ ] How to crawl mentions with the twitter API -- [ ] How to retweet people with the twitter API +- [x] How to crawl mentions with the twitter API +- [x] How to retweet people with the twitter API ## to do -- [ ] Twitter: Crawl mentions +- [x] Twitter: Crawl mentions - [ ] Mastodon: Crawl mentions - [ ] Write toots/tweets to database -- [ ] Twitter: retweet people +- [x] Twitter: retweet people - [ ] Mastodon: boost people - [ ] Mastodon: toot who has been retweeted on twitter - [ ] Twitter: tweet who has been boosted on mastodon diff --git a/triggerwords b/triggerwords index e69de29..1463537 100644 --- a/triggerwords +++ b/triggerwords @@ -0,0 +1,24 @@ +kontrolle +ticketfrei +konti +db +zivil +sicherheit +uniform +station +bus +bahn +tram +linie +nuernberg +nbg +nürnberg +s1 +s2 +s3 +u1 +u2 +u3 +s4 +u21 +u11 diff --git a/twitter/main.py b/twitter/main.py index b9d2a81..7b87b70 100644 --- a/twitter/main.py +++ b/twitter/main.py @@ -13,8 +13,8 @@ After you received keys, store them in ../../api_keys, one at a line. """ with open("../appkeys/ticketfrei@twitter.com", "r") as file: keys = file.readlines() - for i in keys: - print i, + for status in keys: + print status, # create an Api object @@ -25,58 +25,61 @@ api = twitter.Api(consumer_key = keys[0].strip(), # This counter is needed to keep track which was the last tweet you retweeted +# ACTUALLY it keeps track of the last mention, whether you retweeted it or not. with open("../last_rt", "r+") as file: last_rt = file.read() # Words which have to be included into the tweets for the tweet to get retweeted with open("../triggerwords", "r") as file: - triggers = file.readlines() + triggers = [s.strip() for s in file.readlines()] try: while 1: sleep(1) # Store all mentions in a list of Status Objects - try: - mentions = api.GetMentions(since_id=last_rt) - except requests.exceptions.ConnectionError: - done = False - while done == False: - try: - mentions = api.GetMentions(since_id=last_rt) - done = True - except: - sleep(10) + done = False + while not done: + try: + mentions = api.GetMentions(since_id=last_rt) + done = True + except requests.exceptions.ConnectionError: + print("[ERROR] Bad Connection.") + sleep(10) - print mentions - for i in mentions: - print i.user.name, i.user.id, i.text # debug + print mentions # debug + for status in mentions: # Is the Text of the Tweet in the triggerlist? - for j in triggers: - if i.text.lower().find(j): + should_retweet = False + for triggerword in triggers: + if status.text.lower().find(triggerword): + should_retweet = True + break - # Retweet status + # Retweet status + if should_retweet: + done = False + while not done: try: - api.PostRetweet(i.id) + api.PostRetweet(status.id) + done = True # This is an Error we need to get rid of. Why are tweets RTed twice? - except twitter.error.TwitterError: - print("[ERROR] probably you already retweeted this tweet.") +# except twitter.error.TwitterError: +# print("[ERROR] probably you already retweeted this tweet.") +# done = True except requests.exceptions.ConnectionError: - done = False - while done == False: - try: - api.PostRetweet(i.id) - done = True - except: - sleep(10) + print("[ERROR] Bad Connection.") + sleep(10) + + # save the id so it doesn't get crawled again + last_rt = status.id + print last_rt - # save the id so it doesn't get crawled again - last_rt = i.id - break except: - api.PostDirectMessage("Help! I broke down. restart me pls :$", "801098086005243904", "links_tech") + print "[ERROR] Shit went wrong, closing down." with open("../last_rt", "w") as file: - file.write(last_rt) \ No newline at end of file + file.write(str(last_rt)) + api.PostDirectMessage("Help! I broke down. restart me pls :$", "801098086005243904", "links_tech") \ No newline at end of file