Lwj.py
提供: StarCraft II: Legacy of the Void 日本語Wiki
from __future__ import print_function
import datetime
import os
import sys
import random
import string
import challonge
import urllib3
from challonge import api
import zipfile
import shutil
import locale
#locale設定-
#locale.setlocale(locale.LC_ALL, '')
username = None
api_key = None
googleDstdir = None
#CONFIG
index="Open011"
openTourney=True
intermediate=True
startDateTime=datetime.datetime(2016,1,16,21,0,0)
replaypack="Open010"
replayUpload=True
subDomain="lwj-"
checkinDuration=60
generateWikiScript=True
#CONSTANTS
challongeFormat="%Y-%m-%dT%H:%M:%S+09:00"
wikiFormat="%Y/%m/%d(%a) %H:%M"
class Main:
def prac():
print(startDateTime.strftime(wikiFormat))
def updateWikiNotification(startDateStr,urlOpen,urlIntermediate):
print("script for wiki update: wiki.bat")
str1="python replace.py -always -summary:lwj.py -file:nextLWJ.txt -regex "
cmd1=str1 + '"startDate=.*" "startDate=' + startDateStr + '"'
cmd2=str1 + '"urlOpen=.*" "urlOpen=' + urlOpen + '"'
cmd3=str1 + '"urlIntermediate=.*" "urlIntermediate=' + urlIntermediate + '"'
print(cmd1)
print(cmd2)
print(cmd3)
f = open('wiki.bat', 'w')
f.write(cmd1 + "\n" + cmd2 + "\n" + cmd3)
f.close()
def createChallonge():
challonge.set_credentials(username, api_key)
signupOpen=""
signupIntermediate=""
startDateTimeChallonge=startDateTime.strftime(challongeFormat)
descStringCommon="<br>" \
"ルール: http://www.sc2jpwiki.com/wiki/Legacy_Weekly_Japanのルール<br>" \
"参加登録: "
if openTourney:
new=challonge.tournaments.create("Legacy Weekly Japan " + index +" Open(ランク無制限)","Open"+index,subdomain="lwj",open_signup=True,accept_attachments=True,check_in_duration=checkinDuration,start_at=startDateTimeChallonge)
signupOpen=new["sign-up-url"]
descString=\
"/join LWJ" + descStringCommon + signupOpen
new=challonge.tournaments.update(new["id"],description=descString,game_name="StarCraft II: Legacy Of the Void")
if intermediate:
new=challonge.tournaments.create("Legacy Weekly Japan " + index +" Intermediate(中級)","Intermediate"+index,subdomain="lwj",open_signup=True,accept_attachments=True,check_in_duration=checkinDuration,start_at=startDateTimeChallonge)
signupIntermediate=new["sign-up-url"]
descString=\
"/join LWJ2" + descStringCommon + signupIntermediate
new=challonge.tournaments.update(new["id"],description=descString,game_name="StarCraft II: Legacy Of the Void")
if generateWikiScript:
startDateStr=startDateTime.strftime(wikiFormat)
Main.updateWikiNotification(startDateStr,signupOpen,signupIntermediate)
def destroyChallonge():
challonge.set_credentials(username, api_key)
if openTourney:
challonge.tournaments.destroy("lwj-Open"+index)
if intermediate:
challonge.tournaments.destroy("lwj-Intermediate"+index)
def getParticipants(tournament):
participants = challonge.participants.index(tournament)
return participants
print("getParticipants done")
def getParticipantsName(p):
if p["name"]:
return p["name"]
else:
return p["username"]
def showParticipants(ps):
for p in ps:
print(Main.getParticipantsName(p))
def idToName(id, participants):
result=""
for p in participants:
if p["id"] == id:
result = Main.getParticipantsName(p)
return result
def getReplaypack():
challonge.set_credentials(username, api_key)
tournament=challonge.tournaments.show(subDomain+replaypack)
ms = challonge.matches.index(tournament["id"])
ps = Main.getParticipants(tournament["id"])
Main.showParticipants(ps)
filelist=[]
for m in ms:
#print(m)
playerA=Main.idToName(m["player1-id"], ps)
playerB=Main.idToName(m["player2-id"], ps)
matchupStr=playerA + " vs " + playerB
print(matchupStr)
attachmentsIndex=api.fetch_and_parse(
"GET",
"tournaments/%s/matches/%s/attachments" % (tournament["id"], m["id"]),
)
for obj in attachmentsIndex:
#print(obj)
filename = matchupStr+"-"+obj["asset-file-name"] + ".SC2Replay"
#print(filename)
url = "http:" + obj["asset-url"]
#print(url)
connection_pool = urllib3.PoolManager()
resp = connection_pool.request('GET',url )
f = open(filename, 'wb')
f.write(resp.data)
f.close()
resp.release_conn()
filelist.append(filename)
#zip
print("creating archive")
zfName=subDomain+replaypack+'-Replaypack.zip'
zf = zipfile.ZipFile(zfName, mode='w')
try:
for filename in filelist:
print('adding '+filename)
zf.write(filename)
finally:
print('closing')
zf.close()
#upload
if replayUpload:
shutil.copy(zfName, googleDstdir)
os.remove(zfName)
#remove unnecesarry replayfiles
for filename in filelist:
os.remove(filename)
def main(param):
if param[1]=="create":
Main.createChallonge()
if param[1]=="destroy":
Main.destroyChallonge()
if param[1]=="replay":
Main.getReplaypack()
if __name__ == "__main__":
username = os.environ.get("CHALLONGE_USER")
api_key = os.environ.get("CHALLONGE_KEY")
googleDstdir = os.environ.get("TOURNAMENT_REPLAYPACKS")
if not username or not api_key:
raise RuntimeError("You must add CHALLONGE_USER and CHALLONGE_KEY \
to your environment variables to run the test suite")
if len(sys.argv) == 1:
print("You need to specify command")
Main.prac()
if len(sys.argv) > 1:
Main.main(sys.argv)