Skip to content

Instantly share code, notes, and snippets.

@dpritchett
Created August 3, 2011 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpritchett/45072d257d0dd48c003c to your computer and use it in GitHub Desktop.
Save dpritchett/45072d257d0dd48c003c to your computer and use it in GitHub Desktop.
cron-scheduled Django task that imports a nightly data feed from a government database and loads it into my own backend
from django.core.management.base import BaseCommand, CommandError
import neighborhoods.views
from neighborhoods.models import Crime
import urllib2, json
class Command(BaseCommand):
args = 'No arguments yet'
help = "Reads yesterday's MPD data into the database"
def handle(self, *args, **options):
new_crimes = neighborhoods.views.get_crime_in_xml_for_day_offset(offset=2)
crimes_list = neighborhoods.views.crime_list_from_xml(new_crimes)
for crime in crimes_list:
saved_crime = neighborhoods.views.save_mpd_crime_from_dict(crime)
neighborhoods.views.add_zip_to_crime(saved_crime)
self.stdout.write(str(saved_crime) + "\t ZIP: " + str(saved_crime.zip_code) + "\n")
self.stdout.write(str(len(crimes_list)) + ' records processed.\n')
# m h dom mon dow command
@midnight cd /home/daniel/myapps/operation/opsafe && /home/daniel/myapps/operation/bin/python /home/daniel/myapps/operation/opsafe/manage.py nightly_crime_load > /tmp/cronlog.txt 2>&1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment