Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import discord
- import logging
- import random
- import argparse
- import lxml
- import sys
- import urllib
- from lxml.html import fromstring
- from lxml.cssselect import CSSSelector
- parser = argparse.ArgumentParser()
- parser.add_argument("-n", "--number",type=int, help="found quote by ID from dtc" )
- parser.add_argument("-r", "--random", action='store_true', help="Random quote from dtc" )
- parser.add_argument("-l", "--last", action='store_true', help="last quote from dtc")
- parser.add_argument("-a", "--all", action='store_true', help="all quote from dtc")
- args = parser.parse_args()
- class Quote(object):
- """Contains a Quote"""
- def __init__(self,number):
- self.number = number
- self.url = "http://danstonchat.com/{}.html".format(self.number)
- try:
- self.data = urllib.urlopen(self.url)
- except Exception, e:
- raise e
- if self.data.getcode() != 200:
- print(self.data.getcode())
- self.html = self.data.read()
- self.parse()
- def parse (self):
- r = fromstring(self.html)
- self.end = 'Quote number %s' % self.number
- for el in r.find_class('decoration'):
- self.end = self.end+"\n"+lxml.html.tostring(el , encoding = "unicode" ).replace('<span class="decoration">','').replace('</span>','')
- self.end = self.end.replace('<','<').replace('>','>')
- def display(self):
- print " "
- print(self.end)
- print " "
- def return_raw(self):
- return self.end.encode("utf-8")
- def getlast():
- lastid = urllib.urlopen("http://danstonchat.com/latest.html").read()
- r = fromstring(lastid)
- sel = CSSSelector('span')
- a = [e.get('id') for e in sel(r)]
- for x in a:
- if x != None :
- last = x
- break
- if last:
- return int(last)
- # Set up the logging module to output diagnostic to the console.
- logging.basicConfig()
- client = discord.Client()
- client.login('pierre_d@live.fr', '1234AZER')
- if not client.is_logged_in:
- print('Logging in to Discord failed')
- exit(1)
- @client.event
- def on_ready():
- print('Connected!')
- print('Username: ' + client.user.name)
- print('ID: ' + client.user.id)
- @client.event
- def on_message(message):
- if message.author.id != client.user.id:
- mots = message.content.split(' ')
- if message.content == "!bashfr":
- quote = Quote(random.randint(1,getlast()))
- client.send_message(message.channel, quote.return_raw())
- if morts.amount()==2:
- if "!biere" == mots[0]:
- client.send_message(message.channel, "@"+message.author.name+ " offre une :beer: a @"+mots[1])
- client.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement