Advertisement
Guest User

Untitled

a guest
Nov 7th, 2015
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.48 KB | None | 0 0
  1. import discord
  2. import logging
  3. import random
  4. import argparse
  5. import lxml
  6. import sys
  7. import urllib
  8. from lxml.html import fromstring
  9. from lxml.cssselect import CSSSelector
  10.  
  11.  
  12. parser = argparse.ArgumentParser()
  13. parser.add_argument("-n", "--number",type=int, help="found quote by ID from dtc" )
  14. parser.add_argument("-r", "--random", action='store_true', help="Random quote from dtc" )
  15. parser.add_argument("-l", "--last", action='store_true', help="last quote from dtc")
  16. parser.add_argument("-a", "--all", action='store_true', help="all quote from dtc")
  17. args = parser.parse_args()
  18.  
  19. class Quote(object):
  20.     """Contains a Quote"""
  21.     def __init__(self,number):
  22.         self.number = number
  23.         self.url = "http://danstonchat.com/{}.html".format(self.number)
  24.         try:
  25.             self.data = urllib.urlopen(self.url)
  26.         except Exception, e:
  27.             raise e
  28.         if self.data.getcode() != 200:
  29.             print(self.data.getcode())
  30.         self.html = self.data.read()
  31.         self.parse()
  32.  
  33.     def parse (self):
  34.         r = fromstring(self.html)
  35.         self.end = 'Quote number %s' % self.number
  36.         for el in r.find_class('decoration'):
  37.            
  38.             self.end = self.end+"\n"+lxml.html.tostring(el , encoding = "unicode" ).replace('<span class="decoration">','').replace('</span>','')
  39.             self.end = self.end.replace('&lt;','<').replace('&gt;','>')
  40.            
  41.  
  42.     def display(self):
  43.         print " "
  44.         print(self.end)
  45.         print " "
  46.  
  47.     def return_raw(self):
  48.         return self.end.encode("utf-8")
  49.  
  50.  
  51. def getlast():
  52.     lastid = urllib.urlopen("http://danstonchat.com/latest.html").read()
  53.     r = fromstring(lastid)
  54.     sel = CSSSelector('span')
  55.     a = [e.get('id') for e in sel(r)]
  56.     for x in a:
  57.         if x != None :
  58.             last = x
  59.             break
  60.     if last:
  61.         return int(last)
  62.  
  63. # Set up the logging module to output diagnostic to the console.
  64. logging.basicConfig()
  65.  
  66. client = discord.Client()
  67. client.login('pierre_d@live.fr', '1234AZER')
  68.  
  69. if not client.is_logged_in:
  70.     print('Logging in to Discord failed')
  71.     exit(1)
  72.  
  73. @client.event
  74. def on_ready():
  75.     print('Connected!')
  76.     print('Username: ' + client.user.name)
  77.     print('ID: ' + client.user.id)
  78.  
  79. @client.event
  80. def on_message(message):
  81.     if message.author.id != client.user.id:
  82.     mots = message.content.split(' ')
  83.    
  84.  
  85.     if message.content == "!bashfr":
  86.         quote = Quote(random.randint(1,getlast()))
  87.  
  88.             client.send_message(message.channel, quote.return_raw())
  89.     if morts.amount()==2:
  90.         if "!biere" == mots[0]:
  91.             client.send_message(message.channel, "@"+message.author.name+ " offre une :beer: a @"+mots[1])
  92.  
  93.  
  94. client.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement