def test_numbering(): ob = Playlist(short_test_pl) ob.populate_video_urls() gen = ob._path_num_prefix_generator(reverse=False) assert '1' in next(gen) assert '2' in next(gen) ob = Playlist(short_test_pl) ob.populate_video_urls() gen = ob._path_num_prefix_generator(reverse=True) assert str(len(ob.video_urls)) in next(gen) assert str(len(ob.video_urls) - 1) in next(gen) ob = Playlist(long_test_pl) ob.populate_video_urls() gen = ob._path_num_prefix_generator(reverse=False) nxt = next(gen) assert len(nxt) > 1 assert '1' in nxt nxt = next(gen) assert len(nxt) > 1 assert '2' in nxt ob = Playlist(long_test_pl) ob.populate_video_urls() gen = ob._path_num_prefix_generator(reverse=True) assert str(len(ob.video_urls)) in next(gen) assert str(len(ob.video_urls) - 1) in next(gen)
def downloadList(url, maxCount=None, start=None, end=None): print("download Youtube playlist:%s, maxCount:%s" % (url, str(maxCount))) # taskCount = cpu_count() -1 # print("we have %d cpus" % (taskCount + 1)) taskCount = DOWNLOAD_TASK_CUNT pl = Playlist(url) pl.populate_video_urls() videoUrls = pl.video_urls if maxCount: videoUrls = videoUrls[0:maxCount:1] elif start and end: videoUrls = videoUrls[start - 1:end] elif start and end is None: videoUrls = videoUrls[start - 1::] elif start is None and end: videoUrls = videoUrls[:end:] prefix_gen = pl._path_num_prefix_generator() playlistTitle = getPlaylistTitle(pl.construct_playlist_url()) #single thread # for link in videoUrls: # prefix = next(prefix_gen) # print('file prefix is: %s' % prefix) # downloadSingle(link, filename_prefix=prefix, subFolder=playlistTitle) # multiple thread argsArrayList = [] for i in range(0, taskCount): argsArrayList.append([]) i = 0 for link in videoUrls: idx = i % taskCount i += 1 prefix = next(prefix_gen) argsArrayList[idx].append((link, prefix, playlistTitle)) s_linkStatusDic[link] = False downloadListMultipleThread(argsArrayList) times = 1 while hasToDownloadTask(): times += 1 toDownloadFileDic = { k: v for k, v in s_linkStatusDic.items() if v == False } print("=>try %d times, file to download count: %d" % (times, len(toDownloadFileDic))) print(" %s", str(toDownloadFileDic)) downloadListMultipleThread(argsArrayList) print("all download task done.")