From 93b83af2bedace2d1735736c4ef058969db83676 Mon Sep 17 00:00:00 2001 From: Francis Tseng Date: Wed, 25 Jun 2014 11:53:03 -0400 Subject: [PATCH] initial commit --- .gitignore | 13 +++++++++++ config-sample.py | 6 +++++ readme.md | 1 + requirements.txt | 2 ++ twitter.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 80 insertions(+) create mode 100644 .gitignore create mode 100644 config-sample.py create mode 100644 readme.md create mode 100644 requirements.txt create mode 100644 twitter.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b213e9a --- /dev/null +++ b/.gitignore @@ -0,0 +1,13 @@ +.DS_Store +*.pyc +.sass-cache/ +node_modules/ +fontcustom/* +*.swp +*.log +config.py +*.rdb +.webassets-cache +bower/ +index.css +config.py \ No newline at end of file diff --git a/config-sample.py b/config-sample.py new file mode 100644 index 0000000..841b910 --- /dev/null +++ b/config-sample.py @@ -0,0 +1,6 @@ +# You can get these by creating a new app at +# https://apps.twitter.com/ +TWITTER_CONSUMER_KEY = 'fill_me_in' +TWITTER_CONSUMER_SECRET = 'fill_me_in' +TWITTER_ACCESS_TOKEN = 'fill_me_in' +TWITTER_ACCESS_SECRET = 'fill_me_in' diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..5d4257d --- /dev/null +++ b/readme.md @@ -0,0 +1 @@ +# CLONES FOR EVERYBODY diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..568173b --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +tweepy==2.3.0 +wsgiref==0.1.2 diff --git a/twitter.py b/twitter.py new file mode 100644 index 0000000..26533fb --- /dev/null +++ b/twitter.py @@ -0,0 +1,58 @@ +import tweepy +from tweepy import TweepError + +import config + +def _api(): + """ + Load auth info from config. + Setup things on Twitter's end at: + https://apps.twitter.com/ + """ + auth = tweepy.OAuthHandler(config.TWITTER_CONSUMER_KEY, config.TWITTER_CONSUMER_SECRET) + auth.set_access_token(config.TWITTER_ACCESS_TOKEN, config.TWITTER_ACCESS_SECRET) + + # Return API object. + return tweepy.API(auth) + +api = _api() + +def tweets(username, count=200): + """ + Returns 200 last tweets for a user. + """ + return [{ + 'body': tweet.text, + 'tid': tweet.id, + 'protected': tweet.user.protected, + 'retweeted': tweet.retweeted + } for tweet in api.user_timeline(screen_name=username, count=count)] + +def retweet(id): + """ + Retweet a tweet by id. + """ + try: + api.retweet(id) + except TweepError as err: + # Assume we may have violated some rate limit + # and forget about it + if '403' in err: + print('403 error when trying to retweet. Possibly hit a rate limit.') + else: + raise err + + +def tweet(text): + """ + Tweet something from your account. + """ + try: + api.update_status(text) + except TweepError as err: + # Assume we may have violated some rate limit + # and forget about it + if '403' in err: + logger.info('403 error when trying to tweet. Possibly hit a rate limit.') + else: + raise err \ No newline at end of file