Skip to content

Instantly share code, notes, and snippets.

@c2t-r
Created May 18, 2024 07:15
Show Gist options
  • Select an option

  • Save c2t-r/a43d9f73fc0618d5440904172c42ae6c to your computer and use it in GitHub Desktop.

Select an option

Save c2t-r/a43d9f73fc0618d5440904172c42ae6c to your computer and use it in GitHub Desktop.
radiko downloader (m3u8)
from requests import get
from os import path as os_path
from re import findall
from random import choices
from string import ascii_lowercase, digits
from base64 import b64encode
radiko_url = "https://radiko.jp/#!/ts/QRR/20240512213000"
ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"
def randomstr(n):
return ''.join(choices(ascii_lowercase + digits, k=n))
def fileName(name: str):
i = 1
base_name, ext = os_path.splitext(name)
while os_path.isfile(name):
name = f'{base_name}_{i}{ext}'
i = i + 1
return name
def main():
station_id = radiko_url.split("/")[-2]
station_url = f'https://api.radiko.jp/program/v4/date/{radiko_url.split("/")[-1][:8]}/station/{station_id}.json'
response = get(station_url)
obj = response.json()
program = [program for i in obj["stations"] for program in i["programs"]["program"] if program["ft"] == radiko_url.split("/")[-1]]
if not program: raise IndexError()
start = program[0]["ft"]
finish = program[0]["to"]
print(program[0]["season_name"], "\n")
area_url = "https://api.radiko.jp/apparea/area"
response = get(area_url)
area = findall(r'class="(.+?)"', response.text)[0]
response = get(radiko_url)
common = findall(r'src="(/apps/js/playerCommon\.js\?_=\d{8})"', response.text)[0]
cpmmon_url = f'https://radiko.jp{common}'
response = get(cpmmon_url)
complete_key = findall(r"'pc_html5', '(.+?)'", response.text)[0]
check_url = "https://radiko.jp/ap/member/webapi/v2/member/login/check"
response = get(check_url)
set_cookie = response.headers["Set-Cookie"]
session = set_cookie.split(" ")[0]
print(session)
auth1_url = "https://radiko.jp/v2/api/auth1"
header = {
"cookie": f'{session} default_area_id={area}; tracking_area_id={area}; shortscc=11; a_exp={randomstr(32)};',
"x-radiko-user": "dummy_user",
"x-radiko-device": "pc",
"x-radiko-app": "pc_html5",
"x-radiko-app-version": "0.0.1",
"user-agent": ua,
}
response = get(auth1_url, headers=header)
token = response.headers["x-radiko-authtoken"]
key_offset = int(response.headers["x-radiko-keyoffset"])
print(f'token={token}')
partial_key = b64encode(complete_key[key_offset:key_offset+16].encode()).decode()
print(f'partial_key={partial_key}')
auth2_url = "https://radiko.jp/v2/api/auth2"
header = {
"cookie": f'{session} default_area_id={area}; tracking_area_id={area}; rdk_uid={randomstr(32)};',
"x-radiko-user": "dummy_user",
"x-radiko-device": "pc",
"X-Radiko-Authtoken": token,
"X-Radiko-partialkey": partial_key,
"user-agent": ua,
}
response = get(auth2_url, headers=header)
url = f'https://tf-f-rpaa-radiko.smartstream.ne.jp/tf/playlist.m3u8?station_id={station_id}&start_at={start}&ft={start}&end_at={finish}&to={finish}&l=15&lsid=58b7f4fc73c0e0d8c56c0553d3220a26&type=b' # idk
header = {
"X-Radiko-Authtoken": token,
"X-Radiko-Areaid": area,
"user-agent": ua,
}
response = get(url, headers=header)
response.raise_for_status()
print()
out_name = fileName(f'radiko_{start}_{finish}.m3u8')
with open(out_name, "wb") as f:
f.write(response.content)
print(out_name)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment