Skip to content

Commit e065c3e

Browse files
authored
Merge pull request CloudBotIRC#161 from linuxdaemon/gonzobot+dog-gifs
Add a command for random dog GIFs
2 parents 7dd830b + 923af57 commit e065c3e

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

plugins/animal_gifs.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
All GIFs courtesy of http://bestanimations.com/
3+
"""
4+
import random
5+
from urllib.parse import urljoin
6+
7+
from cloudbot import hook
8+
from cloudbot.util.http import get_soup
9+
10+
BASE_URL = "http://bestanimations.com/Animals/Mammals/Dogs/"
11+
DOG_PAGES = (
12+
"Dogs.html",
13+
"Dogs2.html", # Pugs
14+
"Dogs3.html", # Puppies
15+
)
16+
17+
18+
def get_gifs(url):
19+
soup = get_soup(url)
20+
container = soup.find('div', class_="row")
21+
gifs = [urljoin(url, elem["src"]) for elem in container.find_all('img')]
22+
return gifs
23+
24+
25+
def get_random_gif(url):
26+
return random.choice(get_gifs(url))
27+
28+
29+
@hook.command(autohelp=False)
30+
def doggifs(logger):
31+
"""- Returns a random dog GIF from http://bestanimations.com/"""
32+
page = random.choice(DOG_PAGES)
33+
url = urljoin(BASE_URL, page)
34+
try:
35+
return get_random_gif(url)
36+
except Exception:
37+
logger.exception("An error occurred while trying to get a dog GIF:")
38+
return "Error occurred when retrieving GIF"

0 commit comments

Comments
 (0)