Created
June 27, 2012 21:34
-
-
Save Roasbeef/3007013 to your computer and use it in GitHub Desktop.
Weather Bot
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 tweepy | |
import time | |
def get_weather(city): | |
''' | |
returns a dictionary of the forecast for a specified city | |
''' | |
import requests | |
from xml.dom import minidom | |
city = city.replace(' ', '+') | |
url = 'http://www.google.com/ig/api?weather=%s' % city | |
r = requests.get(url) | |
dom = minidom.parseString(r.text) | |
forecasts = [] | |
for node in dom.getElementsByTagName('forecast_conditions'): | |
forecasts.append({ | |
'day': node.getElementsByTagName('day_of_week')[0].getAttribute('data'), | |
'low': node.getElementsByTagName('low')[0].getAttribute('data'), | |
'high': node.getElementsByTagName('high')[0].getAttribute('data'), | |
'condition': node.getElementsByTagName('condition')[0].getAttribute('data') | |
}) | |
c_condition = dom.getElementsByTagName('current_conditions')[0] | |
return { | |
'current_condition': c_condition.getElementsByTagName('condition')[0].getAttribute('data').lower(), | |
'current_temp': c_condition.getElementsByTagName('temp_f')[0].getAttribute('data'), | |
'forecasts': forecasts, | |
'title': dom.getElementsByTagName('forecast_information')[0].getElementsByTagName('city')[0].getAttribute('data') | |
} | |
def init_bot(): | |
# authenticate bot | |
CONSUMER_KEY = '' | |
CONSUMER_SECRET = '' | |
ACCESS_KEY = '' | |
ACCESS_SECRET = '' | |
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) | |
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET) | |
bot = tweepy.API(auth) | |
mentions = bot.mentions() | |
since_id = mentions[0].id | |
return bot, since_id | |
def main(): | |
bot, since_id = init_bot() | |
while True: | |
mentions = bot.mentions(since_id) | |
if not mentions: | |
print 'No new mentions' | |
time.sleep(20) | |
continue | |
for mention in mentions: | |
print 'Mention: @%(author)s: %(tweet)s | ID: %(ID)s' % {'author': mention.author.screen_name, | |
'tweet': mention.text, | |
'ID': mention.id | |
} | |
tweet = mention.text | |
location = tweet.replace("@TweetLeWeather ", "") | |
if location.isdigit() or len(location.split()) in range(1, 3): | |
try: | |
data = get_weather(location) | |
weather = 'It is %(current_temp)s degrees and %(current_condition)s in %(title)s' % data | |
except: | |
print 'Failed to get weather info' | |
since_id = mention.id | |
continue | |
tweet = '@%(author)s %(weather)s #TweetLeWeather' % {'author': mention.author.screen_name, 'weather': weather} | |
attempts = 0 | |
while attempts < 3: | |
try: | |
bot.update_status(tweet, mention.id) | |
print 'Tweet sent: %s \n\n' % tweet | |
since_id = mention.id | |
break | |
except: | |
attempts += 1 | |
print 'Error sending tweet' | |
time.sleep(5) | |
else: | |
print 'Not valid query' | |
since_id = mention.id | |
continue | |
since_id = mention.id | |
print 'one pass' | |
time.sleep(5) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment