summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authormdway <romangraef@gmail.com>2017-10-01 18:35:19 +0200
committermdway <romangraef@gmail.com>2017-10-01 18:35:19 +0200
commit8ca8afbc80704b3fe922c2c4409b7b9a9325d304 (patch)
tree1a02bb417aba1f5a508fedd260c402ebd07ff300 /src
parent72097be5c9cdab29497d98812d932937440c3414 (diff)
downloadPostfix-Bot-8ca8afbc80704b3fe922c2c4409b7b9a9325d304.tar.gz
Postfix-Bot-8ca8afbc80704b3fe922c2c4409b7b9a9325d304.tar.bz2
Postfix-Bot-8ca8afbc80704b3fe922c2c4409b7b9a9325d304.zip
Added Youtube info support
Diffstat (limited to 'src')
-rw-r--r--src/main.py14
-rw-r--r--src/modules/echo.py6
-rw-r--r--src/modules/youtube.py74
3 files changed, 86 insertions, 8 deletions
diff --git a/src/main.py b/src/main.py
index d85b81d..4fd533d 100644
--- a/src/main.py
+++ b/src/main.py
@@ -1,14 +1,13 @@
+import argparse
import asyncio
import importlib
-import re
+import json
+import os
+import sys
import traceback
from string import whitespace
import discord
-import sys
-import json
-import argparse
-import os
client = discord.Client()
@@ -50,6 +49,7 @@ commands += [help_cmd]
class Config(object):
def __init__(self, **kwargs):
+ self.google = kwargs['google']
self.token = kwargs['token']
self.postfix = kwargs.get('postfix', '...')
@@ -136,7 +136,7 @@ def load_modules(dir='modules', module='modules') -> tuple:
total += 1
print('Loading module: %s' % (module + '.' + file), flush=True)
try:
- importlib.import_module('%s.%s' % (module, file)).setup(commands, client)
+ importlib.import_module('%s.%s' % (module, file)).setup(commands, client, config)
except KeyboardInterrupt:
raise
except Exception as e:
@@ -154,6 +154,7 @@ if __name__ == '__main__':
args.add_argument('config_file', type=open, help='Config file in json format.')
ns = args.parse_args(sys.argv[1:])
config = json.load(ns.config_file)
+ config = Config(**config)
print('Config: %s' % str(config))
print()
print('Loading modules')
@@ -168,5 +169,4 @@ if __name__ == '__main__':
line = input("Start anyway? (Y/N)")
if line.lower() == 'n':
sys.exit(1)
- config = Config(**config)
client.run(config.token, bot=True)
diff --git a/src/modules/echo.py b/src/modules/echo.py
index 9510442..d4a8f4e 100644
--- a/src/modules/echo.py
+++ b/src/modules/echo.py
@@ -4,6 +4,8 @@ import main
client = None
+config = None
+
@main.description("aka cat")
@main.name("echo")
@@ -12,7 +14,9 @@ def echo(args, author, channel, message):
yield from client.send_message(channel, content=author.mention + ': ' + args[0])
-def setup(default_cmds, cclient):
+def setup(default_cmds, cclient, cconfig):
global client
+ global config
+ config = cconfig
default_cmds += [echo]
client = cclient
diff --git a/src/modules/youtube.py b/src/modules/youtube.py
new file mode 100644
index 0000000..f031760
--- /dev/null
+++ b/src/modules/youtube.py
@@ -0,0 +1,74 @@
+import asyncio
+import json
+import re
+
+import discord
+import requests
+
+import main
+
+client = None
+config = None
+
+YOUTUBE_VIDEOS_LIST = "https://www.googleapis.com/youtube/v3/videos?part=snippet%2CcontentDetails%2Cstatistics&id=%s" \
+ "&key="
+
+YOUTUBE_CHANNEL = "https://www.googleapis.com/youtube/v3/channels?part=snippet&id=%s&fields=items%2Fsnippet" \
+ "%2Fthumbnails&key="
+
+
+def timeformat(seconds):
+ mins = seconds / 60
+ seconds %= 60
+ hours = mins / 60
+ mins %= 60
+ return "%02d:%02d:%02d" % (hours, mins, seconds)
+
+
+@main.description("Gives you information about a youtube video")
+@main.name("youtube")
+@asyncio.coroutine
+def get_youtube_info(args, author, channel, message):
+ print("IN HERE")
+ regex = re.compile(r"https?://(www\.)?(youtube\.com/watch/?\?v=|youtu\.be/)(?P<id>[a-z\-_]+)")
+ id = regex.search(args[0]).group("id")
+ json_data = json.loads(requests.get(YOUTUBE_VIDEOS_LIST % id))
+ entry = json_data['items'][0]
+ content_details = json_data['contentDetails']
+ snippet = entry['snippet']
+ title = snippet['title']
+ published_at = snippet['publishedAt']
+ uploaded_by = snippet['channelTitle']
+ uploader_id = snippet['channelId']
+ uploader_json = json.loads(requests.get(YOUTUBE_CHANNEL % uploader_id))
+ uploader_icon = uploader_json['items'][0]['snippet']['thumbnails']['default']
+ keywords = ', '.join(snippet['tags'])
+ description = snippet['description']
+ thumbnail = snippet['thumbnail']['default']['url']
+ likes = content_details['likeCount']
+ dislikes = content_details['dislikeCount']
+ views = content_details['viewCount']
+ duration = content_details['duration'].replace("PT", "").replace("S", "").replace("M", ":")
+ url = args[0]
+ embed = discord.Embed(title=title,
+ description=description,
+ url=url)
+ embed.set_image(url=thumbnail)
+ embed.set_author(name=uploaded_by, icon_url=uploader_icon)
+ embed.set_footer(text='Youtube API?', icon_url="http://youtube.com/yts/img/favicon-vfl8qSV2F.ico")
+ embed.add_field(name="Published at", value=published_at)
+ embed.add_field(name="Keywords", value=keywords)
+ embed.add_field(name="Duration", value=duration)
+ embed.add_field(name="Ratings", value="+%d;-%d;ratio %.2f" % (likes, dislikes, likes / dislikes))
+ embed.add_field(name="Views", value=str(views))
+ yield from client.send_message(channel, embed=embed)
+
+
+def setup(default_cmds, cclient, cconfig):
+ global client
+ global config
+ global YOUTUBE_VIDEOS_LIST
+ config = cconfig
+ default_cmds += [get_youtube_info]
+ client = cclient
+ YOUTUBE_VIDEOS_LIST += config.google['developer-token']