Last active
February 22, 2023 13:49
-
-
Save is2js/3c32a75986e6f17364c39e6fa2a05cbc to your computer and use it in GitHub Desktop.
백준 사이트의 비공식 api를 이용하여 특정user의 정보들을 반환하는 요청 함수 3개를 정의함.
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 json | |
import requests | |
def get_profile(user_id): | |
""" | |
정보 조회 - user_id를 입력하면 백준 사이트에서 해당 user의 프로필 정보 중 일부를 반환해줌. | |
:param str user_id: 사용자id | |
:return: 백준 프로필 정보 | |
:rtype: dict | |
""" | |
url = f"https://solved.ac/api/v3/user/show?handle={user_id}" | |
r_profile = requests.get(url) | |
if r_profile.status_code == requests.codes.ok: | |
profile = json.loads(r_profile.content.decode('utf-8')) | |
profile = \ | |
{ | |
"tier" : profile.get("tier"), | |
"rank" : profile.get("rank"), | |
"solvedCount" : profile.get("solvedCount"), | |
"rating" : profile.get("rating"), | |
"exp" : profile.get("exp"), | |
} | |
else: | |
print("프로필 요청 실패") | |
return profile | |
def get_solved(user_id): | |
""" | |
정보 조회 - user_id를 입력하면 백준 사이트에서 해당 user가 푼 총 문제수, 문제들 정보(level 높은 순)를 튜플(int, list)로 반환해줌. | |
:param str user_id: 사용자id | |
:return: 내가 푼 문제수, 내가 푼 문제들 정보 | |
:rtype: int, list | |
""" | |
url = f"https://solved.ac/api/v3/search/problem?query=solved_by%3A{user_id}&sort=level&direction=desc" | |
r_solved = requests.get(url) | |
if r_solved.status_code == requests.codes.ok: | |
solved = json.loads(r_solved.content.decode('utf-8')) | |
count = solved.get("count") | |
items = solved.get("items") | |
solved_problems = [] | |
for item in items: | |
solved_problems.append( | |
{ | |
'problemId': item.get("problemId"), | |
'titleKo': item.get("titleKo"), | |
'level': item.get("level"), | |
} | |
) | |
# print("푼 문제수와 젤 고난이도 문제 1개만 >>>", count, solved_problems[0]) | |
else: | |
print("푼 문제들 요청 실패") | |
return count, solved_problems | |
def get_count_by_level(user_id): | |
""" | |
정보 조회 - user_id를 입력하면 백준 사이트에서 해당 user가 푼 문제들에 대한 level별 문제수 정보를 level 높은 순으로 반환해줌. | |
:param str user_id: 사용자id | |
:return: level별 총 문제수, 내가 푼 문제수 | |
:rtype: list | |
""" | |
url = f"https://solved.ac/api/v3/user/problem_stats?handle={user_id}" | |
r_count_by_level = requests.get(url) | |
if r_count_by_level.status_code == requests.codes.ok: | |
count_by_level = json.loads(r_count_by_level.content.decode('utf-8')) | |
filted_count_by_level = [ {"level":dict_['level'], "total":dict_['total'], "solved":dict_['solved'],} for dict_ in count_by_level if dict_.get('solved') != 0 ] | |
filted_count_by_level = sorted(filted_count_by_level, key=lambda x:x['level'], reverse=True) | |
else: | |
print("레벨별, 전체 문제수, 푼 문제수 요청 실패") | |
return filted_count_by_level | |
user_id = "tingstyle1" | |
profile_dict = get_profile(user_id) | |
print(f"========{user_id}님의 프로필========") | |
print(profile_dict) | |
count, sovled_list = get_solved(user_id) | |
print(f"========{user_id}님이 푼 문제들({count})========") | |
print(sovled_list) | |
print(f"========{user_id}님이 푼 문제들의 레벨별 갯수========") | |
count_by_level_list = get_count_by_level(user_id) | |
print(count_by_level_list) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
이런 종류의 API가 있는지 처음 알아서 코드를 실행해 보는 동안 그리고 Aritable에 올려주신 API 문서를 읽어보는 동안 정말 즐겁게 시간을 보낸 거 같아요. 무엇보다도 주석을 꼼꼼하게 달아두셔서 코드를 이해하기 정말로 쉬었습니다! 솔직히 너무 잘하셔서 제가 보기에는 개선할 부분이 없는 거 같습니다. 가져오신 데이터를 어디에 어떻게 사용하실지 개인적으로 정말로 궁금합니다!