Advertisement
Guest User

Untitled

a guest
Jul 25th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import sys
  3. import discord
  4. import asyncio
  5. import getpass
  6. import requests
  7. import time
  8. import re
  9. from bs4 import BeautifulSoup
  10. import pprint
  11. import json
  12. import signal
  13.  
  14. client = discord.Client()
  15. master = "188846528553549824" #aiglebleu
  16. server_id = "181163144163491840" #hightechlowlife.eu
  17. channel_id = "181163144163491840" #htll
  18. url_login = "https://hightechlowlife.eu/board/login/login"
  19. url_shouty = "https://hightechlowlife.eu/board/index.php?chat/refresh"
  20. headers = { 'User-Agent' : "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36" }
  21.  
  22. # Thanks to https://github.com/htll/Charitum/blob/master/charitum.py
  23. def callback_shutdown( signal, frame ):
  24. import sys
  25. print( "\nShutting down...\n" )
  26. sys.exit(0)
  27.  
  28. # Thanks to https://github.com/SteveMcGrath/pyxenforo/blob/master/xenforo.py
  29. def getToken(page):
  30. token_rex = re.compile(r'\<a href\=\"logout/\?_xfToken\=(.+?[^\"])\"')
  31. token = token_rex.findall(page)[0]
  32. token = token.replace('%2C',',')
  33. return token
  34.  
  35. # updates the session element with session cookies
  36. def login():
  37. # Prompt user for login and pass
  38. username = input("Username: ")
  39. password = getpass.getpass("Password: ")
  40. payload = { 'login' : username, 'password' : password, 'remember' : '1', 'register' : 0 }
  41.  
  42. s.post(url_login, data=payload, headers=headers)
  43. return username
  44.  
  45. # Returns a soup object of the html in the shoutbox
  46. def seekShouty(lastID):
  47. payload = {'_xfResponseType' : 'json', '_xfToken' : token, 'last_id' : lastID}
  48. r = s.post(url_shouty, headers=headers, data=payload)
  49. t = json.loads(r.text)
  50. return BeautifulSoup(t["messages"], "lxml")
  51.  
  52. # Prints the shouty to console
  53. async def showShouty():
  54. await client.wait_until_ready()
  55.  
  56. lastID = 0
  57. channel = discord.Object(id=channel_id)
  58. while not client.is_closed:
  59. soup = seekShouty(lastID)
  60. users = []
  61. for i in soup.findAll('li'):
  62. if i.has_attr('data-author'):
  63. users.append(i['data-author'])
  64. if int(i['id']) > lastID:
  65. lastID = int(i['id'])
  66. messages = []
  67. for i in soup.findAll('span', {'class':'siropuChatMessage'}):
  68. messages.append(i.text)
  69.  
  70. for i in range(len(messages)):
  71. await client.send_message(channel, "%-20s: %s" % (users[i], messages[i]))
  72. print("%-20s: %s" % (users[i], messages[i]))
  73.  
  74. @client.event
  75. async def on_ready():
  76. print('Logged in as', client.user.name, client.user.id)
  77. for server in client.servers:
  78. print(server.name, server.id)
  79. if server.id == server_id:
  80. for chan in server.channels:
  81. print("\t", chan.name, chan.id)
  82. print('------')
  83.  
  84. @client.event
  85. async def on_message(message):
  86. if message.content.startswith('!') and message.author.id == master:
  87. command = message.content.split()[0][1:]
  88. args = message.content.split()[1:]
  89. print(command)
  90. print(args)
  91. await client.send_message(message.channel, commands[command](args))
  92. if message.content.startswith('!test'):
  93. counter = 0
  94. tmp = await client.send_message(message.channel, 'Calculating messages...')
  95. async for log in client.logs_from(message.channel, limit=100):
  96. if log.author == message.author:
  97. counter += 1
  98.  
  99. await client.edit_message(tmp, 'You have {} messages.'.format(counter))
  100. elif message.content.startswith('!sleep'):
  101. await client.send_message(message.channel, 'ZzzzzzZZzzzz')
  102. await asyncio.sleep(5)
  103. await client.send_message(message.channel, 'Done sleeping')
  104. elif message.content.startswith('!whoami'):
  105. await client.send_message(message.channel, message.author.id)
  106.  
  107. def do_add(args):
  108. try:
  109. if len(args) >= 2:
  110. args = list(map(int, args))
  111. return str(sum(args))
  112. else:
  113. return "[ERROR]: Requires at least 2 arguments"
  114. except:
  115. return "[ERROR]: Bad input format"
  116.  
  117. commands = {'add': do_add}
  118.  
  119. with requests.session() as s:
  120. signal.signal( signal.SIGINT, callback_shutdown ) # register graceful shutdown here
  121.  
  122. if len(sys.argv) > 1:
  123. if sys.argv[1] == "bridge":
  124. username = login()
  125. token = getToken(s.get(url_shouty, headers=headers).text)
  126. client.loop.create_task(showShouty())
  127.  
  128. client.run('ENTER DISCORD SECRET BOT API KEY HERE')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement