Implement !cheese command

master
Nikola Forró 6 years ago
parent 78c189ceff
commit 38f03b98bb

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

@ -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<q>")?(?P<query>.+)(?(q)")$'), self._do_yt),
(re.compile(r'^!clip\s+(?P<q>")?(?P<filter>.+)(?(q)")$'), self._do_clip),
(re.compile(r'^!cheese\s+(?P<q>")?(?P<query>.+)(?(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)

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

@ -2,6 +2,7 @@ discord.py
fuzzywuzzy[speedup]
google-api-python-client
irc
pyquery
python-dateutil
python-twitter
requests

@ -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))
Loading…
Cancel
Save