Skip to content

Instantly share code, notes, and snippets.

@PetterKraabol
Last active February 20, 2021 19:27
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save PetterKraabol/0daed253f4c4ebce25c3fdac8f3cf0d3 to your computer and use it in GitHub Desktop.
ChatReplay (Moved)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests, sys, time
#
# DEPENDENCIES
#
# You'll need the Requests library: http://docs.python-requests.org/en/master/user/install/#install
#
# Video id input (prefixed 'v' in the parameter is is optional).
videoId = 'v' + sys.argv[1].replace('v', '')
# Get start and stop time by looking at the 'detail' message from Twitch
#
# If you query this API with invalid an invalid timestamp (none or out of range),
# it will tell you the start and stop timestamp, however, in text format.
response = requests.get('https://rechat.twitch.tv/rechat-messages?start=0&video_id=' + videoId).json()
# Parse response for start and stop
#
# The response will look something like this
# {
# "errors": [
# {
# "status": 400,
# "detail": "0 is not between 1469108651 and 1469133795"
# }
# ]
# }
#
# As the start and stop timestamp is (for some weird reason)
# in text format, we have to parse the response.
detail = response['errors'][0]['detail'].split(' ') # We split the detail string into an array
start = int(detail[4]) # The start timestamp is on index 4
stop = int(detail[6]) # while stop has the index 6
# Used message ids
#
# Every message has an unique ID, which we can check if we've already stored it.
# Querying a specific timestamp will not just return messages form that timestamp,
# but also messages that has been sent a few seconds after as well.
# I'm not sure what the time frame is.
messageIds = []
# Open output file
#
# This is where we save the messages.
file = open(videoId+'.txt', 'w')
# Download messages from timestamps between start and stop.
timestamp = start
while timestamp <= stop:
# Request messages from Twitch
response = requests.get('https://rechat.twitch.tv/rechat-messages?start=' + str(timestamp) + '&video_id=' + videoId).json()
data = response['data'];
# Increase by one (will be overwritten if new messages are found).
timestamp += 1
for message in data:
# Check the unique message ID to make sure it's not already saved.
if not any(message['id'] in s for s in messageIds):
# If this is a new message, save the unique ID to prevent duplication later.
messageIds.append(message['id'])
# Message data
date = time.strftime('%m/%d/%Y %H:%M:%S', time.gmtime(message['attributes']['timestamp']/1000.))
sender = message['attributes']['from'].encode('utf-8')
text = message['attributes']['message'].encode('utf-8')
# Append message to output file
file.write(date + ' ' + sender + ': ' + text + '\n')
# Set timestamp to this message's timestamp to improve
# performance and skip timestamps where no new messages are coming in.
#
# Note: The message timestamp is divided by 1000 because the ReChat API
# query does not want the last 3 digits (for whatever reason)
timestamp = int(message['attributes']['timestamp']/1000)
# Print to console (optional)
print '\033[94m' + date + ' \033[92m'+ sender + '\033[0m' + ': ' + text
# Close file
file.close()
@sheddup
Copy link

sheddup commented May 22, 2016

Thanks, wonder if there is any way to speed up the amount of lines per second.

@PetterKraabol
Copy link
Author

Optimized and moved to PetterKraabol/Twitch-Chat-Downloader

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment