Skip to content

Instantly share code, notes, and snippets.

@rennerocha
Last active August 29, 2015 13:56
Show Gist options
  • Save rennerocha/9222072 to your computer and use it in GitHub Desktop.
Save rennerocha/9222072 to your computer and use it in GitHub Desktop.
ics calendar for LHC
# -*- coding: utf-8 -*-
from datetime import datetime
from icalendar import Calendar, Event
import json
import os
import urllib2
if __name__ == '__main__':
LHC_ICS_FILE = '/home/hscps/hsc.tia.mat.br/lhc.ics'
try:
os.remove(LHC_ICS_FILE)
except OSError:
pass
GET_EVENTS_URL = 'https://lhc.net.br/w/api.php?action=query&list=allpages&apnamespace=100&format=json'
request = urllib2.urlopen(GET_EVENTS_URL)
content = json.loads(request.read())
cal = Calendar()
allpages = content['query']['allpages']
for page in allpages:
page_title = page['title']
start_title_idx = page_title.find(' ')
summary = page_title[start_title_idx:]
event_ns_date = page_title.split(' ')[0]
event_date = event_ns_date.split(':')[1]
year, month, day = event_date.split('/')
year, month, day = int(year), int(month), int(day)
event = Event()
event['uid'] = page['pageid']
event.add('summary', summary)
event.add('dtstart', datetime(year, month, day, 8, 0, 0))
event.add('dtend', datetime(year, month, day, 8, 0, 0))
cal.add_component(event)
with open(LHC_ICS_FILE, 'wb') as ics_file:
ics_file.write(cal.to_ical())
icalendar==3.6.1
python-dateutil==2.2
pytz==2013.9
requests==2.2.1
six==1.5.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment