#matrix_bot.py# 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # GPL3 or later
  2. import os
  3. import sys
  4. import traceback
  5. import time
  6. import datetime
  7. import randomun
  8. import threading
  9. import json
  10. import simplematrixbotlib as botlib
  11. import youtube
  12. import commands
  13. from ui import *
  14. # Calculating today's data for volume voting!
  15. session = {"bot":None,
  16. "room":None,
  17. "now_playing":None}
  18. def files_manager():
  19. # This fucntion will run in it's own thread and manage downloading songs.
  20. while True:
  21. try:
  22. time.sleep(5)
  23. with open('stuff.json') as json_file:
  24. stuff = json.load(json_file)
  25. for videoId in stuff.get("songs", {}):
  26. song = stuff.get("songs", {}).get(videoId, {})
  27. if not song.get("file"):
  28. print(" [ "+clr["tbbu"]+"GETTING"+clr["norm"]+" ] "+videoId)
  29. d = youtube.insure_song(videoId, videoId)
  30. if d:
  31. with open('stuff.json') as json_file:
  32. stuff = json.load(json_file)
  33. stuff["songs"][videoId]["file"] = d
  34. stuff["songs"][videoId]["timestamp"] = int(time.time())
  35. with open('stuff.json', 'w') as fp:
  36. json.dump(stuff, fp, indent=4)
  37. except:
  38. pass
  39. # def playing_now():
  40. # try:
  41. # with open('stuff.json') as json_file:
  42. # stuff = json.load(json_file)
  43. # if stuff.get("now_playing") != session["now_playing"]:
  44. # session["now_playing"] = stuff.get("now_playing")
  45. # m = "Now Playing: " + stuff.get("now_playing", "")
  46. # try:
  47. # session["bot"].api.send_markdown_message(session["room"].room_id, m)
  48. # except:
  49. # pass
  50. # except Exception as e:
  51. # print(e)
  52. t2 = threading.Thread(target=files_manager)
  53. t2.setDaemon(True)
  54. t2.start()
  55. # t3 = threading.Thread(target=playing_now)
  56. # t3.setDaemon(True)
  57. # t3.start()
  58. #[ OKAY ] 17yWeynOfOI | 03:32.0
  59. # [ GETTING ] 17yWeynOfOI | 03:32.0
  60. with open('stuff.json') as json_file:
  61. stuff = json.load(json_file)
  62. matrix_username = stuff["matrix"]["username"]
  63. matrix_password = stuff["matrix"]["password"]
  64. the_room = stuff["matrix"]["room"]
  65. print("Searching on the '"+the_room+"' room.")
  66. server = "https://"+matrix_username[matrix_username.find(":")+1:]
  67. creds = botlib.Creds(server, matrix_username, matrix_password)
  68. bot = botlib.Bot(creds)
  69. session["bot"] = bot
  70. PREFIX = '!'
  71. @bot.listener.on_message_event
  72. async def echo(room, message):
  73. with open('stuff.json') as json_file:
  74. stuff = json.load(json_file)
  75. new_date_format = "%Y/%m/%d"
  76. today = datetime.datetime.strftime(datetime.datetime.today(), new_date_format)
  77. for d in range(400):
  78. yesterday = datetime.datetime.strftime(datetime.datetime.today()-datetime.timedelta(days=d+1), new_date_format)
  79. if yesterday in stuff.get("volume", {}):
  80. break
  81. if room.name == the_room:
  82. session["room"] = room
  83. username = str(message.sender)
  84. if matrix_username != username:
  85. print(" [ "+clr["bold"]+clr["tbbu"]+"MESSAGE"+clr["norm"]+" ] "+str(message).replace("\n", "\n "))
  86. answer = commands.execute(username, message.body)
  87. if not answer:
  88. return
  89. sendis = ""
  90. for n, i in enumerate(answer.split("\n")):
  91. sendis = sendis+"\n"+i
  92. if n % 100 == 0:
  93. print(" [ "+clr["tbbu"]+"SENDING"+clr["norm"]+" ] "+sendis.replace("\n", "\n "))
  94. await bot.api.send_markdown_message(room.room_id, sendis)
  95. sendis = ""
  96. time.sleep(2)
  97. print(" [ "+clr["tbbu"]+"SENDING"+clr["norm"]+" ] "+sendis.replace("\n", "\n "))
  98. await bot.api.send_markdown_message(room.room_id, sendis)
  99. return
  100. while True:
  101. try:
  102. bot.run()
  103. except:
  104. message = str(traceback.format_exc())
  105. print()
  106. print(" [ "+clr["tbrd"]+"ERROR!"+clr["norm"]+" ]", message.replace("\n", "\n "))
  107. print()