-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
62 lines (52 loc) · 2.36 KB
/
app.py
File metadata and controls
62 lines (52 loc) · 2.36 KB
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
from flask import Flask, render_template, request, jsonify
import requests
import re
app = Flask(__name__)
# Professional User-Agent string as requested by Wikimedia best practices
HEADERS = {'User-Agent': 'Outreachy-Smart-Auditor/1.0 (shehrbanoali2230@gmail.com)'}
@app.route('/')
def index():
return render_template('index.html')
@app.route('/check_status', methods=['POST'])
def check_status():
url = request.json.get('url')
try:
# Timeout set to 5s to prevent the app from hanging on slow Lusophone servers
res = requests.get(url, headers=HEADERS, timeout=5)
# This will send 200 OK or 404 Not Found to my website terminal
return jsonify({'status': f"{res.status_code} {res.reason}"})
except requests.exceptions.RequestException as e:
# This will help us capturing the name of the error (like ConnectionError or Timeout):
error_name = type(e).__name__
return jsonify({'status': f'FAILED ({error_name})'})
@app.route('/get_score', methods=['POST'])
def get_score():
url = request.json.get('url')
qid = None
# Extracting QID using Regex for Wikidata or local museum links
if "wikidata.org" in url:
match = re.search(r"Q\d+", url)
if match:
qid = match.group()
elif "museudofutebol.org.br" in url:
match = re.search(r"personalidades/(\d+)", url)
if match:
qid = f"Q{match.group(1)}"
if qid:
api_url = f"https://www.wikidata.org/w/api.php?action=wbgetentities&ids={qid}&format=json"
try:
res = requests.get(api_url, headers=HEADERS, timeout=5).json()
item = res.get('entities', {}).get(qid, {})
# Metadata Scoring Logic
score = (1 if item.get('labels') else 0) + \
(1 if item.get('descriptions') else 0) + \
len(item.get('claims', {}))
return jsonify({'qid': qid, 'score': score})
except requests.exceptions.RequestException as e:
# Updated the API error reporting
error_name = type(e).__name__
print(f"Wikidata API Error: {error_name}")
return jsonify({'score': 0, 'error': error_name})
return jsonify({'score': None})
if __name__ == '__main__':
app.run(debug=True)