Advertisement
kneefer

Send submission

Nov 25th, 2015
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.42 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. import os.path
  4. import sys
  5. import getopt
  6. import time
  7. from requests import session
  8. import webbrowser
  9.  
  10. loginFormLink      = "https://knowledgepit.fedcsis.org/login/index.php"
  11. submissionFormLink = "https://knowledgepit.fedcsis.org/mod/challenge/view.php?id=891"
  12.  
  13. def parse_arguments():
  14.     userName = ''
  15.     password = ''
  16.     fileName = ''
  17.  
  18.     try:
  19.         myopts, args = getopt.getopt(sys.argv[1:],"u:p:f:", ['username=', 'password=', 'file='])
  20.     except getopt.GetoptError as e:
  21.         print (str(e))
  22.         print("Usage: %s -u username -p password -f fileName" % sys.argv[0])
  23.         sys.exit(2)
  24.  
  25.     for o, a in myopts:
  26.         if o in ('-u', '--username'):
  27.             userName = a
  28.         elif o in ('-p', '--password'):
  29.             password = a
  30.         elif o in ('-f', '--file'):
  31.             fileName = a
  32.  
  33.     if not userName or not password or not fileName:
  34.         print("Usage: %s -u username -p password -f fileName" % sys.argv[0])
  35.         exit(2)
  36.  
  37.     print 'USER_NAME :', userName
  38.     print 'PASSWORD  :', '*' * len(password)
  39.     print 'FILE_NAME :', fileName
  40.  
  41.     return [userName, password, fileName]
  42.  
  43. ########################################
  44. #              POCZĄTEK                #
  45. ########################################
  46.  
  47. # Parse input arguments
  48. userName, password, submissionFileName = parse_arguments()
  49.  
  50. loginData = {
  51.         'username': userName,    # user name text input
  52.         'password': password,    # password input
  53.         'submit'  : 'Login',     # submit form button
  54.         'rememberusername' : 0   # remember logging checkbox
  55. }
  56.  
  57. submissionData = {
  58.         'submissionname' : '',       # submission name text input
  59.         'notes'          : '',       # submission description textarea
  60.         'chooseFinal'    : 1,        # choosing the best score select input
  61.         'submit1'        : 'Submit', # submit form button
  62. }
  63.  
  64. # Open session
  65. c = session()
  66. print 'Created session'
  67.  
  68. # Login to the knowledgepit.fedcsis.org
  69. print 'Logging in as an user: %s ...' % (loginData['username'])
  70.  
  71. start = time.time()
  72. loginResponse = c.post(
  73.         url  = loginFormLink,
  74.         data = loginData,
  75.         allow_redirects = True)
  76. print 'Operation took %s seconds' % (time.time() - start)
  77.  
  78. # Check the login response
  79. if loginResponse.status_code == 200:
  80.         print 'Successfully logged in'
  81. else:
  82.         print '### Error ### with logging in'
  83.         exit(1)
  84.  
  85. # Send submission with submission file
  86. print 'Sending submission file: %s ...' % (submissionFileName)
  87.  
  88. start = time.time()
  89. with open(submissionFileName, 'rb') as submissionFile:
  90.         submissionResponse = c.post(
  91.                 url   = submissionFormLink,
  92.                 data  = submissionData,
  93.                 files = {'challengefile' : (os.path.basename(submissionFileName), submissionFile)})
  94. print 'Operation took %s seconds' % (time.time() - start)
  95.  
  96. # Check the send submission response
  97. if submissionResponse.status_code == 200:
  98.         print 'Successfully sent submission file'
  99. else:
  100.         print '### Error ### with sending submission file'
  101.         exit(1)
  102.  
  103. # Save response (HTML page) for checking for errors
  104. with open('submissionTempOutput.html', 'w') as logFile:
  105.         logFile.write(submissionResponse.text)
  106.  
  107. # Open browser with the submission send confirmation
  108. webbrowser.open_new_tab(logFile.name)
  109.  
  110. # Close session
  111. c.close()
  112. print 'Closed session'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement