- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscraper_radiko.py
More file actions
Latest commit
193 lines (169 loc) · 8.03 KB
/
Copy pathscraper_radiko.py
File metadata and controls
193 lines (169 loc) · 8.03 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
fromurllib.parseimporturlparse, parse_qs
importrequests
importre
frompathlibimportPath
importconcurrent.futures
importrequests
fromsubprocessimportrun, DEVNULL
fromutilimportdownload
classRadikoExtractor():
def__init__(self, url, save_dir='.', *args, **kwargs):
self.url=url
try:
self.host=urlparse(url).hostname
except:
self.host=''
self.save_dir=save_dir
forkeyinkwargs:
setattr(self, key, kwargs[key])
defparse(self, *args, **kwargs):
importbase64
importxml.etree.ElementTreeasET
fromdatetimeimportdatetime, timedelta
fromhttp.cookiejarimportMozillaCookieJar
importtime
importpytz
tz=pytz.timezone('Asia/Tokyo')
s=requests.Session()
cookie_file=Path('cookies.txt') # for premium account
ifcookie_file.exists():
print(f"Found cookie file at {cookie_file}. Load...")
cj=MozillaCookieJar(cookie_file)
cj.load(ignore_expires=True,ignore_discard=True)
forcookieincj:
ifcookie.expires==0:
cookie.expires=int(time.time()+86400)
s.cookies=cj
#http://www.joqr.co.jp/timefree/mss.php
#http://radiko.jp/share/?sid=QRR&t=20200822260000
#http://radiko.jp/#!/ts/QRR/20200823020000
is_joqr_timefree=False
ifre.search(r'joqr\.co\.jp/timefree/', self.url):
is_joqr_timefree=True
r=requests.get(self.url)
self.url=re.search(r'<META.+URL=(.+)">', r.text)[1]
ifm:=re.search(r'/ts/(.+)/(\d{8})(\d+)$', self.url):
station=m[1]
date=m[2]
time=m[3]
elifre.search(r'/share/', self.url):
qs=parse_qs(urlparse(self.url).query)
station=qs['sid'][0]
t=qs['t'][0]
date=t[0:8]
time=t[8:]
else:
print('URL format invalid.')
return
# 节目单XML的日期是30小时制(实质上是29小时:深夜节目计算为前一天,早晨5点起为新的一天),但是里面的metadata是正常24小时制。
# 然后上述两种URL又分别用两种时间制度(share是29小时,/ts/是24小时)所以这里转换下。
# “natural_date”指的是29小时制时的日期(找不到更好的名字了),date和time都是24小时制。
hr=int(time[0:2])
natural_date=date
ifhr<5:
natural_date= (datetime.strptime(date, '%Y%m%d') -timedelta(days=1)).strftime('%Y%m%d')
ifhr>=24:
date= (datetime.strptime(date, '%Y%m%d') +timedelta(days=1)).strftime('%Y%m%d')
time='{:02d}'.format(hr-24) +time[2:]
date_time_obj=tz.localize(datetime.strptime(date+time, '%Y%m%d%H%M%S'))
now_obj=datetime.now().astimezone(tz)
if (now_obj-date_time_obj) >timedelta(days=7) andis_joqr_timefree:
print('[Warning] the link is more than 1 week old. Likely it\'s already expired. Automatically change to next week...')
date_time_obj=date_time_obj+timedelta(days=7)
natural_date= (datetime.strptime(natural_date, '%Y%m%d') +timedelta(days=7)).strftime('%Y%m%d')
prog_list=s.get(f'http://radiko.jp/v3/program/station/date/{natural_date}/{station}.xml')
prog_list.encoding='utf-8'
root=ET.fromstring(prog_list.text)
title=''
forproginroot[2][0][1]:
ifprog.tag=='prog':
ifint(prog.attrib['ft']) <=int(date_time_obj.strftime('%Y%m%d%H%M%S')) <int(prog.attrib['to']):
print('Find the program!', prog[0].text)
title=prog[0].text
ft=prog.attrib['ft']
to=prog.attrib['to']
break
ifnottitle:
print('Failed to find the program from the list.')
return
headers= {
'x-radiko-device': 'pc',
'x-radiko-app-version': '0.0.1',
'x-radiko-user': 'dummy_user',
'x-radiko-app': 'pc_html5'
}
auth1=s.get('https://radiko.jp/v2/api/auth1', headers=headers)
auth_token=auth1.headers['X-Radiko-AuthToken']
key_length=int(auth1.headers['X-Radiko-KeyLength'])
key_offset=int(auth1.headers['X-Radiko-KeyOffset'])
key='bcd151073c03b352e1ef2fd66c32209da9ca0afa'#hard-coded key
partial_key=key[key_offset: key_offset+key_length]
partial_key_b64=base64.b64encode(bytes(partial_key, 'ascii')).decode('ascii')
print('[Auth info] token: ', auth_token, 'partial key: ', partial_key_b64)
headers2= {
'x-radiko-authtoken': auth_token,
'x-radiko-partialkey': partial_key_b64
}
auth2=s.get('https://radiko.jp/v2/api/auth2', headers=headers2)
ifauth2.status_code==200:
ifauth2.text=='OUT':
print('Geo-restricted. Please use a Japan IP.')
return
print('[Auth info] Auth2 succeed.')
playlist_url=f'https://radiko.jp/v2/api/ts/playlist.m3u8?station_id={station}&&ft={ft}&to={to}'
playlist=s.get(playlist_url, headers=headers2)
ifm2:=re.search(r'http.+\.m3u8', playlist.text):
m3u8_url=m2[0]
withs.get(m3u8_url) asr:
urls=re.findall(r'https://media\.radiko\.jp/sound/b/.+?/.+?/.+\.aac', r.text)
print(f'Find {len(urls)} segments!')
else:
print(f'Cannot get playlist from {playlist_url}! Response: {playlist.text}')
return
save_folder=Path(self.save_dir)
filename=f'{ft[2:8]}{ft[8:]}{title} [{station}].m4a'
save_folder.mkdir(parents=True, exist_ok=True)
fullpath=save_folder/filename
#Temp folder
temp_folder=save_folder/'temp'
i=1
whiletemp_folder.exists(): #Make sure to not use existing folder(s)
temp_folder=save_folder/f'temp{i}'
i=i+1
print(f'Create temp dir {temp_folder.name} to save temp files.')
temp_folder.mkdir(parents=True)
print(f'Download video {filename} segs from\n{m3u8_url}')
withconcurrent.futures.ThreadPoolExecutor(max_workers=10) ase:
forurlinurls:
e.submit(download, url, save_path=temp_folder)
files= [fforfintemp_folder.iterdir() iff.suffix.lower() =='.aac']
# Since FFMPEG 4.4, "the file names / paths given in the concat file are relative to the position of the concat file"
# See: https://trac.ffmpeg.org/ticket/9277
# So lets just use abs. path.
my_str='\n'.join(f"file '{f.resolve()}'"forfinfiles)
filelist=temp_folder/'files.txt'
filelist.write_text(my_str, encoding='utf-8')
temp_aac=temp_folder/'temp.aac'
# Concat in raw aac first, then remuxed in m4a container. Otherwise the duration in SOME software would be wrong. Don't ask me why..
run(['ffmpeg', '-f', 'concat', '-safe', '0', '-i', filelist, '-c', 'copy', temp_aac], stdout=DEVNULL)
ifnottemp_aac.exists():
print('[Error] acc concat failed. Please check manually!')
return
run(['ffmpeg', '-i', temp_aac, '-c', 'copy', fullpath], stdout=DEVNULL)
# Remove temp files
forfinfiles:
f.unlink()
filelist.unlink()
temp_aac.unlink()
temp_folder.rmdir()
else:
print(f'[Auth info] Auth2 failed (HTTP {auth2.status_code}).')
return
if__name__=="__main__":
importsys
iflen(sys.argv) >1:
url=sys.argv[1]
else:
url='http://www.joqr.co.jp/timefree/mss.php'
e=RadikoExtractor(url)
e.parse()