-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
181 lines (159 loc) · 6.28 KB
/
main.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import os
import subprocess
import sys
from time import localtime, strftime, gmtime
import yt_dlp
from internetarchive import get_item, upload
def get_twitch_info(link: str, cookies: str) -> dict:
ydl = yt_dlp.YoutubeDL({"extract_flat": "in_playlist", "quiet": True})
if cookies is not None:
ydl.params["cookiefile"] = cookies
result = ydl.extract_info(link, download=False)
return result
def get_vods(username: str, cookies: str) -> list:
link = f"https://www.twitch.tv/{username}/videos"
result = get_twitch_info(link, cookies)
all_videos = []
for entry in result["entries"]:
all_videos.append(entry)
return all_videos
def check_identifier_exists(identifier):
try:
item = get_item(identifier)
return item.exists
except Exception as e:
print(f"Error: {e}")
return False
def clear_dir(temp_dir: str) -> None:
files = os.listdir(temp_dir)
for file in files:
os.remove(os.path.join(temp_dir, file))
def main():
current_dir = os.getcwd()
streamers = sys.argv[1].split(",")
cookies = None
twitch_downloader_cli = None
files = os.listdir(current_dir)
if not any("TwitchDownloaderCLI" in file for file in files):
print("TwitchDownloaderCLI not found")
sys.exit(1)
for file in files:
if "TwitchDownloaderCLI" in file:
twitch_downloader_cli = os.path.abspath(os.path.join(current_dir, file))
break
if not "ffmpeg" in os.environ:
subprocess.run([twitch_downloader_cli, "ffmpeg", "-d"])
if any("cookies" in file for file in files):
for file in files:
if "cookies" in file:
cookies = file
break
temp_dir = os.path.join(current_dir, "data")
if not os.path.exists(temp_dir):
os.makedirs(temp_dir)
for streamer in streamers:
print("Getting vods of " + streamer)
vods = get_vods(streamer, cookies)
for vod in vods:
vod_id = vod["id"][1:]
print(f"Checking if vods exists : {vod_id}")
identifier = f"TwitchVod-{vod_id}"
if check_identifier_exists(identifier):
print("Skipping vod since it already exists in internet archive")
continue
print("\t Getting vod info...")
vod_info = get_twitch_info(vod["url"], cookies)
if vod is None:
print("Failed to get vod info")
continue
if vod_info["is_live"] == True:
print("Skipping vod since it is live")
continue
subprocess.run([twitch_downloader_cli, "cache", "--force-clear"])
files = os.listdir(temp_dir)
for file in files:
os.remove(os.path.join(temp_dir, file))
print("Downloading Chat...")
chat_file = os.path.join(temp_dir, f"{vod_id}.json")
try:
subprocess.run([
f"{twitch_downloader_cli}",
"chatdownload",
"--id",
vod_id,
"--embed-images",
"--bttv=true",
"--ffz=True",
"--stv=true",
"-o",
chat_file,
"--compression",
"Gzip",
"--banner",
"false",
"-t",
"2",
])
chat_file = chat_file + ".gz"
except Exception as e:
print(f"Failed to download chat: {e}")
clear_dir(temp_dir)
continue
print("Downloading Vod...")
livestream_file = os.path.join(temp_dir, f"{vod_id}.mp4")
yt_opts = {
"format": "best",
"outtmpl": livestream_file,
"tmpdir": temp_dir,
"noplaylist": True,
"retries": 10,
}
if cookies is not None:
yt_opts["cookiefile"] = cookies
try:
with yt_dlp.YoutubeDL(yt_opts) as ydl:
ydl.download([vod_info["url"]])
except yt_dlp.utils.DownloadError as e:
clear_dir(temp_dir)
print(
f"Failed to download vod {vod['id']}. URL: https://www.twitch.tv/videos/{vod['id']}"
)
continue
except Exception as e:
clear_dir(temp_dir)
print(
f"Failed to download vod {vod['id']}. URL: https://www.twitch.tv/videos/{vod['id']}"
)
continue
md = {
"title": vod_info["fulltitle"],
"mediatype": "movies",
"creator": vod_info["uploader_id"],
"description": "\n".join(
[
f"{strftime('%H:%M:%S', gmtime(chapter['start_time']))} - {chapter['title']}"
for chapter in vod_info["chapters"]
]
),
"date": strftime("%Y-%m-%d", localtime(vod_info["epoch"])),
"subject": ["Twitch", "Twitch Vod", "Twitch Chat"],
"language": "eng",
"game": list(set(chapter["title"] for chapter in vod_info["chapters"])),
}
files = [livestream_file, chat_file]
print("Uploading...")
r = upload(
identifier, files=files, metadata=md, request_kwargs={"timeout": 600}
)
if r[0].status_code == 200:
print(
f"Successfully uploaded {vod_id}. URL: https://www.twitch.tv/videos/{vod_id}. Internet Archive URL: https://archive.org/details/{identifier}"
)
else:
print(
f"Failed to upload {vod_id}. URL: https://www.twitch.tv/videos/{vod_id}"
)
for file in files:
os.remove(file)
if __name__ == "__main__":
main()