Implement YT live countdown

master
Nikola Forró 4 years ago
parent fe23a237c5
commit d834187bb5

@ -2,8 +2,10 @@
import asyncio import asyncio
import configparser import configparser
import datetime
import json import json
import logging import logging
import os
import pathlib import pathlib
import re import re
@ -11,9 +13,12 @@ import discord
import persistqueue import persistqueue
import tweepy import tweepy
from timer import Timer
from youtube import YouTube, YouTubeError from youtube import YouTube, YouTubeError
COUNTDOWN = [30.5, 5.5]
TIMEOUT = 1 TIMEOUT = 1
TWITTER_USER_URL = 'https://twitter.com/i/user/' TWITTER_USER_URL = 'https://twitter.com/i/user/'
@ -30,7 +35,7 @@ logger.setLevel(logging.INFO)
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read('settings.cfg') config.read(os.getenv('CHEDDAR_KNIGHT_CONFIG', 'settings.cfg'))
storage_path = config.get('General', 'storage_path') storage_path = config.get('General', 'storage_path')
@ -119,6 +124,7 @@ class Bot(discord.Client):
await channel.send(embed=self.make_twitter_embed(tweet)) await channel.send(embed=self.make_twitter_embed(tweet))
async def process_youtube_event(self, data): async def process_youtube_event(self, data):
# TODO: log data for now
entry = data.get('feed', {}).get('entry', {}) entry = data.get('feed', {}).get('entry', {})
if entry.get('yt:channelId') != yt_channel_id: if entry.get('yt:channelId') != yt_channel_id:
return return
@ -131,8 +137,17 @@ class Bot(discord.Client):
return return
channel = self.get_channel(youtube_channel_id) channel = self.get_channel(youtube_channel_id)
if channel: if channel:
# TODO: setup timer for livestreams note = ''
await channel.send('{title}\n{link}'.format(**video)) 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): def make_youtube_embed(self, playlist):
embed = discord.Embed( embed = discord.Embed(

@ -4,6 +4,7 @@ import hashlib
import hmac import hmac
import json import json
import logging import logging
import os
import pathlib import pathlib
import flask import flask
@ -15,7 +16,7 @@ from twitivity import Twitivity, TwitivityError
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read('settings.cfg') config.read(os.getenv('CHEDDAR_KNIGHT_CONFIG', 'settings.cfg'))
app = flask.Flask(__name__) app = flask.Flask(__name__)

@ -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()

@ -1,5 +1,6 @@
import argparse import argparse
import configparser import configparser
import os
import tweepy import tweepy
@ -7,7 +8,7 @@ from twitivity import Twitivity, TwitivityError
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read('settings.cfg') config.read(os.getenv('CHEDDAR_KNIGHT_CONFIG', 'settings.cfg'))
consumer_key = config.get('Twitter', 'consumer_key') consumer_key = config.get('Twitter', 'consumer_key')

Loading…
Cancel
Save