File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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"
You can’t perform that action at this time.
0 commit comments