-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from varundeepsaini/22-feat-add-daily-challenges
feat: add daily challenges
- Loading branch information
Showing
5 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
from discord.ext import commands | ||
from discord import Interaction, ApplicationContext | ||
from codeforces_api import CodeforcesApi | ||
from datetime import datetime | ||
from utils.mongo import problems_collection, user_collection | ||
from utils.codeforces import getTwoProblems | ||
from requests import get | ||
|
||
|
||
class DailyCP(commands.Cog): | ||
def __init__(self, bot: commands.Bot): | ||
self.bot = bot | ||
self.cf_api = CodeforcesApi() | ||
|
||
@commands.slash_command(description="Get your daily challenge") | ||
async def daily_cp(self, ctx: Interaction | ApplicationContext): | ||
await ctx.response.defer() | ||
user_id = ctx.author.id | ||
dateAndTime = datetime.now() | ||
date = dateAndTime.strftime("%d/%m/%Y") | ||
user_obj = user_collection.find_one({"discord_id": user_id}) | ||
if user_obj is None: | ||
await ctx.respond("First you need to register using `/register` command") | ||
today_problems = problems_collection.find_one( | ||
{"discord_id": user_id, "date": date} | ||
) | ||
if today_problems is not None: | ||
given_problems = today_problems["problems"] | ||
msg = f"problem 1 - {given_problems[0]['link']}\nproblem 1 - {given_problems[1]['link']}" | ||
await ctx.respond(msg) | ||
return | ||
username = user_obj["username"] | ||
url = f"https://codeforces.com/api/user.info?handles={username}" | ||
formatted_response = get(url) | ||
if formatted_response.status_code != 200: | ||
await ctx.respond("Oopsie, an error occurred") | ||
return | ||
try: | ||
data = formatted_response.json() | ||
except ValueError: | ||
await ctx.respond("Oopsie, an error occurred") | ||
return | ||
if "rating" not in data["result"][0]: | ||
await ctx.respond("Skill Issue: you are unrated") | ||
return | ||
new_problems = getTwoProblems(username, data["result"][0]["rating"]) | ||
new_problems_doc = { | ||
"discord_id": user_id, | ||
"problems": [ | ||
{ | ||
"contestId": new_problems[0]["contestId"], | ||
"index": new_problems[0]["index"], | ||
"link": f"https://codeforces.com/problemset/problem/{new_problems[0]['contestId']}/{new_problems[0]['index']}", | ||
}, | ||
{ | ||
"contestId": new_problems[1]["contestId"], | ||
"index": new_problems[1]["index"], | ||
"link": f"https://codeforces.com/problemset/problem/{new_problems[1]['contestId']}/{new_problems[1]['index']}", | ||
}, | ||
], | ||
"date": date, | ||
} | ||
problems_collection.insert_one(new_problems_doc) | ||
msg = f"problem 1 - {new_problems_doc['problems'][0]['link']}\nproblem 2 - {new_problems_doc['problems'][1]['link']}" | ||
await ctx.respond(msg) | ||
|
||
|
||
def setup(bot): | ||
bot.add_cog(DailyCP(bot)) |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import requests | ||
|
||
|
||
def getTwoProblems(username, rating): | ||
rounded_rating = round(rating / 100) * 100 | ||
higher_rating = rounded_rating + 200 | ||
|
||
submissions_url = ( | ||
f"https://codeforces.com/api/user.status?handle={username}&from=1&count=1000000" | ||
) | ||
response = requests.get(submissions_url) | ||
data = response.json() | ||
if data["status"] != "OK": | ||
return [] | ||
|
||
solved_problems = set() | ||
for submission in data["result"]: | ||
if submission["verdict"] == "OK": | ||
problem_id = ( | ||
submission["problem"]["contestId"], | ||
submission["problem"]["index"], | ||
) | ||
solved_problems.add(problem_id) | ||
|
||
problems_url = "https://codeforces.com/api/problemset.problems" | ||
response = requests.get(problems_url) | ||
data = response.json() | ||
if data["status"] != "OK": | ||
return [] | ||
|
||
selected_problems = [] | ||
for problem in data["result"]["problems"]: | ||
if "rating" not in problem: | ||
continue | ||
if ( | ||
problem["rating"] == rounded_rating | ||
and (problem["contestId"], problem["index"]) not in solved_problems | ||
): | ||
selected_problems.append(problem) | ||
if len(selected_problems) == 2: | ||
break | ||
elif ( | ||
problem["rating"] == higher_rating | ||
and (problem["contestId"], problem["index"]) not in solved_problems | ||
): | ||
selected_problems.append(problem) | ||
if len(selected_problems) == 2: | ||
break | ||
|
||
return selected_problems |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
|
||
load_dotenv() | ||
DISCORD_BOT_TOKEN = environ.get("DISCORD_BOT_TOKEN") | ||
MONGO_URI = environ.get("MONGO_URI") |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from pymongo import MongoClient, database | ||
from .env import MONGO_URI | ||
|
||
client = MongoClient(MONGO_URI) | ||
db = client.saini_certified | ||
user_collection = db.users | ||
problems_collection = db.problems |