Guest User

Untitled

a guest
Jun 21st, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. import telebot
  2. import json
  3. import urllib.request
  4. bot = telebot.TeleBot(*ТУТ ТОКЕН*)
  5. class Getter:
  6. def processMessage(self, message):
  7. if message.text.find('/парсить') != 0:
  8. return 0
  9. if len(message.text) > len("/парсить") + 2:
  10. string = ""
  11. for x in range(len("/парсить") + 1, len(message.text)):
  12. string += message.text[x]
  13. request = urllib.request.Request("https://2ch.hk/b/res/" + string + ".json")
  14. response = urllib.request.urlopen(request)
  15. processed = json.loads(response.read())
  16. webms = []
  17. for post in processed['threads'][0]['posts']:
  18. if 'files' not in post:
  19. continue
  20. for file in post['files']:
  21. if file['name'].find(".webm") != -1:
  22. webms.append(file['path'])
  23. if len(webms) != 0:
  24. for x in webms:
  25. request = urllib.request.Request("https://2ch.hk" + x)
  26. bot.send_video(message.chat.id, urllib.request.urlopen(request).read())
  27. return 1
  28. class PlaceHolder():
  29. def processMessage(self, message):
  30. return 0
  31. handlers = (Getter(), PlaceHolder())
  32. def handleMessages(messages):
  33. for message in messages:
  34. if message.content_type == 'text':
  35. for handler in handlers:
  36. if handler.processMessage(message) == 1:
  37. break
  38. bot.set_update_listener(handleMessages)
  39. bot.polling()
Advertisement
Add Comment
Please, Sign In to add comment