Last active
December 14, 2015 01:28
-
-
Save jrmoserbaltimore/5006032 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
<xml> | |
<station> | |
<call>ktit</call> | |
<tz>America/New_York</tz> | |
<daypart> | |
<name>earlymorning</name> | |
<schedule> | |
<days>1,2,3,4,5</days> | |
<starttime>0500</starttime> | |
<endtime>0700</endtime> | |
</schedule> | |
</daypart> | |
<daypart> | |
<name>morning</name> | |
<schedule> | |
<days>1,2,3,4,5</days> | |
<starttime>0700</starttime> | |
<endtime>0900</endtime> | |
</schedule> | |
</daypart> | |
<daypart> | |
<name>latemorning</name> | |
<schedule> | |
<days>1,2,3,4,5</days> | |
<starttime>0900</starttime> | |
<endtime>1000</endtime> | |
</schedule> | |
</daypart> | |
<daypart> | |
<name>night</name> | |
<schedule> | |
<days>1,2,3,4,5,6,7</days> | |
<starttime>2100</starttime> | |
<endtime>220</endtime> | |
</schedule> | |
</daypart> | |
</station> | |
</xml> |
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
#!/bin/env python | |
from xml.dom import minidom | |
import sys | |
import datetime | |
class LiveStreamSchedule: | |
"""Class to load LiveStreamSchedule XML files""" | |
def __init__(self, filename=None): | |
self.stations = {} | |
self.__loadFile(filename) | |
self.__loadStations() | |
def __loadFile(self, filename): | |
self.XMLdoc = minidom.parse(filename); | |
def __loadStations(self): | |
"""Loads self.stations dictionary with each station, | |
referenced as self.stations[callsign]""" | |
for s in self.XMLdoc.getElementsByTagName('station'): | |
callsign = \ | |
s.getElementsByTagName('call')[0].firstChild.nodeValue | |
timezone = \ | |
s.getElementsByTagName('tz')[0].firstChild.nodeValue | |
self.stations[callsign] = {} | |
self.stations[callsign]["XMLdoc"] = s | |
self.stations[callsign]["timezone"] = timezone | |
self.__loadDayparts(s) | |
def __loadDayparts(self, station): | |
callsign = \ | |
station.getElementsByTagName('call')[0].firstChild.nodeValue | |
self.stations[callsign]["dayparts"] = {} | |
for d in station.getElementsByTagName('daypart'): | |
name = \ | |
d.getElementsByTagName('name')[0].firstChild.nodeValue | |
daypart = self.__loadSchedule(d) | |
self.stations[callsign]["dayparts"][name] = daypart | |
def __loadSchedule(self, daypart): | |
"""Creates array with days 1=monday 2=tuesday... elements [1:8] | |
each array contains a dictionary for start/end time""" | |
output = [] | |
for s in daypart.getElementsByTagName('schedule'): | |
days = \ | |
s.getElementsByTagName('days')[0].firstChild.nodeValue | |
days = days.split(",") | |
days = [ int(i) for i in days ] | |
starttime = \ | |
s.getElementsByTagName('starttime')[0].firstChild.nodeValue | |
endtime = \ | |
s.getElementsByTagName('endtime')[0].firstChild.nodeValue | |
for d in days: | |
output.insert (d, { | |
"day":d, | |
"starttime":starttime, | |
"endtime":endtime | |
} ) | |
return output | |
def daypartIsNow(daypart, station): | |
"""Returns True if the daypart is current""" | |
timezone = station["timezone"] | |
now = datetime.now(timezone) | |
begintime = datetime( | |
now.year, | |
now.month, | |
now.day, | |
int(daypart["starttime"][:2]), | |
int(daypart["starttime"][-2:], | |
None, None, | |
timezone) | |
endtime = datetime( | |
now.year, | |
now.month, | |
now.day, | |
int(d["endtime"][:2]), | |
int(d["endtime"][-2:], | |
None, None, | |
timezone) | |
if begintime < d and d < endtime: | |
return True | |
return False | |
def findCurrentDaypart(station): | |
"""Returns the current daypart {day: starttime: endtime: } dictionary | |
Stops when it finds the first--assumes no overlap""" | |
for (k, daypart) in station["dayparts"].items() | |
d = daypart[now.isoweekday()] | |
if d != None and daypartIsNow(d, station): | |
return k | |
return None | |
def getRecordingDaypart(callsign, station): | |
"""Connects to check what Live Streams are recording, and | |
particularly which for the current station. | |
Returns None if none, otherwise a text string for the | |
daypart""" | |
pass | |
def dvrDispatcher(callsign, station): | |
"""This is our dispatcher to take any needed action. | |
It assesses what should currently be recording; if anything | |
is recording; and what to do to bring these two in line.""" | |
currentDaypart = findCurrentDaypart(station) | |
recordingDaypart = getRecordingDaypart(callsign, station) | |
# All is well. | |
if currentDaypart == recordingDaypart: | |
return None | |
# Stop recording, rename files | |
if recordingDaypart != None: | |
stopRecording(callsign, station, recordingDaypart) | |
renameRecordedFiles(callsign, station, recordingDaypart) | |
# Begin recording | |
if currentDaypart != None | |
def printParsedStation(callsign, station): | |
"""Walks the station's data structure and prints to output | |
Essentially, a unit test to make sure the parser works""" | |
print "Callsign:", callsign | |
print "Timezone:", station["timezone"] | |
for (k, daypart) in station["dayparts"].items(): | |
print " " + k | |
for i in daypart: | |
print " " + str(i["day"]), \ | |
"Start:", i["starttime"], \ | |
"End:", i["endtime"] | |
def processLiveStream(path): | |
"""Process the XML file""" | |
schedule = LiveStreamSchedule(path) | |
for (callsign, station) in schedule.stations.items(): | |
printParsedStation(callsign, station) | |
#dvrDispatcher(callsign, station) | |
for i in sys.argv[1:]: | |
processLiveStream(i) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment