|
| 1 | +#!/bin/env python |
| 2 | + |
| 3 | +import json |
| 4 | +from requests_oauthlib import OAuth1Session |
| 5 | +import time |
| 6 | +import yaml |
| 7 | + |
| 8 | +# Normally we would organize this as a class, but we haven't talked |
| 9 | +# about how or why to do that yet |
| 10 | + |
| 11 | +# Getting credentials |
| 12 | +with open('../etc/creds.yml', 'r') as f: |
| 13 | + creds = yaml.load(f) |
| 14 | + |
| 15 | +# Setting up Twitter OAuth object |
| 16 | +twitter = OAuth1Session(**creds) |
| 17 | +search_endpoint = "https://api.twitter.com/1.1/search/tweets.json" |
| 18 | +post_endpoint = "https://api.twitter.com/1.1/statuses/update.json" |
| 19 | + |
| 20 | +# Enter your search parameters here |
| 21 | +search_parameters = {'q' : 'DlabAtBerkeley'} |
| 22 | + |
| 23 | +# Here is our main function |
| 24 | +def main(): |
| 25 | + # You may want to set a condition here, like 'never on Sunday' |
| 26 | + while True: |
| 27 | + r = twitter.get(search_endpoint, search_parameters) |
| 28 | + if r.ok: |
| 29 | + for tweet in r: |
| 30 | + status = r['text'] |
| 31 | + # You may want to set a condition here |
| 32 | + if True: |
| 33 | + # Enter your post parameters here |
| 34 | + post_parameters = { |
| 35 | + 'status' : status + ' @dillonniederhut' |
| 36 | + } |
| 37 | + p = r.post(post_endpoint, post_parameters) |
| 38 | + if p.ok: |
| 39 | + print(time.asctime(), post_parameters, p.status) |
| 40 | + else raise BaseException(r.reason) |
| 41 | + time.sleep(30) |
| 42 | + # Else, iff you are exceeding the rate limit |
| 43 | + elif r.status_code == 429: |
| 44 | + time.sleep(60) |
| 45 | + else: |
| 46 | + raise BaseException(r.reason) |
| 47 | + |
| 48 | +# This bit of syntax tells Python not to run this code when imported |
| 49 | +# into an interactive session, but only when it is called as a program |
| 50 | +# from bash or another program. |
| 51 | + |
| 52 | +if __name__ == 'main': |
| 53 | + main() |
0 commit comments