-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtver-downloader.py
executable file
·170 lines (134 loc) · 5.61 KB
/
tver-downloader.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
#!/usr/pkg/bin/python3.11
# Copyright (c) 2022 Ryo ONODERA <[email protected]>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# Generate titles list from URLs and download movies with the title from TVer.jp
#
# Install:
# devel/py-requests
# net/yt-dlp
# security/py-cryptodome
# multimedia/ffmpeg5
import requests
import json
import time
import datetime
import urllib
import subprocess
import sys
import argparse
import pathlib
ytdlPath = '/usr/pkg/bin/yt-dlp'
ffmpegPath = '/usr/pkg/bin/ffmpeg5'
maxFilenameLength = 220
tverVideoBase = 'https://tver.jp/episodes/'
tverApiServer = 'https://platform-api.tver.jp'
tverAccessTokensURL = tverApiServer + '/v2/api/platform_users/browser/create'
tverSearchURL = tverApiServer + '/service/api/v1/callKeywordSearch'
def getTverTokens():
response = requests.post(tverAccessTokensURL, data='device_type=pc',
headers={'Content-Type': 'application/x-www-form-urlencoded'})
return response.json()['result']
def getTverSearchResults(query):
print("query:", query)
encodedQuery = urllib.parse.quote(query)
accessTokens = getTverTokens()
platformUid = accessTokens['platform_uid']
platformToken = accessTokens['platform_token']
searchURL = tverSearchURL + '?platform_uid=' + platformUid + \
'&platform_token=' + platformToken + \
'&require_data=later&keyword=' + encodedQuery
try:
response = requests.get(searchURL, headers={'x-tver-platform-type': 'web'})
results = response.json()['result']['contents']
except Exception:
print('Trying again in 5 seconds...')
time.sleep(5)
response = requests.get(searchURL, headers={'x-tver-platform-type': 'web'})
results = response.json()['result']['contents']
return results
def getTverVideoURLs(query):
URLs = []
results = getTverSearchResults(query)
for result in results:
# Accept search with title not omly seriesTitle.
# This is a workaround for too short seriesTitle.
if query in result['content']['seriesTitle'] or query in result['content']['title']:
URLs.append(tverVideoBase + result['content']['id'])
return URLs
def getVideoTitle(URL):
episodeId = pathlib.PurePath(urllib.parse.urlparse(URL).path).name
infoURL = 'https://statics.tver.jp/content/episode/' + episodeId + '.json'
response = requests.get(infoURL)
if response.status_code == requests.codes.ok:
json = response.json()
rawTitle = json['share']['text']
title = rawTitle.replace('\n#TVer', '')
return title
else:
return 'ERROR'
def writeTverTitles(URLsFilename, targetFilename):
titles = []
URL = []
with open(URLsFilename, 'r') as file:
URLs = file.read().splitlines()
for URL in URLs:
titles.append(getVideoTitle(URL))
with open(targetFilename, 'w', encoding='utf-8', newline='\n') as tf:
tf.write('\n'.join(titles) + '\n')
def getCommandResponse(command):
return subprocess.Popen(command, stdout=subprocess.PIPE,
shell=True).communicate()
def getCommandRetVal(command):
return subprocess.Popen(command, stdout=None,
shell=True).wait()
def downloadTverVideo(URL):
command = ytdlPath + ' --get-filename ' + URL
filenameBytes = getCommandResponse(command)[0].strip()
trimmedFilenameBytes = filenameBytes[0:maxFilenameLength]
filenameShort = trimmedFilenameBytes.decode(encoding='utf-8', errors='ignore').replace('.mp4', '').replace('#', '#') + '.mp4'
command = ytdlPath + ' --ffmpeg-location ' + ffmpegPath + ' -o "' + filenameShort + '" --concurrent-fragments 3 ' + URL
ret = getCommandRetVal(command)
if ret == 0:
return
return
def downloadTverVideos(titleFilename):
title = []
with open(titleFilename, 'r') as file:
titles = file.read().splitlines()
for title in titles:
print(title)
URLs = getTverVideoURLs(title)
for URL in URLs:
print(URL)
downloadTverVideo(URL)
if __name__ == '__main__':
argParser = argparse.ArgumentParser(description='Get movie titles and download movies with the titles from TVer.jp')
argParser.add_argument('--gentitle', help='Generate titles from URLs in GENTITLE file', action='store')
argParser.add_argument('title_filename', help='Input titles file generated by --genfile option, in --gentitle case, this means output target filename', action='store')
args = argParser.parse_args()
if args.gentitle != None:
writeTverTitles(args.gentitle, args.title_filename)
else:
downloadTverVideos(args.title_filename)