59
59
print ("CRITICAL: Non-HVIB compliant videoInfoBackend.")
61
61
raise PlayListVIBNotSetException()
63
63
#Verify it matches required version
64
64
if not videoInfoBackend.HVIB_API_VERSION >= 1:
65
65
print ("CRITICAL: Incompatible HVIB")
67
67
raise PlayListVIBNotSetException()
69
69
self.__videoInfoBackend = videoInfoBackend
70
70
self._playList = []
73
73
def __createBlock(self, id = 0, runDate = 0, runTime = 0, name = "DefaultBlockName" , description = ""):
80
80
block['description'] = description
81
81
block['parts'] = []
84
def __createPart(self, resource):
84
def __createPart(self, resource):
87
87
#The video file name
88
88
part['file'] = resource['file']
89
89
part['name'] = resource['name']
91
91
#Duration in seconds
92
92
part['duration'] = self.__videoInfoBackend.HVIB_RunningTime(part['file'])
94
94
#Will hold when to play the file as a datetime format (yyyymmddhhiiss)
98
98
def load(self, playListFile):
99
99
'''Load the playlist file into memory'''
101
# Get the playlist file
102
playListFile = playListFile + "/playlist.json"
103
self.logger.debug("Loading playlist from %s" % (playListFile,))
102
105
#Verify the playlist file exists
103
106
if not os.path.exists(playListFile):
104
107
self.logger.critical("Playlist file not found.");
105
108
raise PlayListNotFoundException()
107
110
#Attempt to parse the playlist file
109
112
playListStruct = json.load(open(playListFile, "r"))
112
115
print("This is a parsing error. Strings in json must be delimited with \" instead of ' ")
113
116
raise PlayListImportErrorException()
115
118
#Verify that some data exists
116
119
for elem in ["blocks","resources"]:
117
120
if not elem in playListStruct:
118
121
print(elem + " not found")
119
122
raise PlayListImportErrorException()
122
125
#Parse the resources
123
126
for resource in playListStruct['resources']:
124
127
if not 'file' in playListStruct['resources'][resource]:
125
128
print("CRITICAL: Resource "+ str(resource) +" has no file attribute");
126
129
raise PlayListImportErrorException()
128
131
if not 'file' in playListStruct['resources'][resource]:
129
132
print("CRITICAL: Resource "+ resource +" does not have a file attribute!")
130
133
raise PlayListImportErrorException()
132
135
if not os.path.exists(playListStruct['resources'][resource]['file']):
133
136
print("CRITICAL: Resource " + str(resource) + " file ("+ playListStruct['resources'][resource]['file'] +") does not exists")
134
137
raise PlayListImportErrorException()
138
141
for dateBlock in playListStruct["blocks"]:
140
143
prev_block_id = 0
142
145
for timeBlock in playListStruct["blocks"][dateBlock]:
143
146
blockStruct = playListStruct["blocks"][dateBlock][timeBlock]
145
148
if len(blockStruct['parts']) == 0:
146
149
print("WARNING: Skipping block " + str(timeBlock) + " on " + str(dateBlock) + ". No parts to play")
149
152
for item in ['name','description', 'parts']:
150
153
if not item in blockStruct:
151
154
print ("CRITICAL: Block " + timeBlock +" on " + dateBlock +" does not have a " + item + "!")
152
155
raise PlayListImportErrorException()
155
158
#blockid is date with seconds, move to unix timestamp
156
159
blockId = ((int(dateBlock) * 10000) + int(timeBlock)) * 100
157
160
blockId = datetime.datetime.strptime(str(blockId), "%Y%m%d%H%M%S")
158
161
blockId = int(time.mktime(blockId.timetuple()))
160
163
block = self.__createBlock(blockId,dateBlock,timeBlock,blockStruct["name"],blockStruct["description"])
161
164
for part in blockStruct['parts']:
163
166
if not part in playListStruct['resources']:
164
167
print("CRITICAL: Part " + str(part) + " has no resource file!")
165
168
raise PlayListImportErrorException()
167
170
resource = playListStruct['resources'][part]
168
171
part = self.__createPart(resource)
170
part['playAt'] = blockId + block['totalRunTime']
173
part['playAt'] = blockId + block['totalRunTime']
171
174
block['parts'].append(part)
172
175
block['totalRunTime'] += part['duration']
174
177
if prev_block_id > blockId:
175
178
print("Critical: Block duration overlapping at " + str(blockId))
176
179
raise PlayListImportErrorException()
178
181
self._playList.append(blockId)
179
182
self._blocks[blockId] = block
181
print ("INFO: Loaded " + str(len(self._playList)) + " blocks.")
184
print ("INFO: Loaded " + str(len(self._playList)) + " blocks.")
182
185
self._playList.sort()
184
187
def getCurrentBlock(self):
185
188
curTimeId = int(time.time())
188
191
for blockId in self._playList:
189
192
if blockId < curTimeId:
190
193
curBlock = blockId
194
197
curBlock = blockId
195
return self._blocks[curBlock]
198
return self._blocks[curBlock]
198
201
if __name__ == "__main__":