<commit_before>#!/usr/bin/env python

"""
Example showing how to import data from the Socrata API.
"""

import json

import requests

PANDA_API = 'http://localhost:8000/api/1.0'
PANDA_AUTH_PARAMS = {
    'email': '<EMAIL>',
    'api_key': '<KEY>'
}
PANDA_DATASET_SLUG = 'foia-request-log-311'

PANDA_DATASET_URL = '%s/dataset/%s/' % (PANDA_API, PANDA_DATASET_SLUG)
PANDA_DATA_URL = '%s/dataset/%s/data/' % (PANDA_API, PANDA_DATASET_SLUG)

SOCRATA_URL = 'http://data.cityofchicago.org/api/views/j2p9-gdf5/rows.json'

# Utility functions
def panda_get(url):
    return requests.get(url, params=PANDA_AUTH_PARAMS)

def panda_put(url, data):
    return requests.post(url, data, params=PANDA_AUTH_PARAMS, headers={ 'Content-Type': 'application/json' })

# Check if dataset exists
response = panda_get(PANDA_DATASET_URL)

# Create dataset if necessary
if response.status_code == 404:
    dataset = {
        'name': 'FOIA Request Log - Office of the Mayor',
        'description': 'FOIA requests made to the mayor\'s office.',
        'schema': [{
            'column': 'Requestor Name',
            'type': 'unicode'
        }, {
            'column': 'Organization',
            'type': 'unicode'
        }, {
            'column': 'Description of Request',
            'type': 'unicode'
        }, {
            'column': 'Date Received',
            'type': 'unicode'
        }, {
            'column': 'Due Date',
            'type': 'unicode'
        }]
    }

    response = panda_put(PANDA_DATASET_URL, json.dumps(dataset))

# Fetch latest data from Socrata
print 'Fetching latest data'
response = requests.get(SOCRATA_URL)

data = json.loads(response.content)

put_data = {
    'objects': []
}

for i, row in enumerate(data['data']):
    # First 8 columns are metadata
    put_data['objects'].append({
        'data': row[-5:],
        'external_id': unicode(row[0])   # per-dataset id
    })

    if i % 100 == 0:
        print 'Updating 100 rows...'

        panda_put(PANDA_DATA_URL, json.dumps(data))
        put_data['objects'] = []
        
if put_data['objects']:
    print 'Updating %i rows' % len(put_data['objects'])
    panda_put(PANDA_DATA_URL, json.dumps(data))

print 'Done'
<commit_msg>Fetch simpler row data from Socrata.<commit_after>#!/usr/bin/env python

"""
Example showing how to import data from the Socrata API.
"""

import json

import requests

PANDA_API = 'http://localhost:8000/api/1.0'
PANDA_AUTH_PARAMS = {
    'email': '<EMAIL>',
    'api_key': '<KEY>'
}
PANDA_DATASET_SLUG = 'foia-request-log-311'

PANDA_DATASET_URL = '%s/dataset/%s/' % (PANDA_API, PANDA_DATASET_SLUG)
PANDA_DATA_URL = '%s/dataset/%s/data/' % (PANDA_API, PANDA_DATASET_SLUG)

SOCRATA_URL = 'http://data.cityofchicago.org/api/views/j2p9-gdf5/rows.json?unwrapped=true'

# Utility functions
def panda_get(url):
    return requests.get(url, params=PANDA_AUTH_PARAMS)

def panda_put(url, data):
    return requests.post(url, data, params=PANDA_AUTH_PARAMS, headers={ 'Content-Type': 'application/json' })

# Check if dataset exists
response = panda_get(PANDA_DATASET_URL)

# Create dataset if necessary
if response.status_code == 404:
    dataset = {
        'name': 'FOIA Request Log - Office of the Mayor',
        'description': 'FOIA requests made to the mayor\'s office.',
        'schema': [{
            'column': 'Requestor Name',
            'type': 'unicode'
        }, {
            'column': 'Organization',
            'type': 'unicode'
        }, {
            'column': 'Description of Request',
            'type': 'unicode'
        }, {
            'column': 'Date Received',
            'type': 'unicode'
        }, {
            'column': 'Due Date',
            'type': 'unicode'
        }]
    }

    response = panda_put(PANDA_DATASET_URL, json.dumps(dataset))

# Fetch latest data from Socrata
print 'Fetching latest data'
response = requests.get(SOCRATA_URL)

data = json.loads(response.content)

put_data = {
    'objects': []
}

for i, row in enumerate(data):
    # First 8 columns are metadata
    put_data['objects'].append({
        'data': row[-5:],
        'external_id': unicode(row[0])   # per-dataset id
    })

    if i % 100 == 0:
        print 'Updating 100 rows...'

        panda_put(PANDA_DATA_URL, json.dumps(data))
        put_data['objects'] = []
        
if put_data['objects']:
    print 'Updating %i rows' % len(put_data['objects'])
    panda_put(PANDA_DATA_URL, json.dumps(data))

print 'Done'

{'fraction_non_alphanumeric': 0.14713835060838215, 'fraction_numerical': 0.011266336187471835, 'mean_word_length': 2.8633594429939078, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5359484', 'n_tokens_mistral': 1589, 'n_tokens_neox': 1546, 'n_words': 335}