Last active
July 19, 2020 06:24
-
-
Save marccarre/599ecdfaf021bb5408442e2f3ed7d652 to your computer and use it in GitHub Desktop.
leetcode-ranking.py
This file contains hidden or 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
#!/usr/bin/env python | |
''' | |
Usage: | |
./leetcode-ranking.py -c 198 marccarre Hydromail daisuke834 | |
2400 marccarre 96 | |
2895 Hydromail 116 | |
6666 daisuke834 267 | |
Prerequisites: | |
- pipenv install click==7.1.2 | |
- pipenv install requests==2.24.0 | |
''' | |
import click | |
import requests | |
from itertools import count | |
URL = 'https://leetcode.com/contest/api/ranking/weekly-contest-%(contest)d/?pagination=%(page)d®ion=global' | |
@click.command() | |
@click.option('-c', '--contest', required=True, type=int, help='Leetcode contest number') | |
@click.option('-s', '--start', default=1, type=int, help='Page to start from') | |
@click.argument('usernames', required=True, nargs=-1) | |
def main(contest, start, usernames): | |
usernames = set([u.lower() for u in usernames]) | |
for page in count(start=start): | |
data = requests.get(URL % {'contest': contest, 'page': page}).json() | |
if not data['total_rank']: | |
break | |
for row in data['total_rank']: | |
if row['username'].lower() in usernames: | |
print(row['rank'], row['username'], page) | |
usernames.discard(row['username'].lower()) | |
if not usernames: | |
return | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment