#!/usr/bin/python import datetime from datetime import date import urllib2 import json import time import re # Magic import sys # sys.setdefaultencoding is cancelled by site.py reload(sys) # to re-enable sys.setdefaultencoding() sys.setdefaultencoding('utf-8') schedule = "acna2019.json" print "Reading JSON ..." with open(schedule, 'r') as f: r = json.load(f) events = r['events'] speakers = r['speakers'] locations = r['metaData']['locations'] tracks = r['metaData']['tracks'] print "Outputting CSV ..." csv = open('schedule.csv', 'w') # Column headings csv.write('"Unique ID","Name","Description","Activity Code","Track","Tags (comma-separated)","Start Time","End Time","Location Name","Parent Activity Unique ID","Group List (comma-separated)","Live Q&A Enabled","Featured",') # speaker column headers i = 1 while i < 6: csv.write('"Speaker '+ str(i) +' Display Name","Speaker '+ str(i) +' First Name","Speaker '+ str(i) +' Last Name","Speaker '+ str(i) +' Role","Speaker '+ str(i) +' Title","Speaker '+ str(i) +' Bio","Speaker '+ str(i) +' Email One","Speaker '+ str(i) +' Email Two","Speaker '+ str(i) +' Organization Name",') i += 1 csv.write("\n") for event in events: for field in ('id','title','abstractText'): csv.write ('"' + event[field].replace("\n", "
") + '",') # Activity code? csv.write (',') # Track found = 0 if event['trackId']: for track in tracks: if track['id'] == event['trackId']: csv.write ('"' + track['names']['en'] + '",' ) found = 1 if found == 0: csv.write ('"General",' ); else: csv.write ('"General",' ); # Tags? csv.write (',') # Times for field in ('start','end'): csv.write('"' + event[field] + '",' ) # Location found = 0 if event['locationId']: # Does it have a locationID? for location in locations: if location['id'] == event['locationId']: csv.write ('"' + location['names']['en'] + '",' ) found = 1 if found == 0: csv.write ('"No Location Specified",') else: csv.write ('"No Location Specified",') # Other unused fields ... # "Parent Activity Unique ID","Group List (comma-separated)","Live Q&A Enabled","Featured" csv.write (',,,,') # Speakers # FIELDS # Speaker 1 Display Name,Speaker 1 First Name,Speaker 1 Last Name,Speaker 1 Role, # Speaker 1 Title,Speaker 1 Bio,Speaker 1 Email One,Speaker 1 Email Two,Speaker 1 Organization Name i = 0 while i < 6 : # How many speakers? if i < len( event['speakerIds'] ): # Are there that many speakers? for speaker in speakers: if speaker['id'] == event['speakerIds'][i]: # What fields? for field in ('name','firstname','lastname'): csv.write ('"' + speaker[field].replace("\n", "\\n" ) + '",') # role, title csv.write (',,') # Bio csv.write ('"' + speaker['bio'].replace("\n", "\\n") + '",' ) # email 1, email 2 csv.write (',,') csv.write ('"' + speaker['company'].replace("\n", "\\n") + '",' ) # There's not that many speakers else: csv.write (',,,,,,,,,') # Not really necessary, but what the heck i += 1 # Wend # Newline csv.write ("\n") # Barn door csv.close() print "Done!\n"