forked from Armankb2/scholar_fetchv1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
94 lines (82 loc) · 3.32 KB
/
app.py
File metadata and controls
94 lines (82 loc) · 3.32 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
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
93
94
#!/usr/bin/env python3
from flask import Flask, request, send_file, render_template_string, redirect, url_for
import os
import threading
from fetch_scholar import fetch_author_by_name_or_id, store_author_and_pubs
app = Flask(__name__)
DOWNLOAD_DIR = "downloads"
# Create folder if not exists
os.makedirs(DOWNLOAD_DIR, exist_ok=True)
# --- HTML Template ---
HTML_TEMPLATE = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Scholar Fetcher</title>
<style>
body { font-family: 'Segoe UI', sans-serif; background: #fafafa; text-align: center; margin-top: 80px; color: #333; }
h1 { color: #222; }
input { padding: 10px; width: 300px; border-radius: 5px; border: 1px solid #bbb; }
button { padding: 10px 20px; border-radius: 5px; border: none; background: #0078D4; color: white; cursor: pointer; }
button:hover { background: #005a9e; }
.msg { margin-top: 30px; color: #008000; font-weight: bold; }
.error { color: #cc0000; }
</style>
</head>
<body>
<h1>🎓 Google Scholar Data Fetcher</h1>
<form method="POST" action="/fetch">
<input name="scholar_id" placeholder="Enter Scholar ID (e.g. daxojLsAAAAJ)" required>
<button type="submit">Fetch Data</button>
</form>
{% if message %}
<div class="msg">{{ message }}</div>
{% endif %}
{% if download_link %}
<div class="msg">📁 <a href="{{ download_link }}">Download CSV</a></div>
{% endif %}
</body>
</html>
"""
@app.route("/", methods=["GET"])
def home():
return render_template_string(HTML_TEMPLATE)
@app.route("/fetch", methods=["POST"])
def fetch():
scholar_id = request.form.get("scholar_id").strip()
if not scholar_id:
return render_template_string(HTML_TEMPLATE, message="❌ Please enter a valid Scholar ID")
filename = f"{scholar_id.lower()}_publications.csv"
filepath = os.path.join(DOWNLOAD_DIR, filename)
def process_scholar():
author_filled = fetch_author_by_name_or_id(scholar_id)
if author_filled:
# Run fetch and store
store_author_and_pubs(author_filled)
# Move CSV to downloads directory
csv_src = f"{author_filled['name'].replace(' ', '_').lower()}_publications.csv"
if os.path.exists(csv_src):
os.rename(csv_src, filepath)
else:
print(f"[!] Author not found for {scholar_id}")
# Run fetch in background
thread = threading.Thread(target=process_scholar)
thread.start()
message = f"✅ Fetching data for Scholar ID <b>{scholar_id}</b>. Please wait a minute..."
download_link = url_for("download", filename=filename)
return render_template_string(HTML_TEMPLATE, message=message, download_link=download_link)
@app.route("/download/<filename>")
def download(filename):
filepath = os.path.join(DOWNLOAD_DIR, filename)
if os.path.exists(filepath):
return send_file(filepath, as_attachment=True)
else:
return render_template_string(HTML_TEMPLATE, message="⏳ File not ready yet. Try again in a moment.")
if __name__ == "__main__":
import socket
hostname = socket.gethostname()
local_ip = socket.gethostbyname(hostname)
print(f"\n🌐 Access this app on your Wi-Fi: http://{local_ip}:5050\n")
print("💡 Others on your Wi-Fi can open this same link in their browser.")
app.run(host="0.0.0.0", port=5050, debug=True)