Created
April 18, 2015 15:59
-
-
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
This file contains 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 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