Last active
August 29, 2015 14:15
-
-
Save bsmithgall/c2eb66e3e8c156d5e3d4 to your computer and use it in GitHub Desktop.
Parse Police Blotter with wextractor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import datetime | |
import time | |
import tempfile | |
from wextractor.extractors import CsvExtractor | |
upload_url = 'http://opendata.ucsur.pitt.edu/data/api/action/datastore_upsert?resource_id=eaa91ff8-6d63-4750-810a-53957a344346' | |
resource_id = 'eaa91ff8-6d63-4750-810a-53957a344346' | |
def parse_blotter_date(val): | |
return datetime.datetime.strptime( | |
val, '%m/%d/%Y' | |
) | |
def make_timestamp(val): | |
arrest_time = time.strptime(val, '%H:%M') | |
return datetime.datetime.combine( | |
datetime.date.today(), | |
datetime.time(arrest_time.tm_hour, arrest_time.tm_min) | |
) | |
extractor = CsvExtractor( | |
'http://apps.pittsburghpa.gov/police/arrest_blotter/arrest_blotter_Monday.csv', | |
dtypes=[ | |
str, int, int, str, parse_blotter_date, make_timestamp, str, str, int, int, str | |
] | |
) | |
data = extractor.extract() | |
for row in data: | |
row['ARREST_TIME'] = str(datetime.datetime.combine(row['ARREST_DATE'].date(), row['ARREST_TIME'].time())) | |
row.pop('ARREST_DATE') | |
test = requests.post( | |
'http://opendata.ucsur.pitt.edu/data/api/action/datastore_upsert', | |
headers={ | |
'content-type': 'application/json', | |
'authorization': 'XXXXX' | |
}, | |
data=json.dumps({ | |
'resource_id': 'eaa91ff8-6d63-4750-810a-53957a344346', | |
'records': data, | |
'method': 'insert', | |
'force': True | |
}) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment