forked from scottjab/supybot-imgur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.py
159 lines (139 loc) · 5.46 KB
/
plugin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
###
# Copyright (c) 2012, James Scott
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions, and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions, and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the author of this software nor the name of
# contributors to this software may be used to endorse or promote products
# derived from this software without specific prior written consent.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
###
import supybot.utils as utils
from supybot.commands import *
import supybot.plugins as plugins
import supybot.ircutils as ircutils
import supybot.callbacks as callbacks
import sqlachemy import create_engine
import requests
import googl
class Imgur(callbacks.Plugin):
"""Add the help for "@plugin help Imgur" here
This should describe *how* to use this plugin."""
threaded = True
def __init__(self, irc):
self.shortenDbEngine =
create_engine('mysql+mysqldb:://%s:%s@%s/%s' %
(self.registryValue('shorten_mysql_user'), self.registryValue('shorten_mysql_pass'),
self.registryValue('shorten_mysql_server'),
self.registryValue('shorten_mysql_db')), pool_size = 2, pool_recycle=3600,
echo=False)
self.shorten = googl.Googl(self.shortenDbEngine,
self.registryValue('shorten_api_key'))
def _lookUpHash(self, imgur_hash):
albumService = 'http://imgur.com/gallery/%s.json'
try:
if imgur_hash is None or imgur_hash == '':
return None
response = requests.get(albumService % imgur_hash)
if response.status_code == 200:
return response.json['data']['image']
else:
return None
except:
return None
@classmethod
def _parse_url(cls, full_url):
"""
Takes a URL and returns the Imgur hash
"""
parsed_url = requests.utils.urlparse(full_url)
imgur_hash = parsed_url.path.split("/")[-1].split(".")[0]
return imgur_hash
@classmethod
def _extract_url(cls, text):
prefix_map = ["https://", "http://", "imgur.com"]
for prefix in prefix_map:
if prefix in text:
url = text[text.find(prefix):].split(" ")[0]
return url
return None
def _build_reply_string(self, json):
title = ""
reddit = ""
views = 0
try:
title = ircutils.bold(json["title"])
except:
pass
try:
views = ircutils.bold(json["views"])
except:
views = ircutils.bold('0')
try:
reddit = ircutils.bold(json["reddit"])
except:
reddit = ircutils.bold("n/a")
return ('Title: %s Views: %s Reddit: %s ' % (title, views, reddit))
def _lookUpImgur(self, irc, msg):
"""
Main function of the Imgur plugin. Pass it and
"""
(recipients, text) = msg.args
url = self._extract_url(text)
imgur_hash = self._parse_url(url)
imgur_json = self._lookUpHash(imgur_hash)
if imgur_json:
reply_string = self._build_reply_string(imgur_json)
irc.reply(reply_string, prefixNick=False)
def doPrivmsg(self, irc, msg):
(recipients, text) = msg.args
if "imgur.com" in text:
self._lookUpImgur(irc, msg)
else:
pass
@classmethod
def _uploadImage(cls, key, url):
uploadService = "http://api.imgur.com/2/upload.json"
headers = {
'key': key,
'image': url
}
try:
response = requests.post(uploadService, params=headers)
if response.status_code == 200:
return response.json['upload']['links']['original']
else:
return None
except:
return None
def imgur(self, irc, msg, args):
"""imgur <url> returns a imgur url for the image passed to it."""
if args[0] is None or args[0] == '':
irc.reply(__doc__)
elif self.registryValue('developer_key') is None:
irc.reply("Set developer_key")
elif "http://" in args[0] or "https://" in args[0]:
key = self.registryValue('developer_key')
irc.reply(self._uploadImage(key, args[0]))
else:
irc.reply("Something Bad Happend.")
Class = Imgur
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: