This repository has been archived by the owner on Jul 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command to retrieve all reports and generate filings.js (v1.1.0)
- Loading branch information
Showing
7 changed files
with
657 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,7 @@ | |
|
||
setup( | ||
name='tx_tecreports', | ||
version='1.0.0', | ||
version='1.1.0', | ||
description='Package for dealing with TEC reports', | ||
author='Tribune Tech', | ||
author_email='[email protected]', | ||
|
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import csv | ||
import datetime | ||
import json | ||
import logging | ||
from optparse import make_option | ||
|
||
from django.core.management.base import BaseCommand | ||
|
||
from ...fetcher import get_filings_list | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class Command(BaseCommand): | ||
option_list = BaseCommand.option_list + ( | ||
make_option('--filename', action='store', dest='filename', | ||
default='./data/curated-statewide-candidates-2014.csv', | ||
help='File to use parse tecid and tecpacid from'), | ||
make_option('--report-due', action='store', dest='raw_report_due', | ||
help='Report due date of filing to parse (format: YYYY/MM/DD)'), | ||
) | ||
|
||
def get_matching_filing(self, filer_id, **kwargs): | ||
logger.debug('fetching filings for %s' % filer_id) | ||
filing_list = get_filings_list(filer_id) | ||
|
||
logger.debug('found %d total filings' % len(filing_list)) | ||
matching_filings = filing_list.find(**kwargs) | ||
matching_filings_len = len(matching_filings) | ||
logger.debug('found %d matching filings' % matching_filings_len) | ||
|
||
if matching_filings_len > 1: | ||
logger.warning('Unable to process %s' % filer_id) | ||
return | ||
if matching_filings_len is 0: | ||
logger.warning('Unable to find any filings for %s' % kwargs) | ||
return | ||
return matching_filings[0] | ||
|
||
def generate_object(self, record, order, filer_id_key='tecid', | ||
filer_type='', **kwargs): | ||
filing = self.get_matching_filing(record[filer_id_key], **kwargs) | ||
if filing is None: | ||
return | ||
|
||
return { | ||
'display_name': record['name'], | ||
'type': filer_type, | ||
'race': record['race'], | ||
'party': record['party'], | ||
'filer_id': record[filer_id_key], | ||
'order': order, | ||
'report_id': filing.report_id, | ||
} | ||
|
||
def handle(self, filename, raw_report_due, **kwargs): | ||
report_due = (datetime.datetime.strptime(raw_report_due, '%Y/%m/%d') | ||
.date()) | ||
|
||
reader = csv.DictReader(open(filename, 'rb')) | ||
data = [] | ||
for record in reader: | ||
logger.info('Fetching for %s' % record['name']) | ||
if record['tecid'] == '00032876': | ||
logger.warn("Skipping %s because it's not properly formatted" % | ||
record['tecid']) | ||
continue | ||
|
||
obj = self.generate_object(record, record['new_order'], | ||
report_due=report_due) | ||
if obj: | ||
data.append(obj) | ||
|
||
if record['tecpacid']: | ||
logger.info('Fetching SPAC for %s' % record['name']) | ||
obj = self.generate_object(record, int(record['new_order']) + 1, | ||
'tecpacid', filer_type='(SPAC)', report_due=report_due) | ||
if obj: | ||
data.append(obj) | ||
print json.dumps(data) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import csv | ||
import datetime | ||
import logging | ||
from optparse import make_option | ||
|
||
from django.core.management.base import BaseCommand | ||
|
||
from ...fetcher import get_filings_list | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class Command(BaseCommand): | ||
option_list = BaseCommand.option_list + ( | ||
make_option('--filename', action='store', dest='filename', | ||
help='File to use parse tecid and tecpacid from'), | ||
make_option('--report-due', action='store', dest='raw_report_due', | ||
help='Report due date of filing to parse (format: YYYY/MM/DD)'), | ||
) | ||
|
||
def get_matching_filing(self, filer_id, **kwargs): | ||
logger.debug('fetching filings for %s' % filer_id) | ||
filing_list = get_filings_list(filer_id) | ||
|
||
logger.debug('found %d total filings' % len(filing_list)) | ||
matching_filings = filing_list.find(**kwargs) | ||
matching_filings_len = len(matching_filings) | ||
logger.debug('found %d matching filings' % matching_filings_len) | ||
|
||
if matching_filings_len > 1: | ||
logger.warning('Unable to process %s' % filer_id) | ||
return | ||
if matching_filings_len is 0: | ||
logger.warning('Unable to find any filings for %s' % kwargs) | ||
return | ||
return matching_filings[0] | ||
|
||
def fetch_and_save(self, filer_id, **kwargs): | ||
logger.info('finding filing for %s' % filer_id) | ||
filing = self.get_matching_filing(filer_id, **kwargs) | ||
if filing: | ||
try: | ||
filing.report.save() | ||
logger.info('found %s and saved' % filer_id) | ||
except KeyError: | ||
logger.warn('KeyError on filing for %s' % filer_id) | ||
|
||
def handle(self, filename, raw_report_due, **kwargs): | ||
logger.debug('processing %s' % filename) | ||
|
||
reader = csv.DictReader(open(filename, 'rb')) | ||
report_due = datetime.datetime.strptime(raw_report_due, '%Y/%m/%d').date() | ||
|
||
for record in reader: | ||
# This report is known to be a malformed CSV | ||
if record['tecid'] == '00032876': | ||
logger.warn("Skipping %s because it's not properly formatted" % | ||
record['tecid']) | ||
continue | ||
self.fetch_and_save(record['tecid'], report_due=report_due) | ||
if record['tecpacid']: | ||
self.fetch_and_save(record['tecpacid'], report_due=report_due) |