From e8a5ef90ec696f0e0926a313a3685268b0286f8d Mon Sep 17 00:00:00 2001 From: Brian Mitchell Date: Sun, 22 Apr 2018 17:38:01 -0500 Subject: [PATCH] Use a configured name for a variable location with no Twitter place --- README.md | 2 +- weatherBot.conf | 3 +++ weatherBot.py | 10 ++++++++-- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 6dfaed3..4a367ed 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ The language as well as the text used for all tweets can be edited or added in ` ### Variable Location Enable variable location to have the location for weather change. The Twitter username in the variable location user setting will be used to determine this location. The specified user must tweet with location fairly regularly (at least every 20 tweets, not including retweets), or the manually entered location will be used. The most recent tweet with a location will be used to get the location for weather. -For example, say the given user tweets from Minneapolis, MN one day. Minneapolis will be used as the location indefinitely until a new tweet with location is posted or if 20 new tweets have been posted that do not contain a location. weatherBot checked the user's timeline every 30 minutes for updated in location. +For example, say the given user tweets from Minneapolis, MN one day. Minneapolis will be used as the location indefinitely until a new tweet with location is posted or if 20 new tweets have been posted that do not contain a location. weatherBot checks the user's timeline every 30 minutes for updates in location. The human readable Twitter location will also be added to the beginning of each tweet. For example, in the same case as earlier, "Minneapolis, MN: " would be prefixed to every tweet. ## Deploying to [Heroku](https://www.heroku.com/) diff --git a/weatherBot.conf b/weatherBot.conf index bd786a0..51ffede 100644 --- a/weatherBot.conf +++ b/weatherBot.conf @@ -45,6 +45,9 @@ [variable location] ;enabled = no ;user = BrianMitchL +# some locations have coordinates but do not contain any Twitter place information +# this will be used as a fallback +;unnamed-location-name = The Wilderness [log] # write log to file diff --git a/weatherBot.py b/weatherBot.py index a3efe23..23a9556 100755 --- a/weatherBot.py +++ b/weatherBot.py @@ -65,7 +65,8 @@ def load_config(path): name=conf['default location'].get('name', 'Morris, MN')), 'variable_location': { 'enabled': conf['variable location'].getboolean('enabled', False), - 'user': conf['variable location'].get('user', 'BrianMitchL') + 'user': conf['variable location'].get('user', 'BrianMitchL'), + 'unnamed_location_name': conf['variable location'].get('unnamed-location-name', 'The Wilderness') }, 'log': { 'enabled': conf['log'].getboolean('enabled', True), @@ -179,7 +180,11 @@ def get_location_from_user_timeline(username, fallback): if tweet.coordinates is not None: lat = tweet.coordinates['coordinates'][1] lng = tweet.coordinates['coordinates'][0] - name = tweet.place.full_name + name = CONFIG['variable_location']['unnamed_location_name'] + # sometimes a tweet contains a coordinate, but is not in a Twitter place + # for example, https://twitter.com/BrianMitchL/status/982664157857271810 has coordinates, but no place + if tweet.place is not None: + 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 @@ -429,6 +434,7 @@ def main(path): api.send_direct_message(screen_name=api.me().screen_name, text=str(random.randint(0, 9999)) + traceback.format_exc()) + if __name__ == '__main__': parser = argparse.ArgumentParser(description='weatherBot') parser.add_argument('conf', metavar='conf', type=str, help='The configuration file')