From f4b41f0049ca4924253eb6ee1b49decfa78f37b3 Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Sun, 25 Sep 2022 05:08:44 +0200 Subject: [PATCH] make addition and removal of channels happen instantly (testing in devrama) --- files/helpers/cron.py | 6 +++-- files/routes/static.py | 51 +++++++++++++++++++++++++++++++++--------- 2 files changed, 45 insertions(+), 12 deletions(-) diff --git a/files/helpers/cron.py b/files/helpers/cron.py index 6f248f9fe..bd1c45e94 100644 --- a/files/helpers/cron.py +++ b/files/helpers/cron.py @@ -31,8 +31,10 @@ def cron(every_5m, every_1h, every_1d, every_1mo): lottery.check_if_end_lottery_task() spin_roulette_wheel() offsitementions.offsite_mentions_task() - if SITE_NAME == 'PCM': - cache.set('live_cached', route_static.live_cached()) + if SITE == 'pcmemes.net' or True: + x = route_static.live_cached() + cache.set('live', x[0]) + cache.set('offline', x[1]) if every_1h: awards.award_timers_bots_task() diff --git a/files/routes/static.py b/files/routes/static.py index 177324407..9e695dcdb 100644 --- a/files/routes/static.py +++ b/files/routes/static.py @@ -436,7 +436,7 @@ def donate(v): return render_template(f'donate_{SITE_NAME}.html', v=v) -if SITE == 'pcmemes.net': +if SITE == 'pcmemes.net' or True: from files.classes.streamers import * live_regex = re.compile('playerOverlayVideoDetailsRenderer":\{"title":\{"simpleText":"(.*?)"\},"subtitle":\{"runs":\[\{"text":"(.*?)"\},\{"text":" • "\},\{"text":"(.*?)"\}', flags=re.A) @@ -460,7 +460,7 @@ if SITE == 'pcmemes.net': count = int(y.group(3)) live.append((x, req.url, t.group(1), y.group(2), y.group(1), count)) except: - offline.append((x, req.url.rstrip('/live'), t.group(1), y.group(2))) + print(x) else: y = offline_regex.search(txt) try: offline.append((x, req.url.rstrip('/live'), y.group(2), y.group(1))) @@ -473,20 +473,22 @@ if SITE == 'pcmemes.net': @app.get('/live') @app.get('/logged_out/live') @auth_desired_with_logingate - def live(v): - live_cached = cache.get('live_cached') or [[],[]] + def live_list(v): + live = cache.get('live') or [] + offline = cache.get('offline') or [] - return render_template('live.html', v=v, live=live_cached[0], offline=live_cached[1]) + return render_template('live.html', v=v, live=live, offline=offline) @app.post('/live/add') @admin_level_required(2) def live_add(v): id = request.values.get('id').strip() - live_cached = cache.get('live_cached') or [[],[]] + live = cache.get('live') or [] + offline = cache.get('offline') or [] if not id or len(id) != 24: - return render_template('live.html', v=v, live=live_cached[0], offline=live_cached[1], error="Invalid ID") + return render_template('live.html', v=v, live=live, offline=offline, error="Invalid ID") existing = g.db.get(Streamer, id) if not existing: @@ -496,7 +498,28 @@ if SITE == 'pcmemes.net': if v.id != KIPPY_ID: send_repeatable_notification(KIPPY_ID, f"@{v.username} has added a [new YouTube channel](https://www.youtube.com/channel/{streamer.id})") - return render_template('live.html', v=v, live=live_cached[0], offline=live_cached[1], msg="Channel added successfuly!") + url = f'https://www.youtube.com/channel/{id}/live' + req = requests.get(url, cookies={'CONSENT': 'YES+1'}, proxies=proxies) + txt = req.text + if '"videoDetails":{"videoId"' in txt: + t = live_thumb_regex.search(txt) + y = live_regex.search(txt) + try: + count = int(y.group(3)) + live.append((id, req.url, t.group(1), y.group(2), y.group(1), count)) + cache.set('live', live) + except: + print(id) + else: + y = offline_regex.search(txt) + try: + offline.append((id, req.url.rstrip('/live'), y.group(2), y.group(1))) + cache.set('offline', offline) + except: + print(id) + + + return render_template('live.html', v=v, live=live, offline=offline, msg="Channel added successfuly!") @app.post('/live/remove') @admin_level_required(2) @@ -509,5 +532,13 @@ if SITE == 'pcmemes.net': send_repeatable_notification(KIPPY_ID, f"@{v.username} has removed a [YouTube channel](https://www.youtube.com/channel/{streamer.id})") g.db.delete(streamer) - live_cached = cache.get('live_cached') or [[],[]] - return render_template('live.html', v=v, live=live_cached[0], offline=live_cached[1], msg="Channel removed successfuly!") \ No newline at end of file + live = cache.get('live') or [] + offline = cache.get('offline') or [] + + live = [x for x in live if x[0] != id] + offline = [x for x in offline if x[0] != id] + + cache.set('live', live) + cache.set('offline', offline) + + return render_template('live.html', v=v, live=live, offline=offline, msg="Channel removed successfuly!") \ No newline at end of file