From d834187bb550e3174840de5da9e42ff246560839 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Forr=C3=B3?= Date: Tue, 8 Sep 2020 20:50:44 +0200 Subject: [PATCH] Implement YT live countdown --- bot.py | 21 ++++++++++++++++++--- endpoint.py | 3 ++- timer.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ twitivirun.py | 3 ++- 4 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 timer.py diff --git a/bot.py b/bot.py index c8b7986..a56d251 100644 --- a/bot.py +++ b/bot.py @@ -2,8 +2,10 @@ import asyncio import configparser +import datetime import json import logging +import os import pathlib import re @@ -11,9 +13,12 @@ import discord import persistqueue import tweepy +from timer import Timer from youtube import YouTube, YouTubeError +COUNTDOWN = [30.5, 5.5] + TIMEOUT = 1 TWITTER_USER_URL = 'https://twitter.com/i/user/' @@ -30,7 +35,7 @@ logger.setLevel(logging.INFO) config = configparser.ConfigParser() -config.read('settings.cfg') +config.read(os.getenv('CHEDDAR_KNIGHT_CONFIG', 'settings.cfg')) storage_path = config.get('General', 'storage_path') @@ -119,6 +124,7 @@ class Bot(discord.Client): await channel.send(embed=self.make_twitter_embed(tweet)) async def process_youtube_event(self, data): + # TODO: log data for now entry = data.get('feed', {}).get('entry', {}) if entry.get('yt:channelId') != yt_channel_id: return @@ -131,8 +137,17 @@ class Bot(discord.Client): return channel = self.get_channel(youtube_channel_id) if channel: - # TODO: setup timer for livestreams - await channel.send('{title}\n{link}'.format(**video)) + note = '' + if video.get('live_broadcast') == 'upcoming': + scheduled_start = video.get('scheduled_start') + if scheduled_start: + remaining = (scheduled_start - datetime.datetime.utcnow()).total_seconds() + for minutes in COUNTDOWN: + if remaining > minutes * 60: + Timer.schedule(scheduled_start - datetime.timedelta(minutes=minutes), data) + note = 'Live in {0} minutes!\n'.format(int(remaining / 60)) + mention = channel.guild.default_role.mention + await channel.send('{mention} {note}**{title}**\n{link}'.format(mention=mention, note=note, **video)) def make_youtube_embed(self, playlist): embed = discord.Embed( diff --git a/endpoint.py b/endpoint.py index 2143da0..d3dd1e6 100644 --- a/endpoint.py +++ b/endpoint.py @@ -4,6 +4,7 @@ import hashlib import hmac import json import logging +import os import pathlib import flask @@ -15,7 +16,7 @@ from twitivity import Twitivity, TwitivityError config = configparser.ConfigParser() -config.read('settings.cfg') +config.read(os.getenv('CHEDDAR_KNIGHT_CONFIG', 'settings.cfg')) app = flask.Flask(__name__) diff --git a/timer.py b/timer.py new file mode 100644 index 0000000..2967fe7 --- /dev/null +++ b/timer.py @@ -0,0 +1,45 @@ +import configparser +import datetime +import json +import os +import pathlib +import subprocess +import sys + +import persistqueue + + +config = configparser.ConfigParser() +config.read(os.getenv('CHEDDAR_KNIGHT_CONFIG', 'settings.cfg')) + + +storage_path = config.get('General', 'storage_path') + + +class Timer: + @classmethod + def schedule(cls, time, data): + subprocess.run(( + '/usr/bin/systemd-run', + '--user', + '--working-directory={0}'.format(pathlib.Path(__file__).parent), + '--timer-property=AccuracySec=1s', + '--on-calendar={0}'.format(time.strftime('%Y-%m-%d %H:%M:%S UTC')), + '/usr/bin/python3', + __file__, + json.dumps(data) + )) + + @classmethod + def execute(cls, data): + queue = persistqueue.SQLiteQueue(storage_path) + queue.put(data) + queue.task_done() + + +def main(): + Timer.execute(json.loads(sys.argv[1])) + + +if __name__ == '__main__': + main() diff --git a/twitivirun.py b/twitivirun.py index 53cf01d..5def93f 100644 --- a/twitivirun.py +++ b/twitivirun.py @@ -1,5 +1,6 @@ import argparse import configparser +import os import tweepy @@ -7,7 +8,7 @@ from twitivity import Twitivity, TwitivityError config = configparser.ConfigParser() -config.read('settings.cfg') +config.read(os.getenv('CHEDDAR_KNIGHT_CONFIG', 'settings.cfg')) consumer_key = config.get('Twitter', 'consumer_key')