diff --git a/main.py b/main.py index 76a69c0..a029a71 100644 --- a/main.py +++ b/main.py @@ -3,11 +3,13 @@ from flask import Flask, jsonify, request from linkedin_api import Linkedin from huggingface_hub import InferenceClient +from flask_cors import CORS client = InferenceClient( "mistralai/Mistral-7B-Instruct-v0.1" ) app = Flask(__name__) +CORS(app) linkedin = Linkedin("snehithb295@gmail.com", "Test@476") @@ -135,6 +137,134 @@ def scrape_github_profiles(): else: return None +@app.route('/scrape_leetcode_profiles/user/',methods = ['GET']) +def scrape_leetcode_profile(): + try: + username = str(request.args.get("user")) + print(username) + base_url = "https://leetcode.com/" + + final_url = base_url + username + + html_text = requests.get(final_url).text + + soup = BeautifulSoup(html_text, "lxml") + + username = get_value(soup, "div", "text-label-3 dark:text-dark-label-3 text-xs") + + candidate_name = get_value( + soup, + "div", + "text-label-1 dark:text-dark-label-1 break-all text-base font-semibold", + ) + + candidate_rank = get_value( + soup, "span", "ttext-label-1 dark:text-dark-label-1 font-medium" + ) + + contest_attended = get_value( + soup, + "div", + "text-label-1 dark:text-dark-label-1 font-medium leading-[22px]", + index=1, + ) + + contest_rating = get_value( + soup, + "div", + "text-label-1 dark:text-dark-label-1 flex items-center text-2xl", + ) + + contest_global_ranking = get_value( + soup, + "div", + "text-label-1 dark:text-dark-label-1 font-medium leading-[22px]", + ) + + total_problem_solved = get_value( + soup, + "div", + "text-[24px] font-medium text-label-1 dark:text-dark-label-1", + ) + + problems_solved = soup.find_all( + "span", + class_="mr-[5px] text-base font-medium leading-[20px] text-label-1 dark:text-dark-label-1", + ) + + language_used = soup.find_all( + "span", + class_="inline-flex items-center px-2 whitespace-nowrap text-xs leading-6 rounded-full text-label-3 dark:text-dark-label-3 bg-fill-3 dark:bg-dark-fill-3 notranslate", + ) + + solved_problem = soup.find_all( + "span", class_="text-label-1 dark:text-dark-label-1 font-medium line-clamp-1" + ) + + topics_covered = soup.find_all( + "span", + class_="inline-flex items-center px-2 whitespace-nowrap text-xs leading-6 rounded-full bg-fill-3 dark:bg-dark-fill-3 cursor-pointer transition-all hover:bg-fill-2 dark:hover:bg-dark-fill-2 text-label-2 dark:text-dark-label-2", + ) + + badges_earned = soup.find_all( + "img", class_="h-full w-full cursor-pointer object-contain" + ) + + most_recent_badge = get_value( + soup, "div", "text-label-1 dark:text-dark-label-1 text-base" + ) + + language_used_list = [language.text for language in language_used] + + solved_problem_list = [problem.text for problem in solved_problem] + + topics_covered_list = [topic.text for topic in topics_covered] + + badges_earned_list = [badge["alt"] for badge in badges_earned] + + solved_problem_json = json.dumps(solved_problem_list) + topics_covered_json = json.dumps(topics_covered_list) + badges_earned_json = json.dumps(badges_earned_list) + language_used_json = json.dumps(language_used_list) + + data_set = { + "LeetCodeUsername": username, + "CandidateName": candidate_name, + "CandidateRank": candidate_rank, + "ContestAttended": contest_attended, + "ContestRating": contest_rating, + "ContestGlobalRanking": contest_global_ranking, + "TotalProblemsSolved": total_problem_solved, + "EasyProblem": get_list_value(problems_solved, 0), + "MediumProblem": get_list_value(problems_solved, 1), + "HardProblem": get_list_value(problems_solved, 2), + "MostRecentlyEarnedBadge": most_recent_badge, + "Last15SolvedProblems": solved_problem_json, + "TopicsCovered": topics_covered_json, + "BadgesEarned": badges_earned_json, + "LanguageUsed": language_used_json, + } + + json_dump = json.dumps(data_set) + return json_dump + + except Exception as e: + error_message = f"Error: {str(e)}" + return json.dumps({"error": error_message}) + + +def get_value(soup, tag, class_name, index=0): + try: + return soup.find(tag, class_=class_name).text + except (AttributeError, IndexError): + return None + + +def get_list_value(lst, index): + try: + return lst[index].text + except (IndexError, AttributeError): + return None @app.route('/scrape_medium_profiles', methods=['POST']) def scrape_medium_profiles():