c8a97420736319c2ea048fa9dfaa770095797203
[swohoa] / swohoa2sqlite.py
1 #!/usr/bin/env python
2
3 import re
4 import urllib2
5 import json
6 import sqlite3 as lite
7 import sys
8
9 from pprint import pprint
10
11 # download the raw data from the website
12 raw_data = urllib2.urlopen("http://shewentofherownaccord.com/mapfeed").read()
13
14 # fix some errors in the json
15 raw_data = raw_data.replace('\r\n', '')
16 raw_data = raw_data.replace('""', '"')
17 raw_data = re.sub(';$', '', raw_data)
18
19 print raw_data
20 sys.exit()
21
22 data = json.loads(raw_data)
23
24 # interate through the data from the json website and create a list of
25 # data we want to store
26 places = []
27 entries = data['places']
28 # interate through the list of things and import it
29 for entry in entries:
30     joke_id = entry['entries'][0]['id']
31     joke = entry['entries'][0]['text']
32     (coord_lat, coord_long) = entry['location']['point']
33     location = entry['location']['name']
34     url = "http://shewentofherownaccord.com/joke/%s" % joke_id
35
36     place_values = (joke_id, joke, coord_lat, coord_long, location, url)
37     places.append(place_values)
38
39
40 # try to connect to the database and make sure that things work
41 con = None
42 try:
43     con = lite.connect('swohoa.db')
44             
45     cur = con.cursor()    
46     cur.execute('SELECT SQLITE_VERSION()') 
47     data = cur.fetchone() 
48     print "SQLite version: %s" % data                
49
50 except lite.Error, e: 
51     print "Error %s:" % e.args[0]
52     sys.exit(1) 
53
54 cur = con.cursor()    
55 cur.execute("DROP TABLE IF EXISTS jokes")
56 cur.execute("CREATE TABLE jokes(id INT, joke TEXT, lat FLOAT, long FLOAT, location TEXT, url TEXT)")
57
58 cur.executemany("INSERT INTO jokes VALUES(?, ?, ?, ?, ?, ?)", places)
59 con.commit()
60
61 if con:
62     con.close()
63

Benjamin Mako Hill || Want to submit a patch?