-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
118 lines (108 loc) Β· 3.2 KB
/
server.py
File metadata and controls
118 lines (108 loc) Β· 3.2 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
from flask import Flask, send_from_directory, render_template_string, redirect, url_for
from pathlib import Path
from datetime import datetime, timedelta
import threading
import time
import subprocess
app = Flask(__name__)
COLLAGE_DIR = Path("collages")
CACHE_DIR = Path("rss_cache") # directory where rss_*.xml files are cached
HTML_TEMPLATE = """
<!DOCTYPE html>
<html>
<head>
<title>Goodreads Collages</title>
<style>
body {
font-family: sans-serif;
background: #111;
color: #eee;
text-align: center;
}
.gallery {
display: flex;
flex-wrap: wrap;
justify-content: center;
padding: 20px;
}
.gallery a {
margin: 10px;
}
.gallery img {
width: 200px;
border: 2px solid #444;
}
button {
margin: 20px;
padding: 10px 20px;
font-size: 16px;
background-color: #444;
color: #eee;
border: none;
cursor: pointer;
}
button:hover {
background-color: #666;
}
</style>
</head>
<body>
<h1>π Goodreads Collages</h1>
<form method="post" action="/refresh">
<button type="submit">Refresh</button>
</form>
<div class="gallery">
{% for filename in files %}
<a href="/collages/{{ filename }}">
<img src="/collages/{{ filename }}" alt="{{ filename }}">
</a>
{% endfor %}
</div>
</body>
</html>
"""
def run_daily_task():
while True:
try:
print("π
Running app.py task...")
subprocess.run(["python", "app.py"], check=True)
print("β
app.py completed")
except Exception as e:
print(f"β Error running app.py: {e}")
time.sleep(24 * 60 * 60)
@app.route("/")
def index():
COLLAGE_DIR.mkdir(exist_ok=True)
files = sorted(
[f.name for f in COLLAGE_DIR.glob("*.jpg")],
reverse=True
)
return render_template_string(HTML_TEMPLATE, files=files)
@app.route("/refresh", methods=["POST"])
def refresh():
print("π Refresh triggered by user")
CACHE_DIR.mkdir(exist_ok=True)
cutoff = datetime.utcnow() - timedelta(hours=24)
# Delete XML files newer than 24h
for file in CACHE_DIR.glob("rss_*.xml"):
mtime = datetime.utcfromtimestamp(file.stat().st_mtime)
if mtime > cutoff:
print(f"π Deleting recent cache file: {file}")
try:
file.unlink()
except Exception as e:
print(f"β Failed to delete {file}: {e}")
# Run app.py once to re-fetch and re-generate collages
try:
subprocess.run(["python", "app.py"], check=True)
print("β
app.py ran successfully")
except Exception as e:
print(f"β Error running app.py: {e}")
# Redirect back to the homepage
return redirect(url_for("index"))
@app.route("/collages/<path:filename>")
def collage_file(filename):
return send_from_directory(COLLAGE_DIR, filename)
if __name__ == "__main__":
threading.Thread(target=run_daily_task, daemon=True).start()
app.run(host="0.0.0.0", port=5000)