-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrootmeapi.py
54 lines (45 loc) · 1.94 KB
/
rootmeapi.py
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
from flask import Flask, jsonify, request
import requests
app = Flask(__name__)
def scraping_rootme(username):
url = f'https://www.root-me.org/{username}'
response = requests.get(url)
if response.status_code == 404:
return {
"userName": "Unknown",
"userRank": "Unknown",
"userScore": "Unknown",
"userChallenge": "Unknown",
"totalChallenge": "Unknown",
"userPercent": "Unknown"
}
html = response.text
userName = username
userRank = userScore = userChallenge = totalChallenge = userPercent = "Unknown"
for line in html.splitlines():
if "<h3><img src='squelettes/img/classement.svg?1647504561' width='24' height='24' /> " in line:
userRank = line.split(" ")[1].split("</h3>")[0].strip()
if "<h3><img src='squelettes/img/valid.svg?1566650916' width='24' height='24' /> " in line:
userScore = line.split(" ")[1].split("</h3>")[0].strip()
if "<h3><img src='IMG/logo/rubon5.svg?1637496507' width='24' height='24' /> " in line:
userChallenge = line.split(" ")[1].split("</h3>")[0].strip()
if '<span class="gris">/' in line:
totalChallenge = line.split('<span class="gris">/')[1].split('</span>')[0].strip()
if "<h3 class=\"text-center\">" in line:
userPercent = line.split("<h3 class=\"text-center\">")[1].split(" ")[0].strip()
return {
"userName": userName,
"userRank": userRank,
"userScore": userScore,
"userChallenge": userChallenge,
"totalChallenge": totalChallenge,
"userPercent": userPercent
}
@app.route('/api', methods=['GET'])
def get_rootme_stats():
username = request.args.get('username')
if not username:
return jsonify({"error": "Username is required"}), 400
return scraping_rootme(username)
if __name__ == '__main__':
app.run(debug=True)