def getStream(self, url): headers = self.getHeaders() r = self.session.get(url, headers = headers) if r.status_code == 500: log('({}) {} returns {} status. Signaling authorization failure'\ .format('getStream', url, r.status_code), True) raise CBCAuthError('getStream', False) elif r.status_code == 401: raise CBCAuthError('getStream', False) elif r.status_code == 402: raise CBCAuthError('getStream', True) elif not r.status_code == 200: log('(getStream) {} returns {} status code'.format(url, r.status_code), True) return None saveCookies(self.session.cookies) dom = parseString(r.content) status = dom.getElementsByTagName('status')[0].firstChild.nodeValue if not status == 'success': log('ERROR: {} has status of {}'.format(url, status), True) return None return { 'url': dom.getElementsByTagName('url')[0].firstChild.nodeValue }
def getLivePrograms(self): r = self.session.get(self.LIST_URL) if not r.status_code == 200: log('ERROR: {} returns status of {}'.format(url, r.status_code), True) return None saveCookies(self.session.cookies) streams = [] items = json.loads(r.content)[self.LIST_ELEMENT] return items
def authorize(self, username=None, password=None, callback=None): full_auth = not username == None and not password == None r = self.session.get(self.IDENTITIES_URL) if not callback == None: callback(20 if full_auth else 50) if not r.status_code == 200: log( 'ERROR: {} returns status of {}'.format( self.IDENTITIES_URL, r.status_code), True) return None dom = parseString(r.content) reg_url = dom.getElementsByTagName( 'registerDeviceUrl')[0].firstChild.nodeValue login_url = dom.getElementsByTagName( 'loginUrl')[0].firstChild.nodeValue auth = self.registerDevice(reg_url) if not callback == None: callback(40 if full_auth else 100) if auth == None: log('Device registration failed', True) return False if full_auth: token = self.radiusLogin(username, password) if not callback == None: callback(60) if token == None: log('Radius Login failed', True) return False jwt = self.radiusJWT(token) if not callback == None: callback(80) if jwt == None: log('Radius JWT retrieval failed', True) return False token = self.login(login_url, auth['devid'], jwt) if not callback == None: callback(100) if token == None: log('Final login failed', True) return False auth['token'] = token saveAuthorization(auth) saveCookies(self.session.cookies) return True
def getShows(self, url=None, offset=0, progress_callback=None): """ Get a list of all shows. Actual shows (not menus) will be items with a <clearleap:itemType>media</clearleap:itemType> tag. Menus are itemType LEAF. """ headers = self.getHeaders() show_url = self.SHOW_LIST_URL if url == None else url if offset > 0: show_url += '?offset={}'.format(offset) r = self.session.get(show_url, headers=headers) if r.status_code == 401 or r.status_code == 500: log('({}) {} returns {} status. Signaling authorization failure'\ .format('getShows', show_url, r.status_code), True) raise CBCAuthError('getShows', False) elif not r.status_code == 200: log('(getShows) {} returns {} status'.format(url, r.status_code), True) return None saveCookies(self.session.cookies) dom = parseString(r.content) statuses = dom.getElementsByTagName('status') if len(statuses) > 0: if not statuses[0].firstChild.nodeValue == 'success': log('Error: unsuccessful retrieval of media', True) return None return dom.getElementsByTagName('url')[0].firstChild.nodeValue results = [] items = dom.getElementsByTagName('item') for item in items: tags = self.getShow(item) results.append(tags) # figure out how many pages total = dom.getElementsByTagName('clearleap:totalResults') progress = len(results) + offset if len(total) > 0: total_shows = int(total[0].firstChild.nodeValue) if progress_callback: progress_callback(100 * progress / total_shows) if total_shows > len(results) + offset: next_results = self.getShows(url, offset + len(results), progress_callback) results.extend(next_results) return results
def parseSmil(self, smil): r = self.session.get(smil) if not r.status_code == 200: log('ERROR: {} returns status of {}'.format(url, r.status_code), True) return None saveCookies(self.session.cookies) dom = parseString(r.content) seq = dom.getElementsByTagName('seq')[0] video = seq.getElementsByTagName('video')[0] src = video.attributes['src'].value title = video.attributes['title'].value abstract = video.attributes['abstract'].value return src
def registerDevice(self, url): r = self.session.post(url, data=self.DEVICE_XML_FMT) if not r.status_code == 200: log('ERROR: {} returns status of {}'.format(url, r.status_code), True) return None saveCookies(self.session.cookies) # Parse the authorization response dom = parseString(r.content) status = dom.getElementsByTagName('status')[0].firstChild.nodeValue if status != "Success": log('Error: Unable to authorize device', True) return None auth = { 'devid': dom.getElementsByTagName('deviceId')[0].firstChild.nodeValue, 'token': dom.getElementsByTagName('deviceToken')[0].firstChild.nodeValue } return auth