def get_sources(self): print("\t- Getting sources...", end=' ') done = 0 for source in self.sources: # Progress done += 1 print(f"\r\t- Getting sources... \t{done}/{len(self.sources)}", end='') playlist = parser.load(source['url']) if playlist is None: if source['last_online'] is not None: source['last_online'] = source['last_online'].strftime("%Y-%m-%d") continue # Updating sources source['count'] = len(playlist) source['last_online'] = datetime.now().date().strftime("%Y-%m-%d") # Adding urls to channels # for segment in playlist: # TODO: Refactor # Normalising name title, quality = Channel.fix_name(segment['title']) # Removing useless channels if title[0] in BANNED_BEGIN_NAME: continue # Searching channel in array found = False for channel in self.channels: if channel.name == title: found = True channel.add_url(segment['uri'], quality) break # Creating new if not found if not found: # Adding new theme theme_id = None theme = utils.fix_theme(segment['group_title']) if theme is not None: if theme in self.themes: theme_id = self.themes.index(theme) else: theme_id = self.DB.run('themes.add', theme=theme) while len(self.themes) < theme_id + 1: self.themes.append(None) self.themes[theme_id] = theme # Creating new channel self.channels.append( Channel(name=title, theme=theme_id, url=(segment['uri'], quality), source_id=source['id']) ) # Disconnecting from database self.DB.end() print("\r\t- Getting sources... Done!")
def test_normal(self): channels = parser.load( 'https://raw.githubusercontent.com/sstive/iptv/master/Tests/playlists/test_normal.m3u8' ) self.assertListEqual([{ 'title': 'Channel 1', 'uri': 'http://url1.test/ch1', 'group_title': 'Group 1' }, { 'title': 'Channel 2', 'uri': 'http://url1.test/ch2', 'group_title': 'Group 2' }, { 'title': 'Channel 3', 'uri': 'http://url1.test/ch3', 'group_title': '' }, { 'title': 'Channel 4', 'uri': 'http://url1.test/ch4', 'group_title': 'Group 4' }], channels)
def test_real(self): channels = parser.load('https://iptvmaster.ru/russia.m3u') print(channels)
def test_empty_m3u(self): channels = parser.load( 'https://raw.githubusercontent.com/sstive/iptv/master/Tests/playlists/test_empty.m3u8' ) self.assertListEqual([], channels)
def test_not_m3u(self): channels = parser.load('https://www.github.com/') self.assertIsNone(channels)
def test_url_not_found(self): channels = parser.load( 'http://www.hhjhjhakjjashjkjhkasfjhakfjh.asd/not_found') self.assertIsNone(channels)
def test_bad_url(self): channels = parser.load('bad_url') self.assertIsNone(channels)