CountZero's blog

By CountZero, 4 years ago, In Russian,

Update (29.05.15)

This script doesn't work anymore because of recent security upgrade from codeforces team. If you have any suggestions about Codeforces API, please read this marat.snowbear's blog post: /blog/entry/18185

==================================================================================================

Hi. Recently I decided to write such script, and it turned out to be very simple task.

Here it is

Setup

First of all, install requests library into your python 3.x (I didn't tested it under 2.x) distribution. Just like that: pip install requests.

Second, edit x_user and csrf_token variables. Look for X-User in your browser cookies, and for X-Csrf-Token in this page source. Update them every time you login.

Third, your submission language will be determined by file extension. By default, .java is bound to codeforces Java 8, .cpp to GNU C++ 0x 4, and .py to Python 3.3. If you want to change this behavior, edit ext_id variable.

If you prefer codeforces.ru over codeforces.com, set cf_domain variable to ru.

Usage

python cfsubmit.py C:\contests\123a.cpp
python cfsubmit.py 123B.py

Solution filename have to be in exactly this format. Case doesn't matter.

Good luck!

 
 
 
 
  • Vote: I like it  
  • +23
  • Vote: I do not like it  

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

I think it's useless without showing the result of submission.

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    see my comment above, now it returns the result :)

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Useless? I don't think so.

    I would say that it will be better if it notifies me about the result of my submission, but it's nice already. Keep it simple. This code is not a complete package, it's purpose is to submit a code. It's a great idea to write a code to check the result of the submission, so we will be pleased to use your code if you decide to write and publish it. (which igorjan94 has already done)

    I was expecting this as an API from Codeforces, though.

»
4 years ago, # |
Rev. 3   Vote: I like it +5 Vote: I do not like it

I updated your script, now it returns result of submission (also can list previous submissions), supports filenames 123/A.cpp and so on (and I fixed bug you didn't exit here if not os.path.exists(sys.argv[1]):)
new version of your script
PS: thanx for this script! added hotkey in my .vimrc, so now submitting process will take only one second without chance to send other file! :)

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Thanks for contribution. There is no need to import urllib and json. Replace lines 97 and 98 with this:

    data = requests.get(...).json()['result']
    ...
    

    Also this code can block your interface for a long time and return nothing useful in case your solution has been lost or rejected or whatever. So I prefer to keep it simple and check the verdict just by pressing F5.

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Oh, thanx, it's easier way to get json from url
      No, it doesn't block the interface. It blocks only if there is long testing queue, but ^C always helps you :)
      If submission rejected or smth else it just prints previous submission(s) and exits

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Is anyone using it right now? It seems I couldn't find X-User info, and try replacing it with my username doesn't work.

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    try opening this page

    then open chrome://settings/cookies. X-User should be in place, it is mandatory cookie

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Howto:

  1. Open any codeforces page
  2. Open your javascript console (Ctrl+Shift+J for Chrome etc.). Type in document.cookie to see your cookies, find X-User=... in that string, Ctrl+C, Ctrl+V
  3. Go to page source, then to head tag, and <meta name="X-Csrf-Token" content="...">. Ctrl+C, Ctrl+V
  4. If you log out from Codeforces, repeat steps 1-3
»
4 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Hello, I have checked in my browser cookies(I use firefox), it's showing X-User="". And I am unable to submit. Can you please help.

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Hard to say. Maybe your system is configured to reject cookies. Or maybe you're checking cookies for codeforces.ru while you logged into codeforces.com. Or vice versa.

    How you check it? The easiest way is right click — "View page info" — "Security" — "View cookies"

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Actually I am logged into .com and also checking for .com. And also My browser is allowing cookies. :(

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      I have relogged in again. Now it's showing : X-User = """;

      • »
        »
        »
        »
        4 years ago, # ^ |
        Rev. 2   Vote: I like it 0 Vote: I do not like it

        try this: press Ctrl+Shift+Q (I actually developed the script using this tool) and reload the page. Then select request type — HTML (on the bottom), select the only request, choose Cookies tab (on the right) and look for X-User

        Or: log out, Ctrl+Shift+Q, log in, look for POST request with status code 302, response cookies.

        • »
          »
          »
          »
          »
          4 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          Firstly thank you for helping, When I press Ctrl+Shift+Q , in firefox : I am getting an alert box containing "No media found for quick download", where as in chrome, window is closing.

          • »
            »
            »
            »
            »
            »
            4 years ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            When I press Ctrl+Shift+Q , in firefox : I am getting an alert box containing "No media found for quick download"

            hit F5

            • »
              »
              »
              »
              »
              »
              »
              4 years ago, # ^ |
                Vote: I like it 0 Vote: I do not like it

              Still it's showing X-User=""; :(

              • »
                »
                »
                »
                »
                »
                »
                »
                4 years ago, # ^ |
                Rev. 2   Vote: I like it 0 Vote: I do not like it

                ====================================================================

                Then, as I said, press "logout" link, Ctrl+Shift+Q, press "enter" link, enter your username and password, press "login", and look for POST request with status code 302.

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  4 years ago, # ^ |
                    Vote: I like it 0 Vote: I do not like it

                  Now it's showing X-User:"""" . Now leave about it, may be I will try after some time.You carry on your work.

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  4 years ago, # ^ |
                    Vote: I like it 0 Vote: I do not like it

                  Is it possible of ignoring X-User variable and instead use password kind of thing ??

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  4 years ago, # ^ |
                  Rev. 2   Vote: I like it 0 Vote: I do not like it

                  yes, but it will slow down the script. Maybe I'll implement tomorrow. Also I suspect Igorjan94 already encoded it in this vim plugin (great job, by the way)

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  4 years ago, # ^ |
                    Vote: I like it 0 Vote: I do not like it

                  Thank you, and I hope it will work for me!!

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  4 years ago, # ^ |
                    Vote: I like it 0 Vote: I do not like it

                  And I am trying to develop a similar kind of plugin(parsing test cases, testing, and submitting code, ...) but in sublime.

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  4 years ago, # ^ |
                    Vote: I like it 0 Vote: I do not like it

                  ========================================================== No, unfortunately I haven't done it automatically or by login/password. I had an idea to parse cookies by python, but it isn't trivial. Now I use the same mechanism. To be honest, I use your code to submit :)

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  4 years ago, # ^ |
                    Vote: I like it 0 Vote: I do not like it

                  For "requests" you can use "session" object to automatically keep track of cookies. And anyway it's not hard to get cookies from response object.

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  4 years ago, # ^ |
                    Vote: I like it 0 Vote: I do not like it

                  ========================================================== yes, but I just wanted to parse browser cookies and not disturb user by any information, which is not one-time-defining
                  maybe enter login/pass would be more convenient, rather than copying values to .vimrc/script

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it
»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

I've used your script here but i found that it doesn't send any thing to be submitted, it just tell me what was my last submission verdict (not that one currently submitted).

  • »
    »
    3 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    just updated my blog post

    • »
      »
      »
      3 years ago, # ^ |
      Rev. 3   Vote: I like it 0 Vote: I do not like it

      Try these lines. Also you should add some variables from cookies as described in my plugin readme(csrf_token, jsessionid, weird(39ce7), user_agent, x_user) and params of submit should be cookies = {'X-User' : x_user, 'JSESSIONID' : jsessionid, '39ce7' : weird}; headers = {'User-Agent' : user_agent}
      It doesn't work during one month as before, but works at least one-two days
      Hope it helps

      • »
        »
        »
        »
        3 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        I've tried what you wrote but now, it don't compile.

        (NameError: name 'jsessionid' is not defined)

        • »
          »
          »
          »
          »
          3 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          so define it :)
          for example:

          jsessionid = [x] * 32 -n1
          user_agent = "Opera/9.80 (X11; Linux x86_64) Presto/2.12.388 Version/12.16"
          wierd = CF [x] * 6
          x_user = [x] * 92
          csrf_token = [x] * 32
          

          (digits -- length of variable)
          Also I forgot to thank marat.snowbear for this solution

          • »
            »
            »
            »
            »
            »
            3 years ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            i'm sorry but what is [x] refering to ?

            • »
              »
              »
              »
              »
              »
              »
              3 years ago, # ^ |
                Vote: I like it 0 Vote: I do not like it

              to nothing. simply char
              my csrf_token is '59a6be04afa5f4e219c287cc91fb5190', wierd is 'CFylualV', for example and so on

      • »
        »
        »
        »
        3 years ago, # ^ |
        Rev. 2   Vote: I like it 0 Vote: I do not like it

        Actually, nowadays, X-User is empty. The new cookies or variables that they send for a submit form in a post request, are ('ftaa' and 'bfaa') with csrf_token. The first one is generated randomly (18-length base36) and the second one is from a fingerprint2 js library. So, it's not trivial to get the second one. And after some tests, It does not work to send arbitrary values of those (with presentation). Any idea?

        • »
          »
          »
          »
          »
          3 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          I have no idea, but I wish CountZero or marat.snowbear know how it can be fixed.
          Anyway I expect MikeMirzayanov doesn't want to prevent submitting by scripts and it will be implemented soon in API in secure way

          • »
            »
            »
            »
            »
            »
            3 years ago, # ^ |
              Vote: I like it +3 Vote: I do not like it

            actually I have an idea how to deal with this stuff. I'll test it in the nearest future, so stay tuned