-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.py
63 lines (46 loc) · 1.56 KB
/
entrypoint.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
55
56
57
58
59
60
61
62
63
import json
import os
import threading
import urllib.request
from collections import defaultdict
from flask import Flask, abort, jsonfy, redirect, request
CACHE_PATH = os.getenv("CACHE_PATH", "cache.json")
DEFAULT_SOURCE = os.getenv("DEFAULT_SOURCE", "https://ffxivita.github.io/XIVITADalamudPlugins/")
USER_AGENT = os.getenv("USER_AGENT",
"FFXIVITADalamudPluginsCounter/1.0 (+ https://github.com/FFXIVITA/FFXIVITADalamudPluginsCounter)")
def load_cache():
c = defaultdict(int)
if os.path.exists(CACHE_PATH):
with open(CACHE_PATH) as f:
c.update(json.load(f))
return c
def save_cache():
with open(CACHE_PATH, "w") as f:
json.dump(cache, f, indent=2, sort_keys=True)
cache = load_cache()
cache_lock = threading.Lock()
app = Flask(__name__)
def check_if_exists(url):
try:
req = urllib.request.Request(url, method="HEAD", headers={"User-Agent": USER_AGENT})
with urllib.request.urlopen(req):
return True
except urllib.error.HTTPError:
return False
@app.route("/<any(stable, testing):channel>/<plugin>")
def download(channel, plugin):
source = request.args.get("source", DEFAULT_SOURCE)
url = f"https://{source}/dist/{channel}/{plugin}/latest.zip"
if not check_if_exists(url):
return abort(404)
with cache_lock:
cache[plugin] += 1
save_cache(cache)
return redirect(url)
@app.route("/stats")
def stats():
with cache_lock:
return jsonfy(cache)
@app.route("/")
def index():
return redirect(f"https:/{DEFAULT_SOURCE}")