Created
August 10, 2011 12:53
-
-
Save timo/1136732 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
# -*- coding:utf8 -*- | |
import urllib | |
import lxml.etree | |
from subprocess import Popen | |
rohdaten = urllib.urlopen("http://events.ccc.de/camp/2011/Fahrplan/schedule.en.xml").read() | |
#daten = unicode(rohdaten, "latin1") | |
daten = rohdaten | |
root = lxml.etree.fromstring(daten).getroottree() | |
SCRIPT_FULLPATH = "genPrerole.py" | |
EXTRA_ARGS = [ | |
#"-o", "/path/to/output", | |
#"-w", "/tmp/workdir" | |
] | |
def call_genPrerole(info): | |
command = ["python", SCRIPT_FULLPATH] | |
arguments = dict(t="title", | |
s="subtitle", | |
p="speakers", | |
d="date", | |
b="start", | |
i="id") | |
for flag, key in arguments.iteritems(): | |
flag = "-" + flag | |
try: | |
data = info[key] | |
except KeyError: | |
print "key %s does not exist for %s" % (key, info["id"]) | |
if data is None: | |
print "value %s was None for %s" % (key, info["id"]) | |
data = "" | |
if isinstance(data, list): | |
command += [flag] + data | |
elif isinstance(data, basestring): | |
command += [flag, data] | |
else: | |
raise ValueError("unexpected datatype") | |
command += EXTRA_ARGS | |
process = Popen(command) | |
returncode = process.wait() | |
if returncode != 0: | |
print "returncode %d != 0 for %r" % (returncode, command) | |
def daten_von_event(event, upper_info): | |
info = upper_info.copy() | |
info["id"] = event.get("id") | |
things = "start room slug title subtitle".split(" ") | |
for thing in things: | |
thing_tag = event.find(thing) | |
if thing_tag is None: | |
print "ERROR: no tag %s in %r" % (thing, event) | |
else: | |
if event.find(thing).text is not None: | |
info[thing] = event.find(thing).text | |
info["speakers"] = [] | |
persons = event.find("persons") | |
for person in list(persons): | |
info["speakers"].append(person.text) | |
call_genPrerole(info) | |
def daten_von_raum(raum, upper_info): | |
info = upper_info.copy() | |
info["name"] = raum.get("name") | |
for event in list(raum): | |
daten_von_event(event, info) | |
def daten_von_tag(tag): | |
info = {} | |
info["date"] = tag.get("date") | |
info["index"] = tag.get("index") | |
for raum in list(tag): | |
daten_von_raum(raum, info) | |
def daten_von_schedule(schedule): | |
for tag in schedule.xpath("day"): | |
daten_von_tag(tag) | |
daten_von_schedule(root) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment