Last active
July 30, 2017 11:32
-
-
Save st98/b36bae442f5a79f2712303d4ee356ce6 to your computer and use it in GitHub Desktop.
Compare teams' solved problems (for CTFd)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, 'team/' + team) | |
r = requests.get(url).content | |
return r | |
def get_name(s): | |
obj = BeautifulSoup(s, 'html.parser') | |
return obj.find(id='team-id').get_text() | |
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()) | |
name, category, points, time = columns | |
res.add('[{1} {2}] {0}'.format(name, category, points, time)) | |
return res | |
if __name__ == '__main__': | |
import argparse | |
import sys | |
parser = argparse.ArgumentParser(description='CTFd') | |
parser.add_argument('url', nargs='?', default='http://www.bugsbunnyctf.me') | |
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 '{1} - {0}'.format(team1['name'], team2['name']) | |
for problem in team2['solves'] - team1['solves']: | |
print '\t{0}'.format(problem) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
beautifulsoup4 | |
requests |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment