From 38f03b98bbef3fe8d0ec2da6e9380f6555ad4f9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Forr=C3=B3?= Date: Thu, 14 Feb 2019 11:44:51 +0100 Subject: [PATCH] Implement !cheese command --- Dockerfile | 2 +- clients/discord.py | 13 +++++++++++++ commands.py | 10 ++++++++++ requirements.txt | 1 + services/cheesecom.py | 26 ++++++++++++++++++++++++++ 5 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 services/cheesecom.py diff --git a/Dockerfile b/Dockerfile index 1362002..a9966f8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,7 +4,7 @@ WORKDIR /bot COPY . . RUN touch settings.cfg -RUN apk add --no-cache gcc libc-dev && \ +RUN apk add --no-cache gcc libc-dev libxml2-dev libxslt-dev && \ rm -rf /var/cache/apk/* RUN pip install --no-cache-dir --requirement requirements.txt diff --git a/clients/discord.py b/clients/discord.py index 82bda32..db08706 100644 --- a/clients/discord.py +++ b/clients/discord.py @@ -62,6 +62,7 @@ class DiscordClient(discord.Client): (re.compile(r'^!(bella(gram|pics)|insta(gram|bella))$'), self._do_bellagram), (re.compile(r'^!yt\s+(?P")?(?P.+)(?(q)")$'), self._do_yt), (re.compile(r'^!clip\s+(?P")?(?P.+)(?(q)")$'), self._do_clip), + (re.compile(r'^!cheese\s+(?P")?(?P.+)(?(q)")$'), self._do_cheese), ] super(DiscordClient, self).__init__() @@ -224,3 +225,15 @@ class DiscordClient(discord.Client): await self.send_message(message.channel, 'Sorry {0}, {1}'.format(message.author.mention, e)) else: await self.send_message(message.channel, '{title}\n{url}'.format(**result)) + + @cooldown(retries=3, timeout=5*60, failure=_cooldown_failure) + async def _do_cheese(self, server, user, message, query, **kwargs): + try: + result = self.commands.query_cheese_com(query) + except CommandError as e: + await self.send_message(message.channel, 'Sorry {0}, {1}'.format(message.author.mention, e)) + else: + embed = discord.Embed(title=result['name'], url=result['url'], + description=result['description'], color=0xffd700) + embed.set_thumbnail(url=result['image']) + await self.send_message(message.channel, embed=embed) diff --git a/commands.py b/commands.py index d10df5d..e07a905 100644 --- a/commands.py +++ b/commands.py @@ -4,6 +4,7 @@ import dateutil.parser import requests from services.youtube import Youtube, YoutubeError +from services.cheesecom import CheeseCom, CheeseComError INSTAGRAM_BASE_URL = 'https://www.instagram.com' @@ -167,6 +168,15 @@ class Commands(object): else: return clip + def query_cheese_com(self, query): + cc = CheeseCom() + try: + result = cc.query(query) + except CheeseComError: + raise CommandError('couldn\'t find exact match on cheese.com') + else: + return result + def _get_instagram_media(self, params): api_url = self.config['Instagram'].get('api_url') r = requests.get('{0}/media'.format(api_url), params=params) diff --git a/requirements.txt b/requirements.txt index 225de6b..debd2c8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,6 +2,7 @@ discord.py fuzzywuzzy[speedup] google-api-python-client irc +pyquery python-dateutil python-twitter requests diff --git a/services/cheesecom.py b/services/cheesecom.py new file mode 100644 index 0000000..d837b03 --- /dev/null +++ b/services/cheesecom.py @@ -0,0 +1,26 @@ +import requests + +from pyquery import PyQuery as pq + + +BASE_URL = 'https://www.cheese.com' + + +class CheeseComError(Exception): + pass + + +class CheeseCom(object): + def query(self, q): + try: + r = requests.get(BASE_URL, params=dict(q=q)) + if not r.history: + raise CheeseComError('no exact match') + d = pq(r.content) + return dict( + url=d('meta[name="twitter:url"]').attr('content'), + name=d('meta[name="twitter:title"]').attr('content'), + description=d('meta[name="twitter:description"]').attr('content'), + image=d('meta[name="twitter:image"]').attr('content')) + except Exception as e: + raise CheeseComError(str(e))