Created
June 6, 2016 08:47
-
-
Save rahulg/043728b6cd7ad2c40b053447727dbf00 to your computer and use it in GitHub Desktop.
Dump twitch chat replays
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import json | |
import re | |
import sys | |
import urllib.request | |
video_id = sys.argv[1] | |
try: | |
start_offset = int(sys.argv[1]) | |
except (IndexError, ValueError): | |
start_offset = 0 | |
def geturl(start): | |
return 'https://rechat.twitch.tv/rechat-messages?start={}&video_id=v{}'.format(start, video_id) | |
try: | |
rd = urllib.request.urlopen(geturl(0)) | |
except urllib.error.HTTPError as err: | |
rd = err | |
d = json.loads(rd.read().decode('utf-8')) | |
error_text = d['errors'][0]['detail'] | |
ts_re = re.compile(r'0 is not between (\d+) and (\d+)') | |
match = ts_re.match(error_text) | |
if not match: | |
print(error_text) | |
sys.exit(1) | |
ts_start = int(match.group(1)) | |
ts_end = int(match.group(2)) | |
for ts in range(ts_start, ts_end, 30): | |
rd = urllib.request.urlopen(geturl(ts)) | |
d = json.loads(rd.read().decode('utf-8')) | |
for msg in d['data']: | |
msg_ts = msg['attributes']['timestamp'] / 1000 | |
offset = datetime.timedelta(seconds=msg_ts-ts_start) | |
try: | |
user = msg['attributes']['tags']['display-name'] | |
except KeyError: | |
user = 'system' | |
msg_text = msg['attributes']['message'] | |
print('[{}] {}: {}'.format(offset, user, msg_text)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment