100 lines
2.4 KiB
Python
100 lines
2.4 KiB
Python
import json
|
|
import os.path
|
|
import random
|
|
import time
|
|
|
|
import requests
|
|
from flask import Flask, render_template
|
|
from flask_apscheduler import APScheduler
|
|
|
|
URL = "https://www.reddit.com/r/InterdimensionalCable/top.json?limit=100&t=week"
|
|
scheduler = APScheduler()
|
|
v = []
|
|
|
|
|
|
def getRedditData(url):
|
|
resp = requests.get(url)
|
|
text = resp.text
|
|
code = resp.status_code
|
|
while code == 429:
|
|
print("waiting for timeout to end ...")
|
|
time.sleep(2)
|
|
resp = requests.get(url)
|
|
text = resp.text
|
|
code = resp.status_code
|
|
|
|
data = json.loads(text)
|
|
token = data["data"]["after"]
|
|
ret = []
|
|
for post in data["data"]["children"]:
|
|
turl = post["data"]["url_overridden_by_dest"]
|
|
if "youtu.be" in turl:
|
|
turl = turl.replace("https://youtu.be/", "https://www.youtube.com/watch?v=")
|
|
if "m.youtube.com" in turl:
|
|
turl = turl.replace(
|
|
"https://m.youtube.com/embed", "https://www.youtube.com/watch?v="
|
|
)
|
|
turl = turl.replace("watch?v=", "embed/")
|
|
turl = turl + "?autoplay=1&enablejsapi=1"
|
|
ret.append(turl)
|
|
|
|
return ret, token
|
|
|
|
|
|
def doPages(numPages):
|
|
token = ""
|
|
vids = []
|
|
for i in range(numPages):
|
|
if i == 0:
|
|
v, t = getRedditData(URL)
|
|
vids = vids + v
|
|
token = t
|
|
time.sleep(2)
|
|
v, t = getRedditData(URL + f"&after={token}")
|
|
vids = vids + v
|
|
print(f"got page {i}...")
|
|
return vids
|
|
|
|
|
|
@scheduler.task("interval", id="do_job_1", minutes=60, misfire_grace_time=900)
|
|
def job1():
|
|
global v
|
|
doFetch = False
|
|
pexist = os.path.exists("temp.json")
|
|
|
|
if pexist:
|
|
print("temp.json exists")
|
|
sec = os.path.getmtime("temp.json")
|
|
now = time.time()
|
|
if now - sec > 600:
|
|
doFetch = True
|
|
else:
|
|
doFetch = True
|
|
|
|
if doFetch:
|
|
print("stale or no file, fetching...")
|
|
v = doPages(2)
|
|
with open("temp.json", "w") as f:
|
|
json.dump(v, f)
|
|
else:
|
|
print("fresh file, not fetching...")
|
|
with open("temp.json", "r") as f:
|
|
v = json.load(f)
|
|
|
|
|
|
app = Flask(__name__)
|
|
scheduler.init_app(app)
|
|
scheduler.start()
|
|
job1()
|
|
|
|
|
|
@app.route("/")
|
|
def hello_world():
|
|
global v
|
|
random.shuffle(v)
|
|
return render_template("index.html", vids=json.dumps(v))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
app.run(host="0.0.0.0", port=8080)
|