ChuckWoodraska
7/26/2015 - 11:23 PM

Tweet weather

Tweet weather

import time
import pywapi
import string
import tweepy
from myconfig import CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_SECRET, ZIPCODE

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)
api = tweepy.API(auth)


while True:
    result = pywapi.get_weather_from_yahoo(ZIPCODE, 'metric')
    weather_str = "The weather is {0} and {1}C now in {2}, {3}.\n".format(string.lower(result['condition']['text']), result['condition']['temp'], result['location']['city'], result['location']['region'])
    try:
        api.update_status(weather_str)
    except Exception:
        # Most likely status was a duplicate because weather didn't change.
        pass
    time.sleep(3600)