Compare two SPOJ users
By Arpit Bhayani on June 07, 2015
Whenever you are practicing questions on SPOJ you tend to follow some user. Since you are following him/her you need to look for the questions that were solved by him/her and not by you. So Instead of manually looking at all the question codes, you can simply execute this script.
The following python script will tell you which questions the other user solved and you didn't. Instead of spending a lot of time checking every question code if it has been solved by you or not; you can simply execute following script and find out the answer ;)
Prerequisite :
  • python2.7
  • urllib2
  • BeautifulSoup4
To install BeautifulSoup4 (140KB), execute the lines below
sudo pip install beautifulsoup4
To execute the script, follow the following steps:
python spojcompare.py <USERNAME1> <USERNAME2>
It will output all the questions solved by USERNAME2 and not by USERNAME1
So if you are comparing yourself to other user, you should execute it as
python spojcompare.py <YOUR_USER_NAME> <OTHER_USER_NAME>
"""
    Author: Arpit Bhayani
    Require
        1. urllib2
        2. beautifulsoup4
"""
from bs4 import BeautifulSoup
import urllib2
import sys

"""
    Returns BeautifulSoup4 object from username
"""
def fetch(username):
    d = urllib2.urlopen('http://www.spoj.com/users/' + username).read()
    return BeautifulSoup(d)

def get_codes(u, d):
    solved = set()

    try:
        tables = d.find(id='user-profile-tables').find_all('table')
    except AttributeError:
        print "Invalid Username : ", u
        exit()
    
    for i in tables[0].find_all('a'):
        code = i.get_text().strip()
        if code != '':
            solved.add(code)

    return solved

def compare(user1, user2):
    d1 = fetch(user1)
    d2 = fetch(user2)

    solved1 = get_codes(user1, d1)
    solved2 = get_codes(user2, d2)

    for i in solved2.difference(solved1):
        print i


if len(sys.argv) < 3:
    print "Usage: python spojcompare.py <USERNAME1> <USERNAME2>"
    exit();

compare(sys.argv[1], sys.argv[2])
If you liked the script then do share it ;)