The Interface of the fdb-spider, based on rocketchat
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

283 lines
9.8 KiB

  1. import os
  2. from rocketchat.api import RocketChatAPI
  3. import configparser
  4. import json
  5. import requests
  6. import yaml
  7. import subprocess
  8. config = configparser.ConfigParser()
  9. config.read('config.ini')
  10. botname = config['Chat']['username']
  11. botpassword = config['Chat']['password']
  12. server_url = config['Chat']['URL']
  13. room_id = config['Chat']['room_id']
  14. bot_user_id = config['Chat']['bot_user_id']
  15. spider_directory = config['Spider']['spider_directory']
  16. # here comes the functions to talk to gpt
  17. # For local streaming, the websockets are hosted without ssl - http://
  18. HOST = 'localhost:5000'
  19. URI = f'http://{HOST}/api/v1/chat'
  20. #URI = f'http://{HOST}/api'
  21. # http://192.168.9.197:5000/api/v1/chat
  22. # For reverse-proxied streaming, the remote will likely host with ssl - https://
  23. # URI = 'https://your-uri-here.trycloudflare.com/api/v1/chat'
  24. if __name__ == '__main__':
  25. api = RocketChatAPI(settings={'username': botname, 'password': botpassword, 'domain': server_url})
  26. # api.send_message('Ciao, I am the fdb-spider', room_id)
  27. # myinfo = api.get_my_info()
  28. # print(myinfo)
  29. # rooms = api.get_private_rooms()
  30. # print(rooms)
  31. # api.send_message('Ole', room_id)
  32. n = 0
  33. import time
  34. import schedule
  35. already_updated = False
  36. import datetime
  37. from datetime import timedelta
  38. while True:
  39. time.sleep(4)
  40. #already_updated = True
  41. now = datetime.datetime.now()
  42. current_hour = now.strftime("%H")
  43. if int(current_hour) > 6:
  44. aftersix = True
  45. if int(current_hour) < 6:
  46. aftersix = False
  47. already_updated == False
  48. if aftersix == True and already_updated == False:
  49. room_list = []
  50. rooms = os.listdir('rooms')
  51. for room in rooms:
  52. room_file = open('rooms/' + room, 'r')
  53. room_file_raw = room_file.read()
  54. room_file.close()
  55. room_file_list = room_file_raw.split('§%§%')
  56. room_list.append([room[:-4], room_file_list[0], room_file_list[1], room_file_list[2]])
  57. with open(spider_directory + '/spiders/config.yaml' , "r") as stream:
  58. try:
  59. config = yaml.safe_load(stream)
  60. except yaml.YAMLError as exc:
  61. print(exc)
  62. fdb_list = []
  63. #print(config)
  64. for key in config:
  65. fdb_list.append(key)
  66. data = dict({})
  67. for room in room_list:
  68. print(room[0])
  69. room_fdbs = room[2]
  70. #subprocess.run(["python", spider_directory + 'main.py', fdbs])
  71. for room_fdb in eval(room_fdbs):
  72. #print('room_fdb',room_fdb, 'fdb_list',fdb_list)
  73. if room_fdb in fdb_list:
  74. try:
  75. iteration_var_list = config.get(room_fdb).get("entry-list").get("iteration-var-list")
  76. except Exception as e:
  77. print('there was an error with the entry-list parameter in the config regarding the fdb ', room_fdb)
  78. iterdict = {}
  79. for i in eval(iteration_var_list):
  80. f = open(spider_directory + "/spiders/output/" + room_fdb + str(i) + "entryList.txt")
  81. text = f.read()
  82. dictionary_entry_list = eval(text)
  83. iterdict[i] = dictionary_entry_list
  84. data[room_fdb] = iterdict
  85. print('data',data['giz'][2],'data')
  86. already_updated = True
  87. # if datestime.split over etc and updated= true etc
  88. n += 1
  89. print(n)
  90. try:
  91. #print('getting the room history')
  92. date = datetime.datetime.now() - timedelta(days=3)
  93. room_history = api.get_private_room_history(room_id, oldest=date)
  94. except Exception as e:
  95. time.sleep(10)
  96. api = RocketChatAPI(settings={'username': botname, 'password': botpassword, 'domain': server_url})
  97. time.sleep(5)
  98. room_history = api.get_private_room_history(room_id, oldest=date)
  99. print('got a connection error, original message is:',e)
  100. messages_list = []
  101. for message in room_history['messages']:
  102. messages_list.append(message)
  103. if len(messages_list) >= 1:
  104. #print('blub', messages_list)
  105. latest_message_user_id = messages_list[0]['u']['_id']
  106. latest_message_user_username = messages_list[0]['u']['username']
  107. latest_message = messages_list[0]['msg']
  108. latest_message_id = messages_list[0]['_id']
  109. new_message_file = open('new_message_file.txt', 'r')
  110. new_message = new_message_file.read()
  111. new_message_file.close()
  112. new_message_list = new_message.split('§%§%')
  113. #print(latest_message, new_message_list[0])
  114. if new_message_list[0] != latest_message and new_message_list[1] != latest_message_id and latest_message_user_id != bot_user_id:
  115. answer = 'Ich habe kein Kommando erhalten.'
  116. new_message_file = open('new_message_file.txt', 'w')
  117. new_message_file.write(latest_message + '§%§%' + latest_message_id)
  118. new_message_file.close()
  119. user_input = latest_message
  120. user_input_list = user_input.split(' ')
  121. if user_input_list[0] == 'printrooms':
  122. # get all the rooms
  123. import os
  124. room_list = []
  125. rooms = os.listdir('rooms')
  126. for room in rooms:
  127. room_file = open('rooms/' + room, 'r')
  128. room_file_raw = room_file.read()
  129. room_file.close()
  130. room_file_list = room_file_raw.split('§%§%')
  131. room_list.append([room[:-4], room_file_list[0], room_file_list[1], room_file_list[2]])
  132. #print(room_list)
  133. answer = str(room_list)
  134. if user_input_list[0] == 'printfdbs':
  135. answer = str(fdb_list)
  136. if len(user_input_list) > 1:
  137. answer = 'Die Syntax zum Ausgeben der konfigurierten Förderdatenbanken hat einen Fehler. Versuche es erneut, mit -> printfdbs'
  138. if user_input_list[0] == 'deleteroom':
  139. try:
  140. room_id_del = user_input_list[1]
  141. except Exception as e:
  142. room_id_del = 'NONE'
  143. print(e)
  144. os.remove("rooms/" + room_id_del + ".txt")
  145. anwer = 'deleting the requested room..'
  146. if len(user_input_list) > 2:
  147. answer = 'Die Syntax zum Löschen eines Raumes hat einen Fehler. Versuche es erneut, nach dem Muster -> deleteroom <room_id>'
  148. if user_input_list[0] == 'createroom':
  149. try:
  150. room_name = user_input_list[1]
  151. except Exception as e:
  152. room_name = 'NONE'
  153. print(e)
  154. try:
  155. databases = user_input_list[2]
  156. except Exception as e:
  157. databases = 'NONE'
  158. print(e)
  159. try:
  160. filters = user_input_list[3]
  161. except Exception as e:
  162. filters = 'NONE'
  163. print(e)
  164. if len(user_input_list) > 3:
  165. answer = 'Die Syntax hat einen Fehler. Wahrscheinlich sind Leerzeichen in den Listen. Leerzeichen definieren die Syntax. Versuche es erneut, nach dem Muster -> createroom room1 ["fdb1","fdb2"] ["tag1","tag2"]'
  166. if room_name == 'NONE' or databases == 'NONE' or filters == 'NONE':
  167. answer = 'Um einen Raum zu erstellen, in dem neueste Einträge geteilt werden, lautet die Syntax: createroom <Raumname> <Liste-Datenbanken> <Liste-Filtertags>'
  168. else:
  169. try:
  170. new_room = api.create_public_room(room_name,
  171. members=[latest_message_user_username],
  172. read_only=True)
  173. new_room_id = new_room['channel']['_id']
  174. room_file = open('rooms/' + new_room_id + '.txt', 'w')
  175. room_file.write( room_name + '§%§%' + databases + '§%§%' + filters)
  176. room_file.close()
  177. answer = 'Der Command wurde übermittelt'
  178. except Exception as e:
  179. print('it was not able to create the room, the original error message is:', e)
  180. answer = 'There was an error creating the room, look up the logs.. the original error was: ' + str(e)
  181. #print('oi', user_input)
  182. api.send_message('Die Anfrage wird bearbeitet..', room_id)
  183. # here comes the code interacting with the spiders output json
  184. #answer = 'the up to date entries are: oi Oi Oi!'
  185. api.send_message(answer, room_id)
  186. time.sleep(1)
  187. api.send_message('Ich bin wieder bereit für Konfigurationsinput : )', room_id)
  188. time.sleep(1)