|
|
|
@ -36,7 +36,8 @@ config.read('settings.cfg')
|
|
|
|
|
storage_path = config.get('General', 'storage_path')
|
|
|
|
|
|
|
|
|
|
token = config.get('Discord', 'token')
|
|
|
|
|
channel_id = config.getint('Discord', 'channel_id')
|
|
|
|
|
twitter_channel_id = config.getint('Discord', 'twitter_channel_id')
|
|
|
|
|
youtube_channel_id = config.getint('Discord', 'youtube_channel_id')
|
|
|
|
|
|
|
|
|
|
consumer_key = config.get('Twitter', 'consumer_key')
|
|
|
|
|
consumer_secret = config.get('Twitter', 'consumer_secret')
|
|
|
|
@ -53,7 +54,21 @@ class Bot(discord.Client):
|
|
|
|
|
self.youtube = YouTube(yt_api_key, yt_channel_id)
|
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
|
|
def get_api(self, user_id):
|
|
|
|
|
async def process_event(self, service, data):
|
|
|
|
|
action = getattr(self, 'process_{0}_event'.format(service))
|
|
|
|
|
await action(data)
|
|
|
|
|
|
|
|
|
|
async def on_message(self, message):
|
|
|
|
|
for pattern in self.commands:
|
|
|
|
|
m = pattern.match(message.content)
|
|
|
|
|
if not m:
|
|
|
|
|
continue
|
|
|
|
|
d = m.groupdict()
|
|
|
|
|
command = d.pop('command')
|
|
|
|
|
action = getattr(self, 'perform_{0}'.format(command))
|
|
|
|
|
await action(message, **d)
|
|
|
|
|
|
|
|
|
|
def get_twitter_api(self, user_id):
|
|
|
|
|
handler = tweepy.OAuthHandler(consumer_key, consumer_secret)
|
|
|
|
|
with open(pathlib.Path(storage_path, 'tokens.json'), 'r') as f:
|
|
|
|
|
tokens = json.load(f)
|
|
|
|
@ -63,7 +78,7 @@ class Bot(discord.Client):
|
|
|
|
|
handler.set_access_token(*access_token)
|
|
|
|
|
return tweepy.API(handler)
|
|
|
|
|
|
|
|
|
|
def make_embed(self, tweet):
|
|
|
|
|
def make_twitter_embed(self, tweet):
|
|
|
|
|
tweet_url = '{0}{1}'.format(TWITTER_STATUS_URL, tweet.get('id_str'))
|
|
|
|
|
author_url = '{0}{1}'.format(TWITTER_USER_URL, tweet.get('user', {}).get('id_str'))
|
|
|
|
|
author_handle = '@{0}'.format(tweet.get('user', {}).get('screen_name'))
|
|
|
|
@ -84,7 +99,7 @@ class Bot(discord.Client):
|
|
|
|
|
)
|
|
|
|
|
return embed
|
|
|
|
|
|
|
|
|
|
async def process_event(self, data):
|
|
|
|
|
async def process_twitter_event(self, data):
|
|
|
|
|
tweets = data.get('tweet_create_events', [])
|
|
|
|
|
if not tweets:
|
|
|
|
|
return
|
|
|
|
@ -94,24 +109,45 @@ class Bot(discord.Client):
|
|
|
|
|
user_id = data.get('for_user_id')
|
|
|
|
|
if not user_id:
|
|
|
|
|
return
|
|
|
|
|
api = self.get_api(user_id)
|
|
|
|
|
api = self.get_twitter_api(user_id)
|
|
|
|
|
if not api:
|
|
|
|
|
return
|
|
|
|
|
await self.wait_until_ready()
|
|
|
|
|
channel = self.get_channel(channel_id)
|
|
|
|
|
channel = self.get_channel(twitter_channel_id)
|
|
|
|
|
if channel:
|
|
|
|
|
for tweet in tweets:
|
|
|
|
|
await channel.send(embed=self.make_embed(tweet))
|
|
|
|
|
await channel.send(embed=self.make_twitter_embed(tweet))
|
|
|
|
|
|
|
|
|
|
async def on_message(self, message):
|
|
|
|
|
for pattern in self.commands:
|
|
|
|
|
m = pattern.match(message.content)
|
|
|
|
|
if not m:
|
|
|
|
|
continue
|
|
|
|
|
d = m.groupdict()
|
|
|
|
|
command = d.pop('command')
|
|
|
|
|
action = getattr(self, 'perform_{0}'.format(command))
|
|
|
|
|
await action(message, **d)
|
|
|
|
|
async def process_youtube_event(self, data):
|
|
|
|
|
entry = data.get('feed', {}).get('entry', {})
|
|
|
|
|
if entry.get('yt:channelId') != yt_channel_id:
|
|
|
|
|
return
|
|
|
|
|
video_id = entry.get('yt:videoId')
|
|
|
|
|
if not video_id:
|
|
|
|
|
return
|
|
|
|
|
try:
|
|
|
|
|
video = self.youtube.get_video(video_id)
|
|
|
|
|
except YouTubeError as e:
|
|
|
|
|
return
|
|
|
|
|
channel = self.get_channel(youtube_channel_id)
|
|
|
|
|
if channel:
|
|
|
|
|
# TODO: setup timer for livestreams
|
|
|
|
|
await channel.send('{title}\n{link}'.format(**video))
|
|
|
|
|
|
|
|
|
|
def make_youtube_embed(self, playlist):
|
|
|
|
|
embed = discord.Embed(
|
|
|
|
|
title=playlist.get('title'),
|
|
|
|
|
description=playlist.get('description'),
|
|
|
|
|
url=playlist.get('link'),
|
|
|
|
|
color=YOUTUBE_COLOR
|
|
|
|
|
)
|
|
|
|
|
embed.set_thumbnail(url=playlist.get('thumbnail_url'))
|
|
|
|
|
embed.set_author(
|
|
|
|
|
name=self.youtube.channel.get('title'),
|
|
|
|
|
url=self.youtube.channel.get('link'),
|
|
|
|
|
icon_url=self.youtube.channel.get('thumbnail_url')
|
|
|
|
|
)
|
|
|
|
|
return embed
|
|
|
|
|
|
|
|
|
|
async def perform_reaction(self, message, query, **kwargs):
|
|
|
|
|
try:
|
|
|
|
@ -123,12 +159,7 @@ class Bot(discord.Client):
|
|
|
|
|
await message.channel.send('Sorry {0}, nothing found'.format(message.author.mention))
|
|
|
|
|
return
|
|
|
|
|
if result.get('kind') == 'playlist':
|
|
|
|
|
embed = discord.Embed(title=result.get('title'), url=result.get('link'),
|
|
|
|
|
description=result.get('description'), color=YOUTUBE_COLOR)
|
|
|
|
|
embed.set_thumbnail(url=result.get('thumbnail_url'))
|
|
|
|
|
embed.set_author(name=self.youtube.channel.get('title'), url=self.youtube.channel.get('link'),
|
|
|
|
|
icon_url=self.youtube.channel.get('thumbnail_url'))
|
|
|
|
|
await message.channel.send(embed=embed)
|
|
|
|
|
await message.channel.send(embed=self.make_youtube_embed(result))
|
|
|
|
|
else:
|
|
|
|
|
await message.channel.send('{title}\n{link}'.format(**result))
|
|
|
|
|
|
|
|
|
@ -144,7 +175,7 @@ def main():
|
|
|
|
|
await asyncio.sleep(TIMEOUT)
|
|
|
|
|
else:
|
|
|
|
|
try:
|
|
|
|
|
await bot.process_event(data)
|
|
|
|
|
await bot.process_event(*data)
|
|
|
|
|
except Exception:
|
|
|
|
|
queue.put(data)
|
|
|
|
|
queue.task_done()
|
|
|
|
|