remove swap file
[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 data = json.loads(raw_data)
20
21 # interate through the data from the json website and create a list of
22 # data we want to store
23 places = []
24 entries = data['places']
25 # interate through the list of things and import it
26 for entry in entries:
27     joke_id = entry['entries'][0]['id']
28     joke = entry['entries'][0]['text']
29     (coord_lat, coord_long) = entry['location']['point']
30     location = entry['location']['name']
31     url = "http://shewentofherownaccord.com/joke/%s" % joke_id
32
33     place_values = (joke_id, joke, coord_lat, coord_long, location, url)
34     places.append(place_values)
35
36
37 # try to connect to the database and make sure that things work
38 con = None
39 try:
40     con = lite.connect('swohoa.db')
41             
42     cur = con.cursor()    
43     cur.execute('SELECT SQLITE_VERSION()') 
44     data = cur.fetchone() 
45     print "SQLite version: %s" % data                
46
47 except lite.Error, e: 
48     print "Error %s:" % e.args[0]
49     sys.exit(1) 
50
51 cur = con.cursor()    
52 cur.execute("DROP TABLE IF EXISTS jokes")
53 cur.execute("CREATE TABLE jokes(id INT, joke TEXT, lat FLOAT, long FLOAT, location TEXT, url TEXT)")
54
55 cur.executemany("INSERT INTO jokes VALUES(?, ?, ?, ?, ?, ?)", places)
56 con.commit()
57
58 if con:
59     con.close()
60

Benjamin Mako Hill || Want to submit a patch?