User:F4r/hbliv.py

From Rigged Wiki
< User:F4r
Revision as of 00:45, 12 February 2017 by F4r (talk | contribs) (sets bot edit flag properly)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
# -*- coding: utf-8 -*-
# hbliv.py
# A simple script deisgned to get the state of hitbox channel (live=yes, host=host (unimplemented, defaults to no), dead=no)
# and the viewcount, and put these values on  wiki pages (currently Template:AutoOn and Template:AutoVC)
# It's a good idea to run this with cron

import requests
import json
import sys

# get the JSON info from hitbox
r = requests.get('http://api.hitbox.tv/media/live/the4chancup')
if r.status_code != 200:
    print "Error, non-200 status code returned. Requests gave us", r.status_code + ". Exiting..."
    sys.exit(0)
elif r.status_code == 200:
    print "Status code: 200 (all good)"
else:
    print "wat?"
    sys.exit(0)

#set state to undef as a precaution, parse JSON into our variables
state = "undef"
parsed_json = json.loads(r.text)
islive = parsed_json['livestream'][0]['media_is_live']
hosted = parsed_json['livestream'][0]['media_hosted_id']
viewers = parsed_json['livestream'][0]['media_views']

# figure out if the channel is live or not
if islive == '0':
    state = "dead"
elif islive == '1':
    state = "live"
else:
    print "lolwhut? state error, islive", islive

# figure out if we're in hostmode or not
if hosted != None:
    state = "host"
elif hosted == None:
    state = "live"


if state == "undef":
	print "Error, state is undef."
	sys.exit(0)
print state, islive, hosted, viewers


# start wiki editing stuff
username = 'Buttbot'
password = 'password'
baseurl = 'http://implyingrigged.info/w/'
summary = 'autoedit'

# figure out what we want to put on the wiki
if state == "live":
    message = "yes"
elif state == "host":
    message = "no"
elif state == "dead":
    message = "no"
else:
    print "error, invalid state:", state
    sys.exit(0)

message2 = viewers
title = 'Template:AutoOn'
title2 = 'Template:AutoVC'

# Login request
payload = {'action': 'query', 'format': 'json', 'utf8': '', 'meta': 'tokens', 'type': 'login'}
r1 = requests.post(baseurl + 'api.php', data=payload)

# login confirm
login_token = r1.json()['query']['tokens']['logintoken']
payload = {'action': 'login', 'format': 'json', 'utf8': '', 'lgname': username, 'lgpassword': password, 'lgtoken': login_token}
r2 = requests.post(baseurl + 'api.php', data=payload, cookies=r1.cookies)

# get edit token2
params3 = '?format=json&action=query&meta=tokens&continue='
r3 = requests.get(baseurl + 'api.php' + params3, cookies=r2.cookies)
edit_token = r3.json()['query']['tokens']['csrftoken']

edit_cookie = r2.cookies.copy()
edit_cookie.update(r3.cookies)

# save action
payload = {'action': 'edit', 'assert': 'user', 'format': 'json', 'utf8': '', 'text': message,'summary': summary, 'title': title, 'token': edit_token, 'bot': ''}
r4 = requests.post(baseurl + 'api.php', data=payload, cookies=edit_cookie)
payload2 = {'action': 'edit', 'assert': 'user', 'format': 'json', 'utf8': '', 'text': message2,'summary': summary, 'title': title2, 'token': edit_token, 'bot': ''}
r5 = requests.post(baseurl + 'api.php', data=payload2, cookies=edit_cookie)

#print (r4.text)
#print (r5.text)