Advertisement
Guest User

Untitled

a guest
May 28th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.54 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3.  
  4. from twisted.words.protocols import irc
  5. from twisted.internet import reactor, protocol
  6. from twisted.python import log
  7. import time, sys, random, MySQLdb, re, datetime
  8.  
  9. class dB():
  10.    
  11.     def __init__(self, mysqlhost, mysqluser, mysqlpass, db):
  12.         self.db = MySQLdb.connect(host=mysqlhost, user=mysqluser, passwd=mysqlpass, db=db)
  13.         self.cursor = self.db.cursor()
  14.        
  15.     def add(self, quote, date, addedby):
  16.         self.cursor.execute("INSERT INTO quotes (quote,date_added,added_by) VALUES ('%s', '%s', '%s')" %(quote, date, addedby))
  17.  
  18.     def randquoteid(self):
  19.         self.cursor.execute("SELECT id FROM quotes")
  20.         rows = self.cursor.rowcount
  21.         if rows:
  22.             id = random.randrange(1, rows + 1)
  23.             return id
  24.         else:
  25.             return False
  26.    
  27.     def randquote(self):
  28.         if self.randquoteid():
  29.             self.cursor.execute("SELECT quote,date_added FROM quotes WHERE id='%s'" %self.randquoteid())
  30.             result = self.cursor.fetchone()
  31.             return result
  32.         else:
  33.             return False
  34.  
  35.     def url(self, url, date, addedby):
  36.         existQuery = self.cursor.execute("SELECT url,date_added,added_by FROM urls WHERE url LIKE %s", ("%"+url+"%"))
  37.         exist = self.cursor.fetchone()
  38.         if not exist:
  39.             self.cursor.execute("INSERT INTO urls (url,date_added,added_by) VALUES ('%s', '%s', '%s')" %(url, date, addedby))
  40.             return False
  41.         else:
  42.             url = exist[0]
  43.             date = exist[1]
  44.             by = exist[2]
  45.             msg = "Url: %s first seen from %s at (%s)" %(url, by, date.split(" ", 1)[0])
  46.             return msg
  47.            
  48.     def close(self):
  49.         self.db.close()
  50.        
  51. class quoteBot(irc.IRCClient):
  52.    
  53.     nickname = ""
  54.     channelkey = ""
  55.     realname = ""
  56.     username = ""
  57.     version = ""
  58.     channel = ""
  59.     mysqlhost = ""
  60.     mysqluser = ""
  61.     mysqlpass = ""
  62.     mysqldb = ""
  63.    
  64.     def connectionMade(self):
  65.         irc.IRCClient.connectionMade(self)
  66.         #self.addquote = addQuote(self.quotelog)
  67.         #self.showquote = showQuote(self.quotelog)
  68.         self.quotedb = dB(self.mysqlhost, self.mysqluser, self.mysqlpass, self.mysqldb)
  69.        
  70.     def connectionLost(self, reason):
  71.         irc.IRCClient.connectionLost(self, reason)
  72.         #self.addquote.close()
  73.         #self.showquote.close()
  74.         self.quotedb.close()
  75.        
  76.     def signedOn(self):
  77.         self.join(self.channel, self.channelkey)
  78.        
  79.     def privmsg(self, user, channel, msg):
  80.         user = user.split("!", 1)[0]
  81.         url = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', msg)
  82.        
  83.         if url:
  84.             date = datetime.datetime.now()
  85.             exist = self.quotedb.url(url[0], date, user)
  86.            
  87.             if exist:
  88.                 self.msg(channel, exist)
  89.        
  90.         if msg.startswith("!addquote"):
  91.             #self.addquote.add(msg.split(" ", 1)[1])
  92.             #date = time.strftime("%Y%m%d%H%M", time.localtime())
  93.             date = datetime.datetime.now()
  94.             msg = msg.split(" ", 1)[1]
  95.             self.quotedb.add(msg, date, user)
  96.             self.msg(channel, "Quote added to database!")
  97.            
  98.         if msg.startswith("!quote"):
  99.             quote = self.quotedb.randquote()
  100.             if quote:
  101.                 msg = "Quote (%s) - %s" %(quote[1].split(" ", 1)[0], quote[0])
  102.                 self.msg(channel, msg)
  103.             else:
  104.                 msg = "Quote database empty."
  105.                 self.msg(channel, msg)
  106.            
  107.     def ctcpQuery_VERSION(self, user, channel, data):
  108.         user = user.split("!", 1)[0]
  109.         self.ctcpMakeReply(user, [("VERSION", "%s" %self.version)])
  110.        
  111. class quoteBotFactory(protocol.ClientFactory):
  112.    
  113.     protocol = quoteBot
  114.    
  115.     def clientConnectionLost(self, connector, reason):
  116.         connector.connect()
  117.        
  118.     def clientConnectionFailed(self, connector, reason):
  119.         reactor.stop()
  120.        
  121. if __name__ == '__main__':
  122.     #log.startLogging(sys.stdout)
  123.     f = quoteBotFactory()
  124.     reactor.connectTCP("irc.inet.tele.dk", 6667, f)
  125.     reactor.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement