utils.py 3.33 KB
Newer Older
ashivani's avatar
ashivani committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
import requests
import html
import markdown
import yaml
import os

class Utils():
    def __init__(self):
        self.base_url = "https://www.aicrowd.com/api/v1/challenges"
        self.api_key = os.getenv("AICROWD_API_KEY")
        self.headers = {'Authorization': 'Token token="' + self.api_key +'"', 'Content-Type': 'application/json'}
        
        with open("aicrowd.yaml", "r") as f:
            self.challenge = yaml.safe_load(f)['challenge_name']
        
        with open("README.md", "r") as f:
            self.content = f.read()
        self.convert()

    def convert(self):
        self.content = markdown.markdown(self.content)
        self.content = self.content.replace("\"","'")
        self.content = self.content.replace("h1","h2")
        html.unescape(self.content)
    
    def create_challenge(self):
        payload = {
                "challenge": self.challenge.upper(),
                "status_cd": "draft",
                "tagline": "Assemble The Puzzle Pieces",
                "perpetual_challenge": False,
                "answer_file_s3_key": None,
                "slug": "cptcha-identify-the-captcha",
                "submission_license": "",
                "api_required": False,
                "media_on_leaderboard": False,
                "online_grading": True,
                "description": self.content,
                "evaluation": None,
                "rules": None,
                "prizes": None,
                "resources": None,
                "submission_instructions": None,
                "subissions_page": True,
                "license": "",
                "dataset_description": None,
                "show_leaderboard": True,
                "grader_identifier": "AIcrowd_GRADER_POOL",
                "online_submissions": True,
                "grader_logs": False,
                "require_registration": False,
                "grading_history": False,
                "post_challenge_submissions": True,
                "submissions_downloadable": False,
                "dataset_note": None,
                "discussions_visible": True,
                "latest_submission": False,
                "other_scores_fieldnames": "",
                "teams_allowed": True,
                "max_team_participants": 5,
                "team_freeze_seconds_before_end": 604800,
                "hidden_challenge": False,
                "team_freeze_time": None,
                "score_title": "",
                "score_secondary_title": "",
                "primary_sort_order_cd": "",
                "secondary_sort_order_cd": "",
                "challenges_organizers_attributes": [{"organizer_id": 9, "challenge_id": 1}],
                "category_challenges_attributes": [{"category_id": "8"}],
            }
        return requests.post(self.base_url, json=payload, headers=self.headers)


    def update_challenge(self):
        payload = {"description": self.content}
        request_url = f"{self.base_url}/{self.challenge}"
        response = requests.patch(request_url, json=payload, headers=self.headers)
        return response

    def update_or_create(self):
        response =  self.update_challenge()
        if response.status_code == 404:
            response = self.create_challenge()
        
        return response



if __name__ == "__main__":
    utils = Utils()
    print(utils.update_or_create().text)