python 脚本从twitch.tv下载过去的广播
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 脚本从twitch.tv下载过去的广播相关的知识,希望对你有一定的参考价值。
import requests
import sys
import json
import re
import os
import string
import argparse
BASE_URL = 'https://api.twitch.tv'
def download_file(url, local_filename):
print("downloading {0}".format(local_filename))
CS = 1024
done = 0
r = requests.get(url, stream=True)
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=CS):
if not chunk: # filter out keep-alive new chunks
continue
f.write(chunk)
f.flush()
done += CS
sys.stdout.write("\r{0:>7.2f} MB".format(done/float(pow(1024,2))))
print("done\n")
def download_broadcast(id_):
""" download all video parts for broadcast 'id_' """
pattern = '{base}/api/videos/a{id_}'
url = pattern.format(base=BASE_URL, id_=id_)
r = requests.get(url)
if r.status_code != 200:
raise Exception("API returned {0}".format(r.status_code))
try:
j = r.json()
except ValueError as e:
print("API did not return valid JSON: {}".format(e))
print("{}".format(r.text))
quit()
qualities = j['chunks']
res = [int(q[:-1]) for q in qualities if re.match("^\d+p", q)]
best_resolution = "{}p".format(max(res))
for nr, chunk in enumerate(j['chunks'][best_resolution]):
video_url = chunk['url']
ext = os.path.splitext(video_url)[1]
filename = "{0}_{1:0>2}{2}".format(id_, nr, ext)
download_file(video_url, filename)
if __name__=="__main__":
parser = argparse.ArgumentParser()
parser.add_argument('video_id', help='twitch video id')
args = parser.parse_args()
download_broadcast(args.video_id)
以上是关于python 脚本从twitch.tv下载过去的广播的主要内容,如果未能解决你的问题,请参考以下文章
XMLHttprequest 无法使用 twitch.tv api
Python 3.4 套接字未收到完整消息
检查 Twitch.tv 流是不是在线并使用 Pircbot 接收观众计数
json Twitch.tv API流式传输数组内容
在 Twitch bot 中添加帐户和更加用户友好的集成
sh Twitch.tv通过VLC媒体播放器直播