Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Just kidding, this is the initial add

  • Loading branch information...
commit a4e7eb4218d0d498ec2cca8c08b5e59f12caee14 1 parent 0f29dfc
Thomas Schultz authored
BIN  .DS_Store
View
Binary file not shown
81,831 free-zipcode-database.csv
View
81,831 additions, 0 deletions not shown
BIN  oldzipdb.db
View
Binary file not shown
37 zipAdapter.py
View
@@ -0,0 +1,37 @@
+import csv
+import sqlite3
+
+conn = sqlite3.connect("zipcodes.db")
+c = conn.cursor()
+
+c.execute('''create table if not exists zipcodes (id integer primary key, zipcode integer, city text, state text, country text)''')
+
+
+zipAdapter = csv.reader(open('free-zipcode-database.csv', 'rb'), delimiter=",", quotechar="'")
+for row in zipAdapter:
+ #insert_string = '''insert into zipcodes values(NULL, {0}, '{1}', '{2}', '{3}')'''.format(row[1].strip('"'),
+ # row[3].strip('"'),
+ # row[4].strip('"'),
+ # row[12].strip('"'))
+ #print insert_string
+
+ try:
+
+ c.execute('''insert into zipcodes values(NULL, ?, ?, ?, ?)''',(row[1].strip('"'),
+ row[3].strip('"'),
+ row[4].strip('"'),
+ row[12].strip('"')))
+ except Exception as e:
+ print e.message
+ break
+ conn.commit()
+
+
+ #print row[12].strip('"')
+ #print row[1].strip('"')
+
+ #print row[3].strip('"')
+ #print row[4].strip('"')
+
+
+c.close()
48 zipapiserver.py
View
@@ -0,0 +1,48 @@
+import time
+import BaseHTTPServer
+import urlparse
+import json
+import sqlite3
+
+
+HOST_NAME = 'localhost' # !!!REMEMBER TO CHANGE THIS!!!
+PORT_NUMBER = 80 # Maybe set this to 9000.
+
+class ZipAPIServerHandler(BaseHTTPServer.BaseHTTPRequestHandler):
+ def do_GET(s):
+ s.send_response(200)
+ s.send_header("Content-type", "text/json")
+ # The Magic!
+ s.send_header("Access-Control-Allow-Origin", "*")
+ s.end_headers()
+
+ # Get the zip from the data
+ qs = {}
+ path = s.path
+ if '?' in path:
+ path, tmp = path.split('?', 1)
+ qs = urlparse.parse_qs(tmp)
+ the_zip = qs['zip']
+
+ # Query database with the ZIP and pull the city, state, country
+ conn = sqlite3.connect('zipcodes.db')
+ c = conn.cursor()
+
+ c.execute('select Country, State, City from zipcodes where ZipCode=?', the_zip)
+
+ row = c.fetchone()
+ if row is not None:
+ s.wfile.write("{" + json.dumps(row) + "}")
+ else:
+ s.wfile.write("404")
+
+
+if __name__ == '__main__':
+ server_class = BaseHTTPServer.HTTPServer
+ httpd = server_class((HOST_NAME, PORT_NUMBER), ZipAPIServerHandler)
+ #print time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER)
+ try:
+ httpd.serve_forever()
+ except KeyboardInterrupt:
+ pass
+ httpd.server_close()
BIN  zipcodes.db
View
Binary file not shown
43 zippidydoda.js
View
@@ -0,0 +1,43 @@
+(function( $ ) {
+ $.fn.ziptastic = function( options ) {
+
+ // Create some defaults, extending them with any options that were provided
+ var settings = $.extend( {
+ 'zip' : 'zip',
+ 'cityid' : 'city',
+ 'stateid' : 'state',
+ 'countryid' : 'country'
+ }, options);
+
+ return this.each(function() {
+ var zip_element = this.children("#" + settings.zipid);
+ var city_element = this.children("#" + settings.cityid);
+ var state_element = this.children("#" + settings.stateid);
+ var country_element = this.children("#" + settings.countryid);
+
+ // Hide the elements that we're going to retreive data for.
+ city_element.hide();
+ state_element.hide();
+ country_element.hide();
+
+ //TODO: Capture the onchange event of the zip field...
+
+ var client = new XMLHttpRequest();
+ client.open("GET", "http://localhost?zip=48867", true);
+ client.onreadystatechange = function() {
+ if(client.readyState == 4) {
+ alert(client.responseText);
+ var location_data = this.parseJSON(client.responseText);
+
+ city_element.val(location_data[0]);
+ state_element.val(location_data[1]);
+ country_element.val(location_data[2]);
+ };
+ };
+
+ client.send();
+
+ });
+
+ };
+})( jQuery );
Please sign in to comment.
Something went wrong with that request. Please try again.