Advertisement
Guest User

Nikolay Viktorov / Timus AutoSubmit script

a guest
May 10th, 2015
526
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.54 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. import os
  4. import requests
  5. import sys
  6. import itertools
  7. import time
  8.  
  9. judge_id = "XXXXXXXX"
  10.  
  11. ext_id = {
  12.     "cpp"   : "10",
  13.     "c"     : "9",
  14.     "py"    : "35",
  15.     "java"  : "32",
  16. }
  17.  
  18. """
  19. Timus Online Judge compilators' ids:
  20. "31" >  FreePascal 2.6
  21. "9"  >  Visual C 2010
  22. "10" >  Visual C++ 2010
  23. "25" >  GCC 4.9
  24. "26" >  G++ 4.9
  25. "27" >  GCC 4.9 C11
  26. "28" >  G++ 4.9 C++11
  27. "30" >  Clang 3.5 C++14
  28. "32" >  Java 1.8
  29. "11" >  Visual C# 2010
  30. "15" >  VB.NET 2010
  31. "34" >  Python 2.7
  32. "35" >  Python 3.4
  33. "14" >  Go 1.3
  34. "18" >  Ruby 1.9
  35. "19" >  Haskell 7.6
  36. "33" >  Scala 2.11
  37. """
  38.  
  39. if len(sys.argv) < 2:
  40.     print("Solution filename not specified")
  41.     sys.exit()
  42.  
  43. if not os.path.exists(sys.argv[1]):
  44.     print("Solution file does not exist or not enough rights to read it")
  45.     sys.exit()
  46.  
  47. filename = os.path.basename(sys.argv[1])
  48.  
  49. problem_index = ''.join(itertools.takewhile(lambda c: c.isdigit(), filename));
  50. extension = filename[len(problem_index) + 1:].lower()
  51.  
  52.  
  53. if (len(problem_index) == 0):
  54.     print("Incorrect filename format. Example: 1000.cpp")
  55.     sys.exit()
  56.  
  57. if extension not in ext_id:
  58.     print("Unknown extension. Please check 'ext_id' variable")
  59.     sys.exit()
  60.  
  61. data = {
  62.     "Action":               "submit",
  63.     "JudgeID":              judge_id,
  64.     "ProblemNum":           problem_index,
  65.     "Source":               open(sys.argv[1], "rb").read(),
  66.     "Language":             ext_id[extension],
  67.     "SourceFile":           "",
  68.     "SpaceID":              "1"
  69. }
  70.  
  71. submit_addr = "http://acm.timus.ru/submit.aspx"
  72.  
  73. requests.post(submit_addr, data=data)
  74.  
  75. print ("\n Solution is successfully sent. Current time is " + time.strftime("%H:%M:%S") + "\n")
  76.  
  77.  
  78. status_addr = "http://acm.timus.ru/status.aspx"
  79. author_id = ''.join(itertools.takewhile(lambda c: c.isdigit(), judge_id))
  80.  
  81. old_verdict = " "
  82. while True:
  83.     req = requests.get(status_addr, params={ "author" : author_id })
  84.  
  85.     con = req.text
  86.     id0 = con.find("Memory used")
  87.    
  88.     id1 = con.find("nofollow", id0)
  89.     id2 = con.find("<", id1)
  90.     submit_id = con[id1+10:id2]
  91.  
  92.     id1 = con.find("verdict", id2)
  93.     id2 = con.find("<", id1)
  94.     new_verdict = con[id1+12:id2]
  95.     br = new_verdict.find("(")
  96.     if (br != -1):
  97.         new_verdict = new_verdict[:br-1]
  98.    
  99.     id1 = con.find("test", id2)
  100.     id2 = con.find("<", id1)
  101.     test = con[id1+6:id2]
  102.            
  103.     id1 = con.find("runtime", id2)
  104.     id2 = con.find("<", id1)
  105.     runtime = con[id1+9:id2]
  106.            
  107.     id1 = con.find("memory", id2)
  108.     id2 = con.find("<", id1)
  109.     memory = con[id1+8:id2]
  110.  
  111.     if (old_verdict != new_verdict):
  112.         if (new_verdict == "Compiling" or new_verdict == "Running"):
  113.             print (" " + new_verdict + "...")
  114.         else:
  115.             print("--------------------------------------------------------------------")
  116.             if (new_verdict == ""):
  117.                 print ('%22s' % ("Compilation error"), "\t\tTime: 0", "\tMemory used: 0 KB", '\n')
  118.                 ses = requests.Session()
  119.                 ses.post("http://acm.timus.ru/auth.aspx", data = { "JudgeID" : judge_id, "Action" : "login" } )
  120.                 print(ses.get("http://acm.timus.ru/ce.aspx?id=" + submit_id).text)
  121.             elif (new_verdict == "Accepted"):
  122.                 print ('%22s' % (new_verdict), "\t\tTime: ", runtime, "\tMemory used: ", memory, "\n")
  123.             else:
  124.                 print ('%22s' % (new_verdict), "\t", test, "\tTime: ", runtime, "\tMemory used: ", memory, "\n")
  125.             break
  126.         old_verdict = new_verdict
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement