diff options
| author | jason | 2014-04-23 15:21:30 -0600 |
|---|---|---|
| committer | jason | 2014-05-28 01:41:18 -0600 |
| commit | 126f8a496be0b089e95cbbc357400fc90d6b8571 (patch) | |
| tree | 150888d3b52fcbba7eb9940f88f938d78f4e6f96 | |
| parent | 79614a5336e765b115de9a8c7effe970fde9e896 (diff) | |
| download | warmachine-126f8a496be0b089e95cbbc357400fc90d6b8571.tar.gz warmachine-126f8a496be0b089e95cbbc357400fc90d6b8571.zip | |
new giphy search
| -rw-r--r-- | wmd/actions/giphy.py | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/wmd/actions/giphy.py b/wmd/actions/giphy.py new file mode 100644 index 0000000..556e573 --- /dev/null +++ b/wmd/actions/giphy.py | |||
| @@ -0,0 +1,32 @@ | |||
| 1 | __author__ = 'jason@zzq.org' | ||
| 2 | __version__ = 1.0 | ||
| 3 | |||
| 4 | import urllib2 | ||
| 5 | import json as simplejson | ||
| 6 | |||
| 7 | from wmd.actions import Action | ||
| 8 | |||
| 9 | import settings | ||
| 10 | |||
| 11 | |||
| 12 | class GiphySearch(Action): | ||
| 13 | def recv_msg(self, irc, obj_data): | ||
| 14 | args = obj_data.params.split(" ") | ||
| 15 | channel = args[0] | ||
| 16 | if channel == settings.NICKNAME: | ||
| 17 | channel = obj_data.get_username() | ||
| 18 | |||
| 19 | if "PRIVMSG" in obj_data.command and ":!gif" in args[1].lower(): | ||
| 20 | search_string = " ".join(args[2:]) | ||
| 21 | url = "http://api.giphy.com/v1/gifs/search?q=%s&api_key=dc6zaTOxFJmzC&limit=1" % \ | ||
| 22 | (search_string.replace(" ", "%20"), ) | ||
| 23 | req = urllib2.Request(url) | ||
| 24 | opener = urllib2.build_opener() | ||
| 25 | data = opener.open(req).read() | ||
| 26 | |||
| 27 | data = simplejson.loads(data) | ||
| 28 | try: | ||
| 29 | result = data['data'][0]['images']['original']['url'] | ||
| 30 | irc.privmsg(channel, result) | ||
| 31 | except IndexError: | ||
| 32 | pass | ||