Skip to content

Instantly share code, notes, and snippets.

@dromer
Created May 30, 2020 11:06
Show Gist options
  • Save dromer/78dce353732624cdc229c7e73d5ace51 to your computer and use it in GitHub Desktop.
Save dromer/78dce353732624cdc229c7e73d5ace51 to your computer and use it in GitHub Desktop.
sopel giphy plugin
# -*- coding: utf8 -*-
"""
giphy.py - Willie giphy Module
"""
from sopel.module import commands, example
import requests
import urllib
import re
import random
@commands('gif')
@example('.gif cat')
def giphy(bot, trigger):
""".giphy cat"""
API_KEY = bot.config.apikeys.giphy
user_input = urllib.parse.quote_plus(trigger.group(2))
payload = {'api_key': API_KEY, 'q': user_input, 'offset': 0, 'limit': 1, 'rating': 'PG-13'}
r = requests.get("http://api.giphy.com/v1/gifs/search", params=payload)
giphy_json = r.json()
if giphy_json['pagination']['count'] > 0:
rand_image = random.randint(0, giphy_json['pagination']['count'] - 1 )
image_url = giphy_json['data'][rand_image]['images']['original']['url']
bot.say(image_url)
else:
bot.say('No gif found. Blame bob')
~
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment