Skip to content

Instantly share code, notes, and snippets.

@karlgluck
Created April 18, 2015 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karlgluck/ab2799715312ccc68f7c to your computer and use it in GitHub Desktop.
Save karlgluck/ab2799715312ccc68f7c to your computer and use it in GitHub Desktop.
Script that can be run with crontab to collect viewer & channel statistics about the top games on Twitch.TV into CSV files
import requests, json
import os
import re
import time
import datetime
data_dir = os.path.dirname(os.path.realpath(__file__)) + '/data'
api_url = 'https://api.twitch.tv/kraken/games/top?limit=100'
r = requests.get(api_url)
data = r.json()
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
for game in data['top']:
name = game['game']['name']
name = re.sub(r'[^a-zA-Z0-9]', r'', name)
row = "%s,%s,%s,%s\n" % (ts, st, game['channels'], game['viewers'])
with open(data_dir + '/' + name + '.csv', 'a+') as f:
f.write(row)
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment