Skip to content

Commit

Permalink
made code python 3 compartible and removed timeout bug
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeyny committed Jul 29, 2016
1 parent f83e392 commit 1c4621f
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 48 deletions.
25 changes: 20 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,27 @@ Python Retweet Bot

This script retweets all Tweets containing your search term. To limit Twitter requests a savepoint file marks Tweets found before. It's Twitter API v1.1 ready.

Dependecies:
-------------
* Tweepy

```pip install tweepy```

* Or alternatively

```pip install requirements.txt```

How to start:
-------------
* Depends on http://tweepy.github.com/ (pip install tweepy)
* Copy 'config.sample' to 'config'
* Define your hashtag or search query
* Add your Twitter app credentials

* Define your hashtag or search query in the config file
* Define the number of Retweets at a time (This avoids overloading -Limit is 180 RT/ 15 mins)
* Add your Twitter app credentials in the config file
* (Tune some other options if you like)
* $ python retweet.py
* Add this call to your crontab (or something similar) to retweet all new tweets regularly
* Add this call to your crontab(unix)/task scheduler(windows) (or something similar) to retweet all new tweets regularly

Compatibility
-------------

Compatible with Python 3.x ,tested on Python 3.5.
11 changes: 0 additions & 11 deletions config.sample

This file was deleted.

3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
tweepy==3.2.0
tweepy==3.5.0
configparser==3.5.0

66 changes: 35 additions & 31 deletions retweet.py
Original file line number Diff line number Diff line change
@@ -1,82 +1,86 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os, ConfigParser, tweepy, inspect, hashlib
import os, configparser, tweepy, inspect, hashlib

path = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))

# read config
config = ConfigParser.SafeConfigParser()
config = configparser.SafeConfigParser()
config.read(os.path.join(path, "config"))

# your hashtag or search query and tweet language (empty = all languages)
hashtag = config.get("settings","search_query")
tweetLanguage = config.get("settings","tweet_language")
hashtag = config.get("settings", "search_query")
tweetLanguage = config.get("settings", "tweet_language")

# Number retweets per time
num = int(config.get("settings","number_of_rt"))

# blacklisted users and words
userBlacklist = []
wordBlacklist = ["RT", u"♺"]

# build savepoint path + file
hashedHashtag = hashlib.md5(hashtag).hexdigest()
hashedHashtag = hashlib.md5(hashtag.encode('ascii')).hexdigest()
last_id_filename = "last_id_hashtag_%s" % hashedHashtag
rt_bot_path = os.path.dirname(os.path.abspath(__file__))
last_id_file = os.path.join(rt_bot_path, last_id_filename)

# create bot
auth = tweepy.OAuthHandler(config.get("twitter","consumer_key"), config.get("twitter","consumer_secret"))
auth.set_access_token(config.get("twitter","access_token"), config.get("twitter","access_token_secret"))
auth = tweepy.OAuthHandler(config.get("twitter", "consumer_key"), config.get("twitter", "consumer_secret"))
auth.set_access_token(config.get("twitter", "access_token"), config.get("twitter", "access_token_secret"))
api = tweepy.API(auth)

# retrieve last savepoint if available
try:
with open(last_id_file, "r") as file:
savepoint = file.read()
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."
savepoint = ""
print("No savepoint found. Bot is now searching for results")

# search query
timelineIterator = tweepy.Cursor(api.search, q=hashtag, since_id=savepoint, lang=tweetLanguage).items()
timelineIterator = tweepy.Cursor(api.search, q=hashtag, since_id=savepoint, lang=tweetLanguage).items(num)

# put everything into a list to be able to sort/filter
timeline = []
for status in timelineIterator:
timeline.append(status)
timeline.append(status)


try:
last_tweet_id = timeline[0].id
except IndexError:
last_tweet_id = savepoint

# filter @replies/blacklisted words & users out and reverse timeline
timeline = filter(lambda status: status.text[0] != "@", timeline)
#timeline = filter(lambda status: status.text[0] = "@", timeline) - uncomment to remove all tweets with an @mention
timeline = filter(lambda status: not any(word in status.text.split() for word in wordBlacklist), timeline)
timeline = filter(lambda status: status.author.screen_name not in userBlacklist, timeline)
timeline = list(timeline)
timeline.reverse()

tw_counter = 0
err_counter = 0

# iterate the timeline and retweet
for status in timeline:
try:
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') }

api.retweet(status.id)
tw_counter += 1
except tweepy.error.TweepError as e:
# just in case tweet got deleted in the meantime or already retweeted
err_counter += 1
#print e
continue

print "Finished. %d Tweets retweeted, %d errors occured." % (tw_counter, err_counter)
try:
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')})

api.retweet(status.id)
tw_counter += 1
except tweepy.error.TweepError as e:
# just in case tweet got deleted in the meantime or already retweeted
err_counter += 1
# print e
continue

print("Finished. %d Tweets retweeted, %d errors occured." % (tw_counter, err_counter))

# write last retweeted tweet id to file
with open(last_id_file, "w") as file:
file.write(str(last_tweet_id))

file.write(str(last_tweet_id))

0 comments on commit 1c4621f

Please sign in to comment.