Skip to content

Instantly share code, notes, and snippets.

@viz3
Last active October 31, 2021 09:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viz3/2191c0c5c673f5fddc52b4dc1cc1de18 to your computer and use it in GitHub Desktop.
Save viz3/2191c0c5c673f5fddc52b4dc1cc1de18 to your computer and use it in GitHub Desktop.
youtube-dl -o "あとで見る.%(ext)s" "$(./tver.py https://tver.jp/episode/1234567)" https://blog.srytk.com/aquei/555.html
#!/usr/bin/env python3
"""
CC0で公開する https://creativecommons.org/publicdomain/zero/1.0/deed.ja
"""
import re
from urllib.request import urlopen
import argparse
def extract(htm):
"""tverのhtmlからidなどを抽出する"""
pattern = r"""^\s+addPlayer\(\n"""\
r"""\s+'(?P<player_id>[\da-zA-Z]*)',\n"""\
r"""\s+'(?P<player_key>[\da-zA-Z]*)',\n"""\
r"""\s+'(?P<catchup_id>[\da-zA-Z]*)',\n"""\
r"""\s+'(?P<publisher_id>[a-z\d]*)',\n"""\
r"""\s+'(?P<reference_id>[\da-zA-Z_-]*)',\n"""\
r"""\s+'(?P<title>[^']+)',\n"""\
r"""\s+'(?P<sub_title>[^']*)',\n"""\
r"""\s+'(?P<service>[a-z]+)',\n"""\
r"""\s+'(?P<service_name>[^']+)',\n"""\
r"""\s+(?P<sceneshare_enabled>true|false),\n"""\
r"""\s+(?P<share_start>\d+)"""
m = re.search(pattern, htm, re.M)
if not m:
raise Exception("ids not found")
else:
data = m.groupdict()
url = ""
#reference_id, publisher_id, player_keyが必要
if data.keys() >= {"reference_id", "publisher_id", "player_key"}:
url = f'http://players.brightcove.net/{data["publisher_id"]}/{data["player_key"]}_default/index.html?videoId=ref:{data["reference_id"]}'
else:
raise Exception("one of ids not found")
return url
def dl(url):
"""getでリスエストを出してレスポンスを返す"""
with urlopen(url) as req:
return req.read().decode('utf-8')
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("urls", nargs="+", help="tver url")
args = parser.parse_args()
for url in args.urls:
html = dl(url)
print(extract(html))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment