Commit 27471a73 authored by Mindiell's avatar Mindiell

First function to import meps from compotista - still under development, but functionnal

parent 6f3241fb
# encoding: utf-8
#import os
from datetime import datetime
import json
from os.path import exists, join
import urllib
from django.core.management.base import BaseCommand
from django.db import transaction
from picampaign.contact.models import Contact
JSON_DUMP_LOCALIZATION = join('/tmp', 'latest_meps.json')
class Command(BaseCommand):
help = 'Update the eurodeputies data by pulling it from compotista'
def handle(self, *args, **options):
if not exists(JSON_DUMP_LOCALIZATION):
print "download lastest data dump of meps from compotista"
urllib.urlretrieve('http://compotista.mm.staz.be/latest/', JSON_DUMP_LOCALIZATION)
print "load json"
meps = json.load(open(JSON_DUMP_LOCALIZATION, "r"))
# This should not be used, since old contacts should stay in database
print "cleaning Contacts"
Contact.objects.all().delete()
print "adding Contacts"
added = 0
for mep in meps:
insert_mep = False
for mandate in mep['mandates']:
if datetime.strptime(mandate['begin_date'], '%Y-%m-%d') < datetime.today() < datetime.strptime(mandate['end_date'], '%Y-%m-%d'):
insert_mep = True
if insert_mep:
first_name = mep['personal']['first_name']
last_name = mep['personal']['last_name']
if mep['contact']['phones']!=[]:
phone = mep['contact']['phones'][0]['phone']
else:
phone = ''
if mep['contact']['emails']!=[]:
mail = mep['contact']['emails'][0]['email']
else:
mail = ''
if mep['contact']['websites']!=[]:
for website in mep['contact']['websites']:
if 'twitter' in website['website']:
twitter = ''.join(('@', website['website'].split('/')[-1]))
break
else:
twitter = ''
if phone!='':
contact = Contact.objects.create(
first_name=first_name,
last_name=last_name,
phone=phone,
mail=mail,
twitter=twitter
)
added += 1
print '%d - %s %s' % (added, first_name, last_name)
if added>50:
return
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment