qdb.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. from event import Event
  2. import re
  3. import difflib
  4. try:
  5. import imgurpython
  6. except ImportError:
  7. print "Warning: QDB module requires imgurpython."
  8. imgurpython = object
  9. try:
  10. import requests
  11. except ImportError:
  12. print "Warning: QDB module requires requests."
  13. requests = object
  14. class QDB:
  15. def __init__(self, events=None, printer_handle=None, bot=None, say=None):
  16. self.events = events
  17. self.printer = printer_handle
  18. self.interests = ['__.qdb__', '1__all_lines__'] # should be first event in the listing.. so lines being added is a priority
  19. self.bot = bot
  20. self.say = say
  21. self.imgur_client_id = "6f4e468a474bb6e"
  22. self.imgur_client_secret = "22f791df5569e7964a1ca78637125c94cba6f312"
  23. self.bot.mem_store['qdb'] = {}
  24. #define a key for _recent since that will not be a potential channel name
  25. self.bot.mem_store['qdb']['_recent'] = []
  26. for event in events:
  27. if event._type in self.interests:
  28. event.subscribe(self)
  29. self.help = ".qdb <search string of first line> | <search string of last line>"
  30. self.MAX_BUFFER_SIZE = 200
  31. self.MAX_HISTORY_SIZE = 10
  32. def _imgurify(self, url):
  33. client = imgurpython.ImgurClient(self.imgur_client_id, self.imgur_client_secret)
  34. replacement_values = list()
  35. if type(url) is list:
  36. for u in url:
  37. resp = client.upload_from_url(u)
  38. replacement_values.append(resp)
  39. else:
  40. try:
  41. resp = client.upload_from_url(url)
  42. replacement_values.append(resp)
  43. except imgurpython.helpers.error.ImgurClientError, e:
  44. self.bot.debug_print("ImgurClientError: ")
  45. self.bot.debug_print(str(e))
  46. except UnboundLocalError, e:
  47. self.bot.debug_print("UnboundLocalError: ")
  48. self.bot.debug_print(str(e))
  49. except requests.ConnectionError, e:
  50. self.bot.debug_print("ConnectionError: ")
  51. self.bot.debug_print(str(e))
  52. return replacement_values
  53. def _detect_url(self, quote):
  54. """
  55. right now this is strictly for tsdbot's printout functionality
  56. follows this format:
  57. http://irc.teamschoolyd.org/printouts/8xnK5DmfMz.jpg
  58. """
  59. try:
  60. url = re.search("(?P<url>http:\/\/irc\.teamschoolyd\.org\/printouts\/.+\.(jpg|png))", quote).group("url")
  61. except AttributeError: # we didn't find anything
  62. return quote
  63. repl = self._imgurify(url)
  64. new_quote = re.sub('(?P<url>http:\/\/irc\.teamschoolyd\.org\/printouts\/.+\.(jpg|png))',repl[0]['link'], quote)
  65. return new_quote
  66. def add_buffer(self, event=None, debug=False):
  67. """Takes a channel name and line passed to it and stores them in the bot's mem_store dict
  68. for future access. The dict will have channel as key. The value to that key will be a list
  69. of formatted lines of activity.
  70. If the buffer size is not yet exceeded, lines are just added. If the buffer
  71. is maxed out, the oldest line is removed and newest one inserted at the beginning.
  72. """
  73. if debug:
  74. print "Line: " + event.line
  75. print "Verb: " + event.verb
  76. print "Channel: " + event.channel
  77. print ""
  78. if not event:
  79. return
  80. #there are certain things we want to record in history, like nick changes and quits
  81. #these often add to the humor of a quote. however, these are not specific to a channel
  82. #in IRC and our bot does not maintain a userlist per channel. Therefore, when nick
  83. #changes and quits occur, we will add them to every buffer. This is not technically
  84. #correct behavior and could very well lead to quits/nick changes that are not visible
  85. #showing up in a quote, but it's the best we can do at the moment
  86. if not event.channel:
  87. #discard events with unwanted verbs
  88. if event.verb not in ["QUIT", "NICK"]:
  89. return
  90. try:
  91. for chan in self.bot.mem_store['qdb'].keys():
  92. if chan != '_recent':
  93. if len(self.bot.mem_store['qdb'][chan]) >= self.MAX_BUFFER_SIZE:
  94. self.bot.mem_store['qdb'][chan].pop()
  95. line = self.format_line(event)
  96. if line:
  97. self.bot.mem_store['qdb'][chan].insert(0, line)
  98. except (KeyError, IndexError):
  99. print "QDB add_buffer() error when no event channel"
  100. #now we continue with normal, per channel line addition
  101. #create a dictionary associating the channel with an empty list if it doesn't exist yet
  102. else:
  103. if event.channel not in self.bot.mem_store['qdb']:
  104. self.bot.mem_store['qdb'][event.channel] = []
  105. try:
  106. #check for the length of the buffer. if it's too long, pop the last item
  107. if len(self.bot.mem_store['qdb'][event.channel]) >= self.MAX_BUFFER_SIZE:
  108. self.bot.mem_store['qdb'][event.channel].pop()
  109. #get a line by passing event to format_line
  110. #insert the line into the first position in the list
  111. line = self.format_line(event)
  112. if line:
  113. self.bot.mem_store['qdb'][event.channel].insert(0, line)
  114. except IndexError:
  115. print "QDB add_buffer() error. Couldn't access the list index."
  116. def format_line(self, event):
  117. """Takes an event and formats a string appropriate for quotation from it"""
  118. # first strip out printout urls and replace them with imgur mirrors
  119. # commenting out for now to avoid uploading to imgur so often
  120. #event.msg = self._detect_url(event.msg)
  121. #format all strings based on the verb
  122. if event.verb == "":
  123. return ''
  124. elif event.verb == "PRIVMSG":
  125. #special formatting for ACTION strings
  126. if event.msg.startswith('\001ACTION'):
  127. #strip out the word ACTION from the msg
  128. return ' * %s %s\n' % (event.user, event.msg[7:])
  129. else:
  130. return '<%s> %s\n' % (event.user, event.msg)
  131. elif event.verb == "JOIN":
  132. return ' --> %s has joined channel %s\n' % (event.user, event.channel)
  133. elif event.verb == "PART":
  134. return ' <-- %s has left channel %s\n' % (event.user, event.channel)
  135. elif event.verb == "NICK":
  136. return ' -- %s has changed their nick to %s\n' % (event.user, event.msg)
  137. elif event.verb == "TOPIC":
  138. return ' -- %s has changed the topic for %s to "%s"\n' % (event.user, event.channel, event.msg)
  139. elif event.verb == "QUIT":
  140. return ' <-- %s has quit (%s)\n' % (event.user, event.msg)
  141. elif event.verb == "KICK":
  142. #this little bit of code finds the kick target by getting the last
  143. #thing before the event message begins
  144. target = event.line.split(":", 2)[1].split()[-1]
  145. return ' <--- %s has kicked %s from %s (%s)\n' % (event.user, target, event.channel, event.msg)
  146. elif event.verb == "NOTICE":
  147. return ' --NOTICE from %s: %s\n' % (event.user, event.msg)
  148. else:
  149. #no matching verbs found. just ignore the line
  150. return ''
  151. def get_qdb_submission(self, channel=None, start_msg='', end_msg=''):
  152. """Given two strings, start_msg and end_msg, this function will assemble a submission for the QDB.
  153. start_msg is a substring to search for and identify a starting line. end_msg similarly is used
  154. to search for the last desired line in the submission. This function returns a string ready
  155. for submission to the QDB if it finds the desired selection. If not, it returns None.
  156. """
  157. if not channel:
  158. return None
  159. #must have at least one msg to search for and channel to look it up in
  160. if len(start_msg) == 0 or not channel:
  161. return None
  162. #first, check to see if we are doing a single string submission.
  163. if end_msg == '':
  164. for line in self.bot.mem_store['qdb'][channel]:
  165. if start_msg.lower() in line.lower():
  166. return self._detect_url(line) #removing temporary printout urls and replacing with imgur
  167. #making sure we get out of the function if no matching strings were found
  168. #don't want to search for a nonexistent second string later
  169. return None
  170. #search for a matching start and end string and get the buffer index for the start and end message
  171. start_index = -1
  172. end_index = -1
  173. #finds oldest matching string for beginning line
  174. for index, line in enumerate(self.bot.mem_store['qdb'][channel]):
  175. if start_msg.encode('utf-8','ignore').lower() in line.encode('utf-8','ignore').lower():
  176. start_index = index
  177. #finds newest matching string for ending line
  178. for index, line in enumerate(self.bot.mem_store['qdb'][channel]):
  179. if end_msg.lower() in line.lower():
  180. end_index = index
  181. break
  182. #check to see if index values are positive. if not, string was not found and we're done
  183. if start_index == -1 or end_index == -1 or start_index < end_index:
  184. return None
  185. #now we generate the string to be returned for submission
  186. submission = ''
  187. try:
  188. for i in reversed(range(end_index, start_index + 1)):
  189. #print 'Index number is ' + str(i) + ' and current submission is ' + submission
  190. submission += self._detect_url(self.bot.mem_store['qdb'][channel][i]) #detect temporary printout urls and replace with imgur
  191. except IndexError:
  192. print "QDB get_qdb_submission() error when accessing list index"
  193. return submission
  194. def submit(self, qdb_submission, debug=False):
  195. """Given a string, qdb_submission, this function will upload the string to hlmtre's qdb
  196. server. Returns a string with status of submission. If it worked, includes a link to new quote.
  197. """
  198. if debug:
  199. print "Submission is:"
  200. print qdb_submission
  201. print "Current buffer is:"
  202. print self.bot.mem_store['qdb']
  203. print ""
  204. return ''
  205. #accessing hlmtre's qdb api
  206. url = 'http://qdb.zero9f9.com/api.php'
  207. payload = {'q':'new', 'quote': qdb_submission.rstrip('\n')}
  208. try:
  209. qdb = requests.post(url, payload)
  210. except ConnectionError, e:
  211. self.bot.debug_print("ConnectionError: ")
  212. self.bot.debug_print(str(e))
  213. #check for any HTTP errors and return False if there were any
  214. try:
  215. qdb.raise_for_status()
  216. except requests.exceptions.HTTPError, e:
  217. self.bot.debug_print('HTTPError: ')
  218. self.bot.debug_print(str(e))
  219. return "HTTPError encountered when submitting to QDB"
  220. try:
  221. q_url = qdb.json()
  222. self.add_recently_submitted(q_url['id'], qdb_submission)
  223. return "QDB submission successful! http://qdb.zero9f9.com/quote.php?id=" + str(q_url['id'])
  224. except (KeyError, UnicodeDecodeError):
  225. return "Error getting status of quote submission."
  226. return "That was probably successful since no errors came up, but no status available."
  227. def delete(self, user, post_id='', passcode=''):
  228. """A special function that allows certain users to delete posts"""
  229. #accessing hlmtre's qdb api
  230. url = 'http://qdb.zero9f9.com/api.php'
  231. payload = {'q':'delete', 'user':user, 'id':post_id, 'code':passcode}
  232. deletion = requests.get(url, params=payload)
  233. #check for any HTTP errors and return False if there were any
  234. try:
  235. deletion.raise_for_status()
  236. except requests.exceptions.HTTPError, e:
  237. self.bot.debug_print('HTTPError: ')
  238. self.bot.debug_print(str(e))
  239. return "HTTPError encountered when accessing QDB"
  240. try:
  241. del_status = deletion.json()
  242. if del_status['success'] == "true":
  243. return "QDB deletion succeeded."
  244. return "QDB deletion failed."
  245. except (KeyError, UnicodeDecodeError):
  246. return "Error getting status of quote deletion."
  247. def recently_submitted(self, submission):
  248. """Checks to see if the given submission is string is at least 75% similar to the strings
  249. in the list of recently submitted quotes.
  250. Returns the id of the quote if it was recently submitted. If not, returns -1.
  251. """
  252. #set up a difflib SequenceMatcher with the first string to test
  253. comparer = difflib.SequenceMatcher()
  254. comparer.set_seq1(submission)
  255. #if we find that it has 75% similarity or greater to a recent submission, return True
  256. try:
  257. for recent_quote in self.bot.mem_store['qdb']['_recent']:
  258. comparer.set_seq2(recent_quote.values()[0])
  259. if comparer.ratio() >= .75:
  260. return recent_quote.keys()[0]
  261. except TypeError:
  262. return -1
  263. except KeyError:
  264. return -1
  265. except IndexError:
  266. return -1
  267. return -1
  268. def add_recently_submitted(self, q_id, submission):
  269. """Takes a string, submission, and adds it to the list of recent submissions.
  270. Also we do length checking, only keep record of the previous MAX_HISTORY_SIZE quotes.
  271. """
  272. #first, see if we have reached the maximum history size. if so, remove last item
  273. if len(self.bot.mem_store['qdb']['_recent']) >= self.MAX_HISTORY_SIZE:
  274. self.bot.mem_store['qdb']['_recent'].pop()
  275. #inserting a dict with the qdb id of the submission and the submission content
  276. self.bot.mem_store['qdb']['_recent'].insert(0, {q_id:submission})
  277. def handle(self, event):
  278. #first check to see if there is a special deletion going on
  279. if event.msg.startswith(".qdbdelete") and event.is_pm:
  280. deletion = event.msg.split(' ', 2)
  281. try:
  282. #requires the format ".qdbdelete <post_id> <password>"
  283. self.say(event.user, self.delete(event.user, deletion[1], deletion[2]))
  284. except IndexError:
  285. self.say(event.user, "Not enough parameters provided for deletion.")
  286. return
  287. #we see if we're going to generate a qdb submission, or just add the line to the buffer
  288. if event.msg.startswith(".qdb "):
  289. #split the msg with '.qdb ' stripped off beginning and divide into 1 or 2 search strings
  290. string_token = event.msg[5:].split('|', 1)
  291. start_msg = string_token[0].rstrip()
  292. #see if we only have a one line submission
  293. if len(string_token) == 1:
  294. #s is the string to submit
  295. s = self.get_qdb_submission(event.channel, start_msg)
  296. recent = self.recently_submitted(s)
  297. if recent > 0:
  298. q_url = "http://qdb.zero9f9.com/quote.php?id=" + str(recent)
  299. self.printer("PRIVMSG " + event.channel + " :QDB Error: A quote of >75% similarity has already been posted here: " + q_url + "\n")
  300. return
  301. if not s:
  302. self.printer("PRIVMSG " + event.channel + ' :QDB Error: Could not find requested string.\n')
  303. return
  304. #Print the link to the newly submitted quote
  305. self.printer("PRIVMSG " + event.channel + ' :' + self.submit(s) + '\n')
  306. return
  307. #We should only get here if there are two items in string_token
  308. end_msg = string_token[1].lstrip()
  309. s = self.get_qdb_submission(event.channel, start_msg, end_msg)
  310. recent = self.recently_submitted(s)
  311. if recent > 0:
  312. q_url = "http://qdb.zero9f9.com/quote.php?id=" + str(recent)
  313. self.printer("PRIVMSG " + event.channel + " :QDB Error: A quote of >75% similarity has already been posted here: " + q_url + "\n")
  314. return
  315. #if there's nothing found for the submission, then we alert the channel and gtfo
  316. if not s:
  317. self.printer("PRIVMSG " + event.channel + ' :QDB Error: Could not find requested quotes or parameters were not specific enough.\n')
  318. return
  319. #print the link to the new submission
  320. self.printer("PRIVMSG " + event.channel + ' :' + self.submit(s) + '\n')
  321. return
  322. self.add_buffer(event)