Skip to content

Instantly share code, notes, and snippets.

@EarthMessenger
Created January 2, 2024 13:24
Show Gist options
  • Save EarthMessenger/4582e889deb86fdc62cee5f20f7e0bf2 to your computer and use it in GitHub Desktop.
Save EarthMessenger/4582e889deb86fdc62cee5f20f7e0bf2 to your computer and use it in GitHub Desktop.
crawl problems titles on cwoi
#!/usr/bin/env python3
import itertools
import requests
import time
CWOI_HOST = 'https://local.cwoi.com.cn:8443'
CWOI_TOKEN = ''
CRAWL_INTERVAL = 0.5
authorized_header = {
'Authorization': f'Bearer {CWOI_TOKEN}'
}
contest_ids = []
for i in itertools.count(1):
res = requests.get(f'{CWOI_HOST}/api/contests?page={i}&pageSize=50', headers=authorized_header)
res.raise_for_status()
data = res.json()
for c in data['rows']:
contest_ids.append((c['contestDisplayId'], c['_id']))
if data['page'] == data['totalPages']:
break
time.sleep(CRAWL_INTERVAL)
for id1, id2 in contest_ids:
res = requests.get(f'{CWOI_HOST}/api/contest/id/{id2}/inContest', headers=authorized_header)
if res.text == 'false':
res = requests.post(f'{CWOI_HOST}/api/contest/{id2}/join', headers=authorized_header, json={ 'vp': True })
res = requests.get(f'{CWOI_HOST}/api/contest/displayId/{id1}', headers=authorized_header)
c = res.json()
print(c['contestDisplayId'], c['contestTitle'], c['startedAt'], c['endedAt'], c['groups'])
for p in c['problems']:
print(p['displayId'], p['problemTitle'])
time.sleep(CRAWL_INTERVAL)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment