Skip to content
This repository has been archived by the owner on Dec 23, 2023. It is now read-only.

Commit

Permalink
Merge pull request #37 from BrianMitchL/location-name-fix
Browse files Browse the repository at this point in the history
Location name fix
  • Loading branch information
BrianMitchL committed Apr 25, 2018
2 parents e725755 + 4d738fc commit d89a092
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
Expand Down
19 changes: 16 additions & 3 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,10 +615,11 @@ def test_config(self):
utils.Time(hour=18, minute=0),
utils.Time(hour=22, minute=0)]
},
'default_location': models.WeatherLocation(-79, 12, 'Just a Test'),
'default_location': models.WeatherLocation(-79.0, 12.0, 'Just a Test'),
'variable_location': {
'enabled': True,
'user': 'test_user'
'user': 'test_user',
'unnamed_location_name': 'Somewhere in deep space'
},
'log': {
'enabled': False,
Expand Down Expand Up @@ -672,7 +673,8 @@ def test_config(self):
}
conf['variable location'] = {
'enabled': 'yes',
'user': 'test_user'
'user': 'test_user',
'unnamed_location_name': 'Somewhere in deep space'
}
conf['log'] = {
'enabled': '0',
Expand Down Expand Up @@ -756,6 +758,17 @@ def test_get_location_from_user_timeline_coordinates(self):
self.assertTrue(type(loc) is models.WeatherLocation)
self.assertEqual(loc, test_loc)

@replace('weatherBot.get_tweepy_api', mocked_get_tweepy_api)
def test_get_location_from_user_timeline_coordinates_no_place_full_name(self):
"""Testing getting a location from twitter account's recent tweets using the coordinates property
when a place does not exist for that location"""
fallback_loc = models.WeatherLocation(4, 3, 'test')
test_loc = models.WeatherLocation(2.5, 1.5, 'unnamed location')
weatherBot.CONFIG['variable_location']['unnamed_location_name'] = 'unnamed location'
loc = weatherBot.get_location_from_user_timeline('coordsnoplace', fallback_loc)
self.assertTrue(type(loc) is models.WeatherLocation)
self.assertEqual(loc, test_loc)

@replace('weatherBot.get_tweepy_api', mocked_get_tweepy_api)
def test_get_location_from_user_timeline_place(self):
"""Testing getting a location from twitter account's recent tweets using the place bounding box"""
Expand Down
5 changes: 5 additions & 0 deletions test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ def __init__(self):
if screen_name == 'nocoords':
self.coordinates = None
self.place = Place('cool place')
elif screen_name == 'coordsnoplace':
self.coordinates = {
'coordinates': [1.5, 2.5]
}
self.place = None
else:
self.coordinates = {
'coordinates': [1, 2]
Expand Down
3 changes: 3 additions & 0 deletions weatherBot.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions weatherBot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit d89a092

Please sign in to comment.