def test_saving_new_channel(self): channel = Channel( title="Channel Title", url="https://www.youtube.com/channel/UCsn8UgBuRxGGqKmrAy5d3gA", ) channel.save() saved_channels = Channel.objects.all() self.assertEqual(saved_channels.count(), 1)
def parseHybrid(self, sds): xmltvCounter = 5000 for muxML in sds.getroot().findall("Multiplexer"): # create a mux if doesn't exist try: mux = DvbMux.objects.get(fec_hp = muxML.attrib['fec_hp'], fec_lp = muxML.attrib['fec_lp'], freq = int(muxML.attrib['freq']), modulation = muxML.attrib['modulation'], symbolRate = int(muxML.attrib['symbol_rate'])) except ObjectDoesNotExist: mux = DvbMux(fec_hp = muxML.attrib['fec_hp'], fec_lp = muxML.attrib['fec_lp'], freq = int(muxML.attrib['freq']), modulation = muxML.attrib['modulation'], symbolRate = int(muxML.attrib['symbol_rate'])) mux.save() # populate mux with channels for chML in muxML.findall("Service"): xmltvML = chML.find("XMLTV"); if xmltvML.attrib['id'] == '--': self.stderr.write("* Warn: auto-assign XMLTV ID %d to %s \n"% (xmltvCounter, chML.find('Name').attrib['value'])) xmltvID = xmltvCounter xmltvCounter += 1 else: xmltvID = int(xmltvML.attrib['id']); chML.remove(xmltvML) try: chan = Channel.objects.get(xmltvID=xmltvID) except ObjectDoesNotExist: chan = Channel() chan.xmltvID = xmltvID nameML = chML.find('Name'); chan.name = nameML.attrib['value']; chML.remove(nameML) lcnML = chML.find('LCN'); chan.lcn = int(lcnML.attrib['value']); chML.remove(lcnML) rateML = chML.find("Rating") if rateML != None: chan.mpaa = rateML.attrib['value'] chML.remove(rateML) else: chan.mpaa = u'G' chan.tune = ET.tostring(chML, encoding='utf-8') chan.mux = mux chan.mode = u'DVB' chan.chanType = chML.attrib['type'] try : chan.save() except Exception as e: self.stderr.write("Failed to save channel %s, reason %s\n" % (chan, e))
def parseIPTV(self, sds): lcnCount = 0 for chXML in sds.getroot().findall("BroadcastDiscovery/ServiceList/SingleService"): try: ch = {} ch['xmltvID'] = int(chXML.attrib['id']) ch['chanType'] = chXML.attrib['type'] mcast = chXML.find('ServiceLocation/IPMulticastAddress') ch['tune'] = "udp://%s:%s" % (mcast.get('Address'), mcast.get('Port')) ch['name'] = chXML.find('TextualIdentifier').get('ServiceName') ch['mpaa'] = chXML.find('Rating') if not ch['mpaa']: ch['mpaa'] = 'G' # by default - general audiences else: ch['mpaa'] = ch['mpaa'].get('value') except Exception as e: self.stderr.write("*** Failed to parse %s reason %s" % (ET.tostring(chXML), e)) continue lcnCount += 1 # we don't duplicate records if Channel.objects.filter(xmltvID=ch['xmltvID']).count() > 0: self.stderr.write(" Channel XMLTV ID=%d already in database, skipping\n" % ch['xmltvID']) continue chanDB = Channel(name = ch['name'], xmltvID = ch['xmltvID'], tune = ch['tune'], chanType = ch['chanType'], mpaa = ch['mpaa'], enabled = True, lcn = lcnCount, mode = u'IPTV', mux = None ) try: chanDB.save() except IntegrityError as e: print self.stderr.write(u"*** Failed to save channel [%d]%s, reason %s\n" % (chanDB.xmltvID, chanDB.name, e.__unicode__())) continue self.stdout.write(u"added [%d] \n" % chanDB.xmltvID)
def resetChannelList(hdid, channelList): device = Device.objects.get(hdid=hdid) Channel.objects.filter(device=device).delete() current_channel = '' channel_pattern = re.compile('SCANNING.*us-cable:(\d+)') program_pattern = re.compile('PROGRAM\s(\d+):\s(?![0])(.*)') for line in channelList: m = channel_pattern.match(line) if m: current_channel = m.group(1) else: m = program_pattern.match(line) if m: c = Channel(device=device, channel=int(current_channel), program=int(m.group(1)), desc=m.group(2)) c.save()
def import_file(channel_name, file): ''' Import csv file to db. ''' with open(file) as f: reader = csv.reader(f) try: channel = Channel.objects.get(name=channel_name) except Channel.DoesNotExist: channel = Channel(name=channel_name) channel.save() for row in reader: if row[0] != 'Category': categories = row[0].split(' / ') i = 0 category_objs = [] while i < len(categories): try: if i == 0: channel_category = ChannelCategory.objects.get( name=categories[i], channel=channel, parent_category=None) else: channel_category = ChannelCategory.objects.get( name=categories[i], channel=channel, parent_category=category_objs[i - 1]) except ChannelCategory.DoesNotExist: channel_category = ChannelCategory() channel_category.name = categories[i] channel_category.channel = channel if i != 0: channel_category.parent_category = category_objs[i - 1] channel_category.save() category_objs.append(channel_category) i += 1
def writeToDataBase(self): ch = Channel(name=self.title, country=self.country, subscriberCount=self.subscriberCount) ch.save()