Skip to content

Instantly share code, notes, and snippets.

@st98
Created August 6, 2017 00:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save st98/a9da8d478553b42f1d64d5b2867ecfac to your computer and use it in GitHub Desktop.
Save st98/a9da8d478553b42f1d64d5b2867ecfac to your computer and use it in GitHub Desktop.
Compare teams' solved problems (for Mellivora)
import re
import requests
import urlparse
from bs4 import BeautifulSoup
def is_number(s):
if not s.isdigit():
raise argparse.ArgumentTypeError('{0} is not a number'.format(s))
return s
def get_content(url, team):
url = urlparse.urljoin(url, 'user?id=' + team)
r = requests.get(url).content
return r
def get_name(s):
obj = BeautifulSoup(s, 'html.parser')
return obj.find('h2', {'class': 'page-header'}).get_text().strip()
_m = re.compile(r'\s*(.+)\s*\n\s*\((.+)\)\s*')
def get_solves(s):
obj = BeautifulSoup(s, 'html.parser')
table = obj.find('table')
tbody = table.find('tbody')
trs = tbody.find_all('tr')
res = set()
for tr in trs:
columns = []
for td in tr.find_all('td'):
columns.append(td.get_text().encode('utf-8'))
a, _, points = columns
name, category = _m.findall(a)[0]
res.add('[{1} {2}] {0}'.format(name, category, points))
return res
if __name__ == '__main__':
import argparse
import sys
parser = argparse.ArgumentParser(description='CTFd')
parser.add_argument('url', nargs='?', default='https://ctf.sha2017.org')
parser.add_argument('team1', type=is_number)
parser.add_argument('team2', type=is_number)
args = parser.parse_args()
c = get_content(args.url, args.team1)
team1 = {
'name': get_name(c),
'solves': get_solves(c)
}
c = get_content(args.url, args.team2)
team2 = {
'name': get_name(c),
'solves': get_solves(c)
}
print '{0} - {1}'.format(team1['name'], team2['name'])
for problem in team1['solves'] - team2['solves']:
print '\t{0}'.format(problem)
print
print '{1} - {0}'.format(team1['name'], team2['name'])
for problem in team2['solves'] - team1['solves']:
print '\t{0}'.format(problem)
beautifulsoup4
requests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment