This repository has been archived by the owner on Aug 1, 2023. It is now read-only.
forked from turicfr/nick-downloader
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnick_web.py
107 lines (88 loc) · 4.32 KB
/
nick_web.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import json
import os
from re import search
import requests
import youtube_dl
from bs4 import BeautifulSoup
subtitle_type = "ttml" # ttml | vtt | both
domain = ""
output_dir = ""
class Episode(object):
def __init__(self, MGID):
self._download_item(self.get_data(MGID), self.save_information())
def get_data(self, mgid):
data = requests.get(
"https://media.mtvnservices.com/pmt/e1/access/index.html?uri=mgid:arc:episode:nick.intl:{}&configtype=edge".format(
mgid), headers={"Referer": link}).json()
self.title = data["feed"]["title"].replace("/", " ")
self.description = data["feed"]['description']
self.image_url = data["feed"]["image"]["url"]
self.airDate = data["feed"]["items"][0]['airDate']
self.duration = data["feed"]["items"][0]['duration']
self.show_title = data["feed"]["items"][0]['title']
self.episode_number = data["feed"]["items"][0]['group']["category"]['episodeN'][-2:]
self.season_number = data["feed"]["items"][0]['group']["category"]['seasonN']
if len(self.season_number) < 2:
self.season_number = "0" + self.season_number
self.linearPubDate = data["feed"]["items"][0]['group']["category"]['linearPubDate']
self.language = data["feed"]["items"][0]['group']["category"]['language']
self.country = data["feed"]["items"][0]['group']["category"]['countryName']
return str(data["feed"]["items"][0]["group"]["content"]).replace("&device={device}",
"") + "&format=json&acceptMethods=hls"
def save_information(self):
dirname = os.path.join(output_dir, self.show_title + " " + self.country, "Season " + self.season_number)
if not os.path.isdir(dirname):
os.makedirs(dirname)
title = "{show} - S{season}E{episode} - {title}".format(show=self.show_title, season=self.season_number,
episode=self.episode_number, title=self.title)
return os.path.join(dirname, title)
@staticmethod
def _download_item(url, output):
item = requests.get(url, params={
"acceptMethods": "hls",
"format": "json",
}).json()["package"]["video"]["item"][0]
try:
src = item["rendition"][-1]["src"]
except KeyError:
print("error while getting episode")
return
if "transcript" in item:
if subtitle_type == "both":
for f in range(2):
sub_type = ["ttml", "vtt"]
subtitles = next(i for i in item["transcript"][0]["typographic"] if i["format"] == sub_type[int(f)])
with open(f"{output}." + sub_type[f], "w", encoding="utf-8") as file:
file.write(requests.get(subtitles["src"]).text)
else:
subtitles = next(i for i in item["transcript"][0]["typographic"] if i["format"] == subtitle_type)
with open(f"{output}." + subtitle_type, "w", encoding="utf-8") as file:
file.write(requests.get(subtitles["src"]).text)
ydl_opts = {'format': 'best',
"outtmpl": output + ".%(ext)s",
"fixup": "detect_or_warn",
"hls_prefer_native": True,
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([src])
def get_MGID():
soup = BeautifulSoup(requests.get(link).text, "html.parser")
return json.loads(soup.find(type="application/ld+json").contents[0])["@id"]
def Season(season_link):
episodes = []
soup = BeautifulSoup(requests.get(season_link).text, "html.parser")
for episode in soup.find_all(class_="item full-ep css-q2f74n-Wrapper e19yuxbf0"):
# print(episode.contents[0]["href"])
episodes.append("https://" + domain + episode.contents[0]["href"])
return episodes
Input_links = []
in_link = input("Video or Series:")
Input_links.append(in_link)
for input_link in Input_links:
domain = search("(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]", input_link).group(0)
# check input list
if "show" in input_link:
for link in Season(input_link):
Episode(get_MGID())
else:
Episode(get_MGID())