commit e3fb71b68fbc9a0eb29254af1834627b8706ca60 Author: LongHairedHacker Date: Tue Sep 20 21:28:39 2016 +0200 First working version diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..98206fb --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.pyc +tokens.py +virtenv diff --git a/main.py b/main.py new file mode 100644 index 0000000..3ec9fd4 --- /dev/null +++ b/main.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python2 +# -*- coding: utf-8 -*- + +import tweepy +from time import sleep + +from retweetingstreamlistener import RetweetingStreamListener +from tokens import CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET + + +def setup_api(): + auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) + auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET) + return tweepy.API(auth) + + +if __name__ == '__main__': + api = setup_api() + + while True: + stream = tweepy.Stream(auth = api.auth, listener=RetweetingStreamListener(api)) + stream.filter(track=['#zaunei2016', '@zaunei2016']) + print "[Error] Something went wrong reconnecting" + sleep(10.0) diff --git a/retweetingstreamlistener.py b/retweetingstreamlistener.py new file mode 100644 index 0000000..25a00d2 --- /dev/null +++ b/retweetingstreamlistener.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python2 +# -*- coding: utf-8 -*- + +import tweepy + +# Nothing wrong with a nice enterprisy class name, right? +class RetweetingStreamListener(tweepy.StreamListener): + + + def on_status(self, status): + print "[Info] %s - %s" % (status.user.screen_name, status.text) + + if not hasattr(status, 'retweeted_status'): + status.retweet() + print "[Info] Retweeted!" + + + print "-----------------------------------------" + + + def on_error(self, status_code): + # Rate limiting + if status_code == 420: + print "[Error] Got Error 420, rate limiting in effect" + #returning False in on_data disconnects the stream + return False + + print "[Error] Got status code %d" % status_code diff --git a/tokens_example.py b/tokens_example.py new file mode 100644 index 0000000..f8dee0d --- /dev/null +++ b/tokens_example.py @@ -0,0 +1,9 @@ +#!/usr/bin/env python2 +# -*- coding: utf-8 -*- + + +CONSUMER_KEY = 'f00' +CONSUMER_SECRET = 'b4r' + +ACCESS_TOKEN = 'd43d' +ACCESS_TOKEN_SECRET = 'b33f'