I develop a script to post my code to the UVa online judge
automatically. This tool will also replace the local #include's with
its real content when uploading the code to the server. So, I haven't
to paste the code all around any more.
Curl and Firefox is required to use this script. Firefox stores various information in SQLite databases, thus makes it easy to access it by third-party tools. Your cookies must be passed to the server for validating your login session.
Code:
- #!/bin/bash
- # ************************************************************************* #
- # Copyright (C) 2008 by Liu Kaipeng #
- # LiuKaipeng at gmail dot com #
- # ************************************************************************* #
- source=$1.cpp
- if [ ! -e $source ]; then
- echo $source do not exist.
- exit
- fi
- # replace the local #include's with its file content
- post=$1.post
- cp $source $post
- includes=$(grep '#include ".*"' $post | cut -d/" -f 2)
- for f in $includes; do
- sed -i -e '/@#include "'"$f"'"@{
- r '"$f"'
- d
- }' $post
- done
- problemid="problemid="
- category="category="
- problem="localid=$1"
- language="language=3"
- code="code="
- codeupl="codeupl=@$post"
- refer='http://icpcres.ecs.baylor.edu/onlinejudge/index.php?'/
- 'option=com_onlinejudge&Itemid=25'
- target='http://icpcres.ecs.baylor.edu/onlinejudge/index.php?'/
- 'option=com_onlinejudge&Itemid=25&page=save_submission'
- cookiedb=~/.mozilla/firefox/alv3pwo5.default/cookies.sqlite
- cookie=$(sqlite3 -line $cookiedb /
- 'select name,value from moz_cookies where host glob "*.ecs.baylor.edu";')
- cookie=$(echo ${cookie} | sed 's/name = //g;s/ value = /=/g;s/ /;/g')
- curl --silent --output submission.html /
- --refer "$refer" /
- --cookie "$cookie" /
- --header "Expect:" /
- --header "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.4) "/
- "Gecko/2008111318 Ubuntu/8.10 (intrepid) Firefox/3.0.4" /
- --header "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" /
- --header "Accept-Language: en-us,en;q=0.5" /
- --header "Accept-Encoding: gzip,deflate" /
- --header "Accept-Charset: UTF-8,*" /
- --header "Keep-Alive: 300" /
- --header "Connection: keep-alive" /
- --location --max-redirs 3 /
- --form-string "$problemid" /
- --form-string "$category" /
- --form-string "$problem" /
- --form-string "$language" /
- --form-string "$code" /
- --form "$codeupl" /
- $target