User:SportsStatsBot/footycode: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
update code
update
Line 23: Line 23:
print "No RegEx found. Skipping."
print "No RegEx found. Skipping."
continue
continue
if league == "dryrun":
request = urllib2.Request(config['leagues'][league]['url'])
request.get_method = lambda : 'HEAD'
continue
response = urllib2.urlopen(request)
shortteams = config['leagues'][league]['short']
new_length = response.info()["Content-Length"]
urlopen = urllib2.urlopen(config['leagues'][league]['url'])
new_length = len(urlopen.read())
txtfiledir = os.path.dirname(os.path.abspath(__file__))+"\\"+league+".txt"
txtfiledir = os.path.dirname(os.path.abspath(__file__))+"\\"+league+".txt"
if not os.path.isfile(txtfiledir):
if not os.path.isfile(txtfiledir):
Line 37: Line 38:
old_length = f.read()
old_length = f.read()
f.close()
f.close()
old_length = 3
if old_length != new_length:
if old_length != new_length:
f = open(txtfiledir, "w")
f = open(txtfiledir, "w")
Line 74: Line 74:
numt += 1
numt += 1
num = 0
num = 0
template = page.Page(site, config['leagues'][league]['template'])
if config['run']['dryrun'][league]:
drypage = page.Page(site, "User:SportsStatsBot/dryrun/"+league)
newtext = template.getWikiText()
for team in config['leagues'][league]['short']:
newtext = drypage.getWikiText()
else:
newtext = re.sub("win_"+name[num]+"=\d*", "win_"+team+"="+won[num], newtext, 1)
newtext = re.sub("draw_"+name[num]+"=\d*", "draw_"+team+"="+drawn[num], newtext, 1)
template = page.Page(site, config['leagues'][league]['template'])
newtext = re.sub("loss_"+name[num]+"=\d*", "loss_"+team+"="+lost[num], newtext, 1)
newtext = template.getWikiText()
newtext = re.sub("gf_"+name[num]+"=\d*", "gf_"+team+"="+forg[num], newtext, 1)
newtext = newtext.decode('utf8')
for team, short in shortteams.iteritems():
newtext = re.sub("ga_"+name[num]+"=\d*", "ga_"+team+"="+against[num], newtext, 1)
shortname = shortteams[name[num].decode('utf8')]
newtext = re.sub("team"+str(num+1)+"=\w{3}", "team"+str(num+1)+"="+shortname, newtext, 1)
newtext = re.sub("win_"+shortname+"=\d*", "win_"+shortname+"="+won[num], newtext, 1)
newtext = re.sub("draw_"+shortname+"=\d*", "draw_"+shortname+"="+drawn[num], newtext, 1)
newtext = re.sub("loss_"+shortname+"=\d*", "loss_"+shortname+"="+lost[num], newtext, 1)
newtext = re.sub("gf_"+shortname+"=\d*", "gf_"+shortname+"="+forg[num], newtext, 1)
newtext = re.sub("ga_"+shortname+"=\d*", "ga_"+shortname+"="+against[num], newtext, 1)
num += 1
num += 1
newtext = re.sub("update=.*$", "update={{subst:CURRENTDAY}} {{subst:CURRENTMONTHNAME}} {{subst:CURRENTYEAR}}", newtext, flags=re.MULTILINE)
newtext = re.sub("update=.*$", "update={{subst:CURRENTDAY}} {{subst:CURRENTMONTHNAME}} {{subst:CURRENTYEAR}}", newtext, flags=re.MULTILINE)
Line 87: Line 94:
newtext = re.sub("\[\[Category:[^\]]*]]", "", newtext)
newtext = re.sub("\[\[Category:[^\]]*]]", "", newtext)
drypage = page.Page(site, "User:SportsStatsBot/dryrun/"+league)
drypage = page.Page(site, "User:SportsStatsBot/dryrun/"+league)
drypage.edit(text=newtext, summary="Bot dry run: Updating football league template ([[User:SportsStatsBot/footyconfig|disable]])")
drypage.edit(text=newtext.encode('utf8'), summary="Bot dry run: Updating football league template ([[User:SportsStatsBot/footyconfig|disable]])")
continue
continue
template.edit(text=newtext, summary="Bot: Updating football league template ([[User:SportsStatsBot/footyconfig|disable]])")
template.edit(text=newtext.encode('utf8'), summary="Bot: Updating football league template ([[User:SportsStatsBot/footyconfig|disable]])")
else:
else:
continue
continue

Revision as of 15:56, 14 June 2017

import urllib2, urllib
from wikitools import *
import sportsbot
import re
import os
import json
import time

site = wiki.Wiki()
site.login(sportsbot.username, sportsbot.password)
configpage = page.Page(site, "User:SportsStatsBot/footyconfig")

configorig = configpage.getWikiText()
config = json.loads(configorig)

def main():
    for league in config['run']:
        if (config['run'][league]) or (config['run']['dryrun'][league]):
            try:
                leagueregex = config['leagues'][league]['regex']
            except:
                print "No RegEx found. Skipping."
                continue
   	    if league == "dryrun":
                continue
            shortteams = config['leagues'][league]['short']
            urlopen = urllib2.urlopen(config['leagues'][league]['url'])
            new_length = len(urlopen.read())
            txtfiledir = os.path.dirname(os.path.abspath(__file__))+"\\"+league+".txt"
            if not os.path.isfile(txtfiledir):
                f = open(txtfiledir, "w")
                f.write(new_length)
                f.close()
                old_length = 0
            else:
                f = open(txtfiledir, "r")
                old_length = f.read()
                f.close()
            if old_length != new_length:
                f = open(txtfiledir, "w")
                f.write(new_length)
                f.close()
            else:
                continue
            openurl = urllib.urlopen(config['leagues'][league]['url']).read()
            tasks = ['pos', 'name', 'played', 'won', 'drawn', 'lost', 'for', 'against', 'gd', 'pts']
            numt = 0
            num = 0
            for team in config['leagues'][league]['short']:
                while num != 10:
                    everything = re.findall(leagueregex[tasks[numt]], openurl)
                    if num == 0:
                        pos = everything
                    elif num == 1:
                        name = everything[1:]
                    elif num == 2:
                        played = everything
                    elif num == 3:
                        won = everything
                    elif num == 4:
                        drawn = everything
                    elif num == 5:
                        lost = everything
                    elif num == 6:
                        forg = everything
                    elif num == 7:
                        against = everything
                    elif num == 8:
                        gd = everything
                    elif num == 9:
                        pts = everything
                    num += 1
                    numt += 1
            num = 0
            if config['run']['dryrun'][league]:
                drypage = page.Page(site, "User:SportsStatsBot/dryrun/"+league)
                newtext = drypage.getWikiText()
            else:
                template = page.Page(site, config['leagues'][league]['template'])
                newtext = template.getWikiText()
            newtext = newtext.decode('utf8')
            for team, short in shortteams.iteritems():
                shortname = shortteams[name[num].decode('utf8')]
                newtext = re.sub("team"+str(num+1)+"=\w{3}", "team"+str(num+1)+"="+shortname, newtext, 1)
                newtext = re.sub("win_"+shortname+"=\d*", "win_"+shortname+"="+won[num], newtext, 1)
                newtext = re.sub("draw_"+shortname+"=\d*", "draw_"+shortname+"="+drawn[num], newtext, 1)
                newtext = re.sub("loss_"+shortname+"=\d*", "loss_"+shortname+"="+lost[num], newtext, 1)
                newtext = re.sub("gf_"+shortname+"=\d*", "gf_"+shortname+"="+forg[num], newtext, 1)
                newtext = re.sub("ga_"+shortname+"=\d*", "ga_"+shortname+"="+against[num], newtext, 1)
                num += 1
            newtext = re.sub("update=.*$", "update={{subst:CURRENTDAY}} {{subst:CURRENTMONTHNAME}} {{subst:CURRENTYEAR}}", newtext, flags=re.MULTILINE)
            if config['run']['dryrun'][league]:
                newtext = re.sub("\[\[Category:[^\]]*]]", "", newtext)
                drypage = page.Page(site, "User:SportsStatsBot/dryrun/"+league)
                drypage.edit(text=newtext.encode('utf8'), summary="Bot dry run: Updating football league template ([[User:SportsStatsBot/footyconfig|disable]])")
                continue
            template.edit(text=newtext.encode('utf8'), summary="Bot: Updating football league template ([[User:SportsStatsBot/footyconfig|disable]])")
        else:
            continue

if __name__ == "__main__":
    main()
# Run speed debugging
#start_time = time.time()
#main()
#print("--- %s seconds ---" % (time.time() - start_time))