Created
March 31, 2018 06:03
-
-
Save andrealmeid/49d13f9f52493a43fd30a2cc18668932 to your computer and use it in GitHub Desktop.
codepit web scrapper
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 | |
from tabulate import tabulate | |
CODEPIT_LOGIN_URL = "https://www.codepit.io/api/v1/user/login" | |
CREDENTIALS_FILE = "credentials.txt" | |
def main(): | |
""" | |
first of all, lets login on codepit. we'll simple make the same post | |
we make when clicking "login" on the web page. | |
""" | |
print("Codepit scrapper started!") | |
# getting our credentials from the secret file | |
print("Getting our credentials...") | |
with open(CREDENTIALS_FILE, "r") as cred_file: | |
credentials = cred_file.read().strip().split(";") | |
# preparing and sending our POST to Login | |
print("Loging on Codepit...") | |
post_data = { | |
"email": credentials[0], | |
"password": credentials[1] | |
} | |
# we open a session to keep the login cookies | |
session = requests.session() | |
req_post = session.post(url=CODEPIT_LOGIN_URL, data=post_data) | |
if "error" in req_post.text: | |
print("Login failed :(") | |
exit(1) | |
print("Login done!") | |
# now, lets get some data from the scoreboard | |
SCORE_URL = "https://www.codepit.io/api/v1/contest/5ab156d701a96e001940764f/metadata" | |
print("Getting problems...") | |
req_get = session.get(SCORE_URL) | |
score_data = req_get.json() | |
# we now filter some data from the response | |
table_data = [] | |
for problem in score_data['problems']: | |
table_data.append([ | |
problem['name'], | |
problem['timelimit'] | |
]) | |
# and print it in a fancy table way :) | |
table_header = ["Name", "Mem. limit", "Time limit"] | |
print(tabulate(table_data, table_header)) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment