From 5b01b71257c2a0541ce4bc1d3f5671d9b51dac32 Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Thu, 19 Mar 2015 01:28:56 +0100 Subject: [PATCH 1/5] Check in current running version of Transbot --- transbot.py | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 transbot.py diff --git a/transbot.py b/transbot.py new file mode 100644 index 0000000..8bb4890 --- /dev/null +++ b/transbot.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import HTMLParser, tweepy, os, translate + +# [target_language,cons_key,cons_secret,access_token,access_secret] + +accounts = [ +] + +search = "from:kumitterer" + +last_id_filename = "last_id" +rt_bot_path = os.path.dirname(os.path.abspath(__file__)) +last_id_file = os.path.join(rt_bot_path, last_id_filename) + +auth = tweepy.OAuthHandler(accounts[0][1], accounts[0][2]) +auth.set_access_token(accounts[0][3], accounts[0][4]) +api = tweepy.API(auth) + +try: + with open(last_id_file, "r") as file: + savepoint = file.read() +except IOError: + savepoint = "" + print "No savepoint found. Trying to get as many results as possible." + +timelineIterator = tweepy.Cursor(api.search, q=search, since_id=savepoint).items() + +timeline = [] + +for status in timelineIterator: + timeline.append(status) + +timeline.reverse() + +tw_counter = 0 +er_counter = 0 + +for status in timeline: + print "(%(date)s) %(name)s: %(message)s\n" % \ + { "date" : status.created_at, + "name" : status.author.screen_name.encode('utf-8'), + "message" : status.text.encode('utf-8') } + + text = HTMLParser.HTMLParser().unescape(status.text).encode('utf-8') + + if text[0] == "@": + continue + + for a in accounts: + auth = tweepy.OAuthHandler(a[1], a[2]) + auth.set_access_token(a[3], a[4]) + api = tweepy.API(auth) + try: + api.update_status(translate.Translator(to_lang=a[0]).translate(text).encode('utf-8')[:140]) + tw_counter += 1 + except tweepy.error.TweepError, e: + print e + er_counter += 1 + continue + + with open(last_id_file, "w") as file: + file.write(str(status.id)) + +print "Finished. %d Tweets retweeted. %d errors occurred." % (tw_counter, er_counter) From 9afd088394d1b7959e4ab7009b89e41a62e77443 Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Thu, 19 Mar 2015 01:31:18 +0100 Subject: [PATCH 2/5] Remove my Twitter handle from the script. ^^ --- transbot.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/transbot.py b/transbot.py index 8bb4890..7bdea23 100644 --- a/transbot.py +++ b/transbot.py @@ -8,7 +8,9 @@ import HTMLParser, tweepy, os, translate accounts = [ ] -search = "from:kumitterer" +origin = "Handle of original account (without @)" + +search = "from:" + origin last_id_filename = "last_id" rt_bot_path = os.path.dirname(os.path.abspath(__file__)) From d01c6581dc798d8000ad5c1ce3c43328975dc2be Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Thu, 19 Mar 2015 01:57:48 +0100 Subject: [PATCH 3/5] Allow Transbot to handle multi-sentence tweets. --- transbot.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/transbot.py b/transbot.py index 7bdea23..de3fea8 100644 --- a/transbot.py +++ b/transbot.py @@ -54,8 +54,21 @@ for status in timeline: auth = tweepy.OAuthHandler(a[1], a[2]) auth.set_access_token(a[3], a[4]) api = tweepy.API(auth) + + tstring = "" + curstr = "" + + for word in text.split(" "): + curstr += word + if word[-1] in "!?.": + tstring += translate.Translator(to_lang=a[0]).translate(curstr)) + " " + curstr = "" + + if curstr != "": + tstring += translate.Translator(to_lang=a[0]).translate(curstr)) + try: - api.update_status(translate.Translator(to_lang=a[0]).translate(text).encode('utf-8')[:140]) + api.update_status(tstring.encode('utf-8')[:140]) tw_counter += 1 except tweepy.error.TweepError, e: print e From c643308ec71c3f8f6429d32b06a8386ad273c223 Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Thu, 19 Mar 2015 02:00:10 +0100 Subject: [PATCH 4/5] Fix typo --- transbot.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/transbot.py b/transbot.py index de3fea8..29d0ebf 100644 --- a/transbot.py +++ b/transbot.py @@ -61,11 +61,11 @@ for status in timeline: for word in text.split(" "): curstr += word if word[-1] in "!?.": - tstring += translate.Translator(to_lang=a[0]).translate(curstr)) + " " + tstring += translate.Translator(to_lang=a[0]).translate(curstr) + " " curstr = "" if curstr != "": - tstring += translate.Translator(to_lang=a[0]).translate(curstr)) + tstring += translate.Translator(to_lang=a[0]).translate(curstr) try: api.update_status(tstring.encode('utf-8')[:140]) From 379c2d4c8cf0d21a669edd18988fc874d47d072e Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Thu, 19 Mar 2015 15:40:36 +0100 Subject: [PATCH 5/5] Actually add spaces after words. ^^ --- transbot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/transbot.py b/transbot.py index 29d0ebf..bc06f1d 100644 --- a/transbot.py +++ b/transbot.py @@ -59,7 +59,7 @@ for status in timeline: curstr = "" for word in text.split(" "): - curstr += word + curstr += word + " " if word[-1] in "!?.": tstring += translate.Translator(to_lang=a[0]).translate(curstr) + " " curstr = ""