How to pull a video file from JSON using Python 3.11?

111 views Asked by At

I need to pull a video file from this JSON object. For instance, I've two Episodes, and I want to pull Episode 1, and download that. How do I do that?

{"list":{"1":{"episode":1,"name":null,"uuid":"95cbd36e-789e-11ec-ae92-0242ac120002","created_timestamp":1571078646,"preview":null,"skips":{"opening":[],"ending":[]},"hls":{"fhd":"/videos/media/ts/8499/1/1080/9186f5d125c799f16965a9b3a38a2c42.m3u8","hd":"/videos/media/ts/8499/1/720/5a310e07f181fb31a7d9b69f7a62b4c8.m3u8","sd":"/videos/media/ts/8499/1/480/0fc60668a8713fbe55bb0cec4b7dc0d4.m3u8"}},"2":{"episode":2,"name":null,"uuid":"95cbd3e3-789e-11ec-ae92-0242ac120002","created_timestamp":1571722736,"preview":null,"skips":{"opening":[],"ending":[]},"hls":{"fhd":"/videos/media/ts/8499/2/1080/314fd5a58d5d08be401be695e5a37e03.m3u8","hd":"/videos/media/ts/8499/2/720/84ce5fdff2cb0fca90db72f900e94359.m3u8","sd":"/videos/media/ts/8499/2/480/4f4e483ac65fb1c24ea90dd1d053c128.m3u8"}}

I can't find any examples, or the person who knows how to do it.

My attempts to do something:

import requests

user_input = input("Enter anime title: ")

url = f'https://api.anilibria.tv/v3/title/search?search={user_input}'
res = requests.get(url)
res.raise_for_status()
data = res.json()

titles_and_links = [] 

for entry in data['list']:
    try:
        title = entry['names']['ru']  
        video_link = entry['episode'][0]['fhd']  

        data_item = {
            'title': title,
            'video_link': video_link
        }
        titles_and_links.append(data_item)
    except KeyError as e:
        print(f"Skipping entry due to missing data: {e}")


for item in titles_and_links:
    print("Title:", item['title'])
    print("Video Link:", item['video_link'])
    print()
1

There are 1 answers

1
Andrej Kesely On

IIUC, you want to get episode 1 from the dictionary named list (very unfortunate naming btw.):

import requests

user_input = input("Enter anime title: ")

url = f"https://api.anilibria.tv/v3/title/search?search={user_input}"
res = requests.get(url)
res.raise_for_status()
data = res.json()

titles_and_links = []

for entry in data["list"]:
    title = entry["names"]["ru"]
    e = entry["player"]["list"]["1"]  # <-- get first episode (`1`) from the `list`
    video_link = e["hls"]["fhd"]
    titles_and_links.append({"title": title, "video_link": video_link})


for item in titles_and_links:
    print("Title:", item["title"])
    print("Video Link:", item["video_link"])
    print()

Prints (for example):

Enter anime title: chan
Title: Девочка, которая видит это
Video Link: /videos/media/ts/9066/1/1080/84d1352caf37e1e74a85fe4b8ad0c3df.m3u8

Title: Дропкик злого духа 2
Video Link: /videos/media/ts/8599/1/1080/595f97d57725538be78cf227f5c31304.m3u8

Title: Дропкик злого духа
Video Link: None

Title: Дэми-тян хочет рассказать
Video Link: None

Title: Мой братик больше не братик!
Video Link: /videos/media/ts/9354/1/1080/e41695fa198835ea5fe8778d4200b53b.m3u8