Lwj.py

提供: StarCraft II: Legacy of the Void 日本語Wiki
移動: 案内検索
  1. from __future__ import print_function
  2. import datetime
  3. import os
  4. import sys
  5. import random
  6. import string
  7. import challonge
  8. import urllib3
  9. from challonge import api
  10. import zipfile
  11. import shutil
  12. import locale
  13.  
  14. #locale設定-
  15. #locale.setlocale(locale.LC_ALL, '')
  16.  
  17. username = None
  18. api_key = None
  19. googleDstdir = None
  20.  
  21. #CONFIG
  22. index="Open011"
  23. openTourney=True
  24. intermediate=True
  25. startDateTime=datetime.datetime(2016,1,16,21,0,0)
  26. replaypack="Open010"
  27. replayUpload=True
  28. subDomain="lwj-"
  29. checkinDuration=60
  30. generateWikiScript=True
  31.  
  32. #CONSTANTS
  33. challongeFormat="%Y-%m-%dT%H:%M:%S+09:00"
  34. wikiFormat="%Y/%m/%d(%a) %H:%M"
  35.  
  36. class Main:
  37.   def prac():
  38.     print(startDateTime.strftime(wikiFormat))
  39.  
  40.  
  41.   def updateWikiNotification(startDateStr,urlOpen,urlIntermediate):  
  42.     print("script for wiki update: wiki.bat")
  43.     str1="python replace.py -always -summary:lwj.py -file:nextLWJ.txt -regex "
  44.  
  45.     cmd1=str1 + '"startDate=.*" "startDate=' + startDateStr + '"'
  46.     cmd2=str1 + '"urlOpen=.*" "urlOpen=' + urlOpen + '"'
  47.     cmd3=str1 + '"urlIntermediate=.*" "urlIntermediate=' + urlIntermediate + '"'
  48.     print(cmd1)
  49.     print(cmd2)
  50.     print(cmd3)
  51.  
  52.     f = open('wiki.bat', 'w') 
  53.     f.write(cmd1 + "\n" + cmd2 + "\n" + cmd3) 
  54.     f.close()
  55.  
  56.   def createChallonge():  
  57.  
  58.     challonge.set_credentials(username, api_key)
  59.     signupOpen=""
  60.     signupIntermediate=""
  61.  
  62.     startDateTimeChallonge=startDateTime.strftime(challongeFormat)
  63.  
  64.     descStringCommon="<br>" \
  65.       "ルール: http://www.sc2jpwiki.com/wiki/Legacy_Weekly_Japanのルール<br>" \
  66.       "参加登録: "
  67.  
  68.     if openTourney:
  69.       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)
  70.       signupOpen=new["sign-up-url"]
  71.       descString=\
  72.       "/join LWJ" + descStringCommon + signupOpen
  73.  
  74.       new=challonge.tournaments.update(new["id"],description=descString,game_name="StarCraft II: Legacy Of the Void")
  75.  
  76.     if intermediate:
  77.       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)
  78.       signupIntermediate=new["sign-up-url"]
  79.       descString=\
  80.       "/join LWJ2" + descStringCommon + signupIntermediate
  81.  
  82.       new=challonge.tournaments.update(new["id"],description=descString,game_name="StarCraft II: Legacy Of the Void")
  83.  
  84.     if generateWikiScript:
  85.       startDateStr=startDateTime.strftime(wikiFormat)
  86.       Main.updateWikiNotification(startDateStr,signupOpen,signupIntermediate)
  87.  
  88.  
  89.   def destroyChallonge():
  90.     challonge.set_credentials(username, api_key)
  91.     if openTourney:
  92.       challonge.tournaments.destroy("lwj-Open"+index)
  93.     if intermediate:
  94.       challonge.tournaments.destroy("lwj-Intermediate"+index)
  95.  
  96.   def getParticipants(tournament):
  97.     participants = challonge.participants.index(tournament)
  98.     return participants
  99.     print("getParticipants done")
  100.  
  101.  
  102.  
  103.  
  104.  
  105.   def getParticipantsName(p):
  106.     if p["name"]:
  107.       return p["name"]
  108.     else:
  109.       return p["username"]
  110.  
  111.   def showParticipants(ps):
  112.     for p in ps:
  113.       print(Main.getParticipantsName(p))
  114.  
  115.   def idToName(id, participants):
  116.     result=""
  117.     for p in participants:
  118.       if p["id"] == id:
  119.         result = Main.getParticipantsName(p)
  120.     return result
  121.  
  122.   def getReplaypack():
  123.     challonge.set_credentials(username, api_key)
  124.     tournament=challonge.tournaments.show(subDomain+replaypack)
  125.     ms = challonge.matches.index(tournament["id"])
  126.     ps = Main.getParticipants(tournament["id"])
  127.     Main.showParticipants(ps)
  128.     filelist=[]
  129.     for m in ms:
  130.         #print(m)
  131.         playerA=Main.idToName(m["player1-id"], ps)
  132.         playerB=Main.idToName(m["player2-id"], ps)
  133.         matchupStr=playerA + " vs " + playerB
  134.         print(matchupStr)
  135.         attachmentsIndex=api.fetch_and_parse(
  136.           "GET",
  137.           "tournaments/%s/matches/%s/attachments" % (tournament["id"], m["id"]),
  138.         )
  139.  
  140.         for obj in attachmentsIndex:
  141.           #print(obj)
  142.           filename = matchupStr+"-"+obj["asset-file-name"] + ".SC2Replay"
  143.           #print(filename)
  144.           url = "http:" + obj["asset-url"]
  145.           #print(url)
  146.  
  147.           connection_pool = urllib3.PoolManager()
  148.           resp = connection_pool.request('GET',url )
  149.           f = open(filename, 'wb')
  150.           f.write(resp.data)
  151.           f.close()
  152.           resp.release_conn()
  153.           filelist.append(filename)
  154.  
  155.     #zip
  156.     print("creating archive")
  157.     zfName=subDomain+replaypack+'-Replaypack.zip'
  158.     zf = zipfile.ZipFile(zfName, mode='w')
  159.     try:
  160.       for filename in filelist:
  161.         print('adding '+filename)
  162.         zf.write(filename)
  163.     finally:
  164.       print('closing')
  165.       zf.close()
  166.  
  167.     #upload
  168.     if replayUpload:
  169.       shutil.copy(zfName, googleDstdir)
  170.       os.remove(zfName)
  171.  
  172.     #remove unnecesarry replayfiles
  173.     for filename in filelist:
  174.       os.remove(filename)
  175.  
  176.  
  177.   def main(param):
  178.  
  179.     if param[1]=="create":
  180.       Main.createChallonge()
  181.  
  182.     if param[1]=="destroy":
  183.       Main.destroyChallonge()
  184.  
  185.     if param[1]=="replay":
  186.       Main.getReplaypack()
  187.  
  188. if __name__ == "__main__":
  189.     username = os.environ.get("CHALLONGE_USER")
  190.     api_key = os.environ.get("CHALLONGE_KEY")
  191.     googleDstdir = os.environ.get("TOURNAMENT_REPLAYPACKS")
  192.     if not username or not api_key:
  193.         raise RuntimeError("You must add CHALLONGE_USER and CHALLONGE_KEY \
  194.             to your environment variables to run the test suite")
  195.  
  196.  
  197.     if len(sys.argv) == 1:
  198.       print("You need to specify command")
  199.       Main.prac()
  200.     if len(sys.argv) > 1:
  201.       Main.main(sys.argv)