Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Dec 22, 2023. It is now read-only.

Commit efc066e

Browse files
committed
Add ratings scraper
1 parent 8267627 commit efc066e

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# cf_ratings.py
2+
<!--Remove the below lines and add yours -->
3+
A webscraper that saves a codeforce user's ratings into a CSV.
4+
5+
### Prerequisites
6+
[requests](https://github.com/psf/requests)
7+
8+
### How to run the script
9+
```python cf_ratings -u user1,user2...```
10+
11+
### Screenshot/GIF showing the sample use of the script
12+
![usage](usage.jpg)
13+
14+
## *Author Name*
15+
<!--Remove the below lines and add yours -->
16+
Made with ♥ by [valterm](github.com/valterm)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import json,csv
2+
import requests
3+
import sys,argparse
4+
5+
6+
def call_api(handle):
7+
#API url
8+
base_url='https://codeforces.com/api/user.rating?handle='
9+
#Append the user
10+
url=base_url+handle
11+
12+
r = requests.get(url)
13+
raw = r.json()
14+
15+
response=json.dumps(raw['result'])
16+
17+
return(response)
18+
19+
20+
#Check how many arguments were supplied
21+
if len(sys.argv)>3:
22+
print('Too many arguments.')
23+
exit()
24+
25+
#Create parser
26+
parser = argparse.ArgumentParser(description='Scrape user ratings from codeforce.')
27+
#Add argument for users
28+
parser.add_argument('-u','--users',type=str,help='Comma separated list of users to gather ratings for.')
29+
30+
#Parse arguments
31+
args = parser.parse_args()
32+
users=args.users
33+
34+
#Split the users by commas
35+
u_list = users.split(',')
36+
37+
#Perform this for each user
38+
for u in u_list:
39+
#Call API and parse the response
40+
raw_response=call_api(u)
41+
parsed_data=json.loads(raw_response)
42+
43+
#Set the filename and the columns
44+
csv_name=u+'.csv'
45+
csv_columns=["contestId","contestName","handle","rank","ratingUpdateTimeSeconds","oldRating","newRating"]
46+
47+
#Write to file
48+
with open(csv_name, 'w', encoding='utf-8', newline='') as f:
49+
writer=csv.DictWriter(f,fieldnames=csv_columns)
50+
writer.writeheader()
51+
for data in parsed_data:
52+
writer.writerow(data)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
requests==2.24.0
4.09 KB
Loading[フレーム]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /