A script that routinely interacts with Twitter
while True:
# implement logic here
# download data
# manipulate data
# post a tweet
time.sleep(60)
St Paul, MN: The clouds are saying hi, it's foggy right now.
— Weather By Brian (@WeatherByBrian) January 23, 2017
Eden Prairie, MN: The forecast for today is mostly cloudy starting in the evening. 21ºF/2ºF. Sweet!
— Weather By Brian (@WeatherByBrian) February 9, 2017
Minneapolis, MN: Get ready, there's a Freezing Rain Advisory until Tue, Feb 07 at 12:00:00 CST. https://t.co/OtpCQ26e3d
— Weather By Brian (@WeatherByBrian) February 7, 2017
Static or dynamic
Twitter has geolocation data...
def get_location_from_user_timeline(username, fallback):
"""
Load the 20 most recent tweets of a given twitter handle and
return a models.WeatherLocation object of the most recent location.
This function will find a tweet with coordinates or a place,
preferring coordinates. If a location is not found in the most
recent 20 tweets, the given fallback location will be returned.
:type username: str
:param username: twitter username to follow
:type fallback: models.WeatherLocation
:param fallback: a fallback in case no location can be found
:return: models.WeatherLocation
"""
api = get_tweepy_api()
# gets the 20 most recent tweets from the given profile
try:
timeline = api.user_timeline(screen_name=username,
include_rts=False,
count=20)
for tweet in timeline:
# if tweet has coordinates (from a smartphone)
if tweet.coordinates is not None:
lat = tweet.coordinates['coordinates'][1]
lng = tweet.coordinates['coordinates'][0]
name = tweet.place.full_name
logging.debug('Found %s: %f, %f', name, lat, lng)
return models.WeatherLocation(lat=lat,
lng=lng,
name=name)
# if the location is a place, not coordinates
elif tweet.place is not None:
point = utils.centerpoint(
tweet.place.bounding_box.coordinates[0])
lat = point[0]
lng = point[1]
name = tweet.place.full_name
logging.debug('Found the center of bounding box at '
'%s: %f, %f', name, lat, lng)
return models.WeatherLocation(lat=lat,
lng=lng,
name=name)
# fallback to hardcoded location if there is no valid data
logging.warning('Could not find tweet with location, '
'falling back to hardcoded location')
return fallback
except tweepy.TweepError as err:
logging.error(err)
logging.warning('Could not find tweet with location, '
'falling back to hardcoded location')
return fallback
import traceback
...
try:
while True:
...
except Exception as err:
logging.error(err)
logging.error('We got an exception!', exc_info=True)
if CONFIG['basic']['dm_errors']:
api = get_tweepy_api()
api.send_direct_message(screen_name=api.me().screen_name,
text=str(random.randint(0, 9999))
+ traceback.format_exc())
I also would love some code review 🙃