Skip to content

Instantly share code, notes, and snippets.

@st98
Last active July 30, 2017 11:32
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/b36bae442f5a79f2712303d4ee356ce6 to your computer and use it in GitHub Desktop.
Save st98/b36bae442f5a79f2712303d4ee356ce6 to your computer and use it in GitHub Desktop.
Compare teams' solved problems (for CTFd)
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
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