def test_check_sources_random(self): iterations = 0 animes = get_animes() while iterations <= MAX_SOURCE_TEST_ITERATION: iterations += 1 random_anime = random.choice(animes) current_sources = get_sources(random_anime) if current_sources: source = random.choice(current_sources) url = urljoin(TWIST_CDN_URL, source.source) try: client = get_auth_client() r = client.get( url, headers={ **dict(client.headers), "referer": TWIST_URL, "range": "bytes=0-10", }, ) r.raise_for_status() except httpx.HTTPError as e: print( f"Error on iteration n°{iterations} for {random_anime.full_title()}\n\tepisode={source.number}" f"{e}\n") continue print( f"Found source for {random_anime.full_title()}\n\tepisode={source.number}\n\turl={r.url}" ) break assert iterations <= 100
def download( slug: str = typer.Argument(None, help=ANIME_SLUG_HELP, callback=select_anime_slug), directory: str = typer.Option( CWD_DIR, "--d", help="Directory where files will be uploaded"), nfrom: int = typer.Option( None, "--nfrom", help="Select episodes greater or equal to the given number"), nto: int = typer.Option( None, "--nto", help="Select episodes lesser or equal to the given number"), dfrom: datetime.datetime = typer.Option( None, "--dfrom", help="Select episodes uploaded after the given date"), dto: datetime.datetime = typer.Option( None, "--dto", help="Select episodes uploaded before the given date"), ): """ Download a list of episodes from a given anime slug The output directory can be specified """ directory = Path(directory) animes = get_animes() animes_by_slug = {anime.slug.slug: anime for anime in animes} try: anime = animes_by_slug[slug] except KeyError: raise typer.BadParameter(invalid_slug_message(slug=slug, animes=animes)) sources = get_sources(anime) filtered_source_ids = set(source.id for source in sources) if nfrom: if type(nfrom) is int: filtered_source_ids -= set(source.id for source in sources if source.number < nfrom) if dfrom: filtered_source_ids -= set(source.id for source in sources if source.created_at < dfrom) if nto: filtered_source_ids -= set(source.id for source in sources if source.number > nto) if dto: filtered_source_ids -= set(source.id for source in sources if source.created_at > dto) sources = list( sorted( (source for source in sources if source.id in filtered_source_ids), key=lambda s: s.number, )) if not sources: typer.secho("No sources to download ! ", bold=True) raise typer.Exit(code=1) typer.echo(download_starting_message(sources=sources, directory=directory)) for source in sources: typer.echo( f"{anime.title} - S{anime.season:02d} - E{source.number:02d}") title_slug = slugify(anime.title) current_dir = directory / title_slug if not current_dir.exists(): current_dir.mkdir(parents=True, exist_ok=True) ext = source.source.rsplit(".", 1)[-1] filepath = ( current_dir / f"{title_slug}-S{anime.season:02d}-E{source.number:03d}.{ext}") download_source(source, filepath)
def test_get_sources(self, anime): sources = get_sources(anime) assert len(sources) == 209 assert sources[0].source == "/anime/narutoold/naruto-001.mp4"