Exit cleanly, adjust threading
[gigfinder] / src / opt / gigfinder / resultsparser.py
1 from datetime import datetime, date
2 import time
3 import simplejson
4
5 def parse_json(json):
6     """ Parse json into usable format """
7     events_list = []
8     today = date.today()
9     json = simplejson.loads(json)
10     
11     events = json['events']['event']
12     for event in events:
13         venue_location = event['venue']['location']
14         address = '\n'.join([venue_location['street'],
15                              venue_location['city'],
16                              venue_location['country'],
17                              venue_location['postalcode']])
18         venue_geo = venue_location['geo:point']
19         if type(event['artists']['artist']) == list:
20             artist = '\n'.join(event['artists']['artist'])
21         else:
22             artist = event['artists']['artist']
23                 
24         yield (event['title'], 
25                event['venue']['name'], 
26                address, 
27                venue_geo['geo:long'],
28                venue_geo['geo:lat'],
29                artist, 
30                parse_date(event['startDate']))
31         
32
33 def parse_date(date_string):
34     """ Parse date string into datetime object """
35     fmt =  "%a, %d %b %Y %H:%M:%S"
36     result = time.strptime(date_string, fmt)
37     return datetime(result.tm_year, 
38                     result.tm_mon, 
39                     result.tm_mday, 
40                     result.tm_hour, 
41                     result.tm_min, 
42                     result.tm_sec)