Created
January 16, 2013 21:19
-
-
Save abelsonlive/4551070 to your computer and use it in GitHub Desktop.
tweetbot.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import tweepy | |
import time | |
import oauth2 | |
import re | |
from urllib import quote | |
# THIS IS WHAT YOU CUSTOMIZE # | |
# terms | |
LIST = "members-of-congress" | |
OWNER = "cspan" | |
TERMS = "(gunsafety)|(firearm)|(assault weapon)|(guncontrol)|(gunviolence)|(gun)|(nra)|(guns)|(nowisthetime)" | |
# access keys | |
consumer_key="---" | |
consumer_secret="---" | |
access_token="---" | |
access_token_secret="---" | |
# THIS IS THE TWEET BOT # | |
# authenticate | |
auth = tweepy.OAuthHandler(consumer_key, consumer_secret) | |
auth.set_access_token(access_token, access_token_secret) | |
api = tweepy.API(auth) | |
# meta variables | |
url = "http://api.twitter.com/1/lists/statuses.json?slug=%s&owner_screen_name=%s&per_page=100&page=1&include_entities=true" % (LIST, OWNER) | |
tweeted = [] | |
test = 1 | |
# endless loop | |
while test > 0: | |
# download list of congressional tweets | |
r = requests.get(url) | |
list_tweets = r.json | |
# extract gun tweets | |
filter_tweets = [] | |
for lt in list_tweets: | |
id_str = lt['id_str'].encode('utf-8') | |
name = lt['user']['screen_name'].encode('utf-8') | |
text = lt['text'].encode('utf-8') | |
if re.search(TERMS, text.lower()): | |
filter_tweets.append([id_str, name, text]) | |
# retweet gun tweets | |
for ft in filter_tweets: | |
the_id = ft[0] | |
if the_id not in tweeted: | |
try: | |
tweeted.append(the_id) | |
print "new tweet:", ft[1] + ":", ft[2] | |
api.retweet(the_id) | |
time.sleep(5) | |
except: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment