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.

510 lines
21 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. #change to False here, if you want to have the update run at server start
  36. already_updated = True
  37. start = True
  38. import datetime
  39. from datetime import timedelta
  40. while True:
  41. time.sleep(2)
  42. #already_updated = True
  43. now = datetime.datetime.now()
  44. current_hour = now.strftime("%H")
  45. #print(current_hour)
  46. # run variable update and creation at start
  47. if start == True:
  48. room_list = []
  49. rooms = os.listdir('rooms')
  50. for room in rooms:
  51. room_file = open('rooms/' + room, 'r')
  52. room_file_raw = room_file.read()
  53. room_file.close()
  54. room_file_list = room_file_raw.split('§%§%')
  55. print(room_list)
  56. print(room)
  57. print(room_file_list)
  58. room_list.append([room[:-4], room_file_list[0], room_file_list[1], room_file_list[2]])
  59. with open(spider_directory + '/spiders/config.yaml' , "r") as stream:
  60. try:
  61. config = yaml.safe_load(stream)
  62. except yaml.YAMLError as exc:
  63. print(exc)
  64. fdb_list = []
  65. #print(config)
  66. for key in config:
  67. fdb_list.append(key)
  68. start = False
  69. if int(current_hour) > 11:
  70. aftersix = True
  71. if int(current_hour) <= 11:
  72. aftersix = False
  73. already_updated = False
  74. if aftersix == True and already_updated == False:
  75. room_list = []
  76. rooms = os.listdir('rooms')
  77. for room in rooms:
  78. room_file = open('rooms/' + room, 'r')
  79. room_file_raw = room_file.read()
  80. room_file.close()
  81. room_file_list = room_file_raw.split('§%§%')
  82. room_list.append([room[:-4], room_file_list[0], room_file_list[1], room_file_list[2]])
  83. with open(spider_directory + '/spiders/config.yaml' , "r") as stream:
  84. try:
  85. config = yaml.safe_load(stream)
  86. except yaml.YAMLError as exc:
  87. print(exc)
  88. fdb_list = []
  89. #print(config)
  90. for key in config:
  91. fdb_list.append(key)
  92. data = dict({})
  93. for room in room_list:
  94. print(room[0])
  95. room_fdbs = room[2]
  96. room_tags = room[3]
  97. #subprocess.run(["python", spider_directory + 'main.py', fdbs])
  98. for room_fdb in eval(room_fdbs):
  99. #print('room_fdb',room_fdb, 'fdb_list',fdb_list)
  100. try:
  101. iteration_var_list = config.get(room_fdb).get("entry-list").get("iteration-var-list")
  102. except Exception as e:
  103. print('there was an error with the entry-list parameter in the config regarding the fdb ', room_fdb)
  104. if room_fdb in fdb_list and room_fdb not in [key for key in data]:
  105. iterdict = {}
  106. for i in eval(iteration_var_list):
  107. f = open(spider_directory + "/spiders/output/" + room_fdb + str(i) + "entryList.txt")
  108. text = f.read()
  109. dictionary_entry_list = eval(text)
  110. iterdict[i] = dictionary_entry_list
  111. data[room_fdb] = iterdict
  112. for i in eval(iteration_var_list):
  113. try:
  114. print(room_fdb, i)
  115. for key in data[room_fdb][i]:
  116. contains_tag = False
  117. name_data_lower = [word.lower() for word in data[room_fdb][i][key]["name"].split(' ')]
  118. info_data_lower = [word.lower() for word in data[room_fdb][i][key]["info"].split(' ')]
  119. text_data_lower = [word.lower() for word in data[room_fdb][i][key]["text"].split(' ')]
  120. for tag in eval(room_tags):
  121. if tag.lower() in (name_data_lower or info_data_lower or text_data_lower):
  122. contains_tag = True
  123. if contains_tag == True:
  124. try:
  125. url = data[room_fdb][i][key]["domain"]
  126. except:
  127. url = data[room_fdb][i][key]["link"]
  128. entry_message = '<' + url + '|' + data[room_fdb][i][key]["name"]+ '>' + '\n' + data[room_fdb][i][key]["info"]
  129. api.send_message(entry_message, room[0])
  130. except Exception as e:
  131. print("probably i was not there in last page, original error is:", e)
  132. #print('data',data['giz'][2],'data')
  133. already_updated = True
  134. # if datestime.split over etc and updated= true etc
  135. n += 1
  136. print(n)
  137. try:
  138. #print('getting the room history')
  139. date = datetime.datetime.now() - timedelta(days=3)
  140. room_history = api.get_private_room_history(room_id, oldest=date)
  141. except Exception as e:
  142. time.sleep(10)
  143. api = RocketChatAPI(settings={'username': botname, 'password': botpassword, 'domain': server_url})
  144. time.sleep(5)
  145. room_history = api.get_private_room_history(room_id, oldest=date)
  146. print('got a connection error, original message is:',e)
  147. messages_list = []
  148. for message in room_history['messages']:
  149. messages_list.append(message)
  150. if len(messages_list) >= 1:
  151. #print('blub', messages_list)
  152. latest_message_user_id = messages_list[0]['u']['_id']
  153. latest_message_user_username = messages_list[0]['u']['username']
  154. latest_message = messages_list[0]['msg']
  155. latest_message_id = messages_list[0]['_id']
  156. new_message_file = open('new_message_file.txt', 'r')
  157. new_message = new_message_file.read()
  158. new_message_file.close()
  159. new_message_list = new_message.split('§%§%')
  160. #print(latest_message, new_message_list[0])
  161. if new_message_list[0] != latest_message and new_message_list[1] != latest_message_id and latest_message_user_id != bot_user_id:
  162. answer = 'Ich habe kein Kommando erhalten.'
  163. new_message_file = open('new_message_file.txt', 'w')
  164. new_message_file.write(latest_message + '§%§%' + latest_message_id)
  165. new_message_file.close()
  166. user_input = latest_message
  167. user_input_list = user_input.split(' ')
  168. if user_input_list[0] == 'addtags':
  169. try:
  170. room_id_add_tags = user_input_list[1]
  171. except:
  172. room_id_add_tags = 'NONE'
  173. try:
  174. new_tags = user_input_list[2]
  175. except:
  176. new_tags = 'NONE'
  177. try:
  178. thirdarg = user_input_list[3]
  179. except:
  180. thirdarg = 'NONE'
  181. if len(room_id_add_tags) >= 1 and len(new_tags) >= 1 and thirdarg == 'NONE':
  182. try:
  183. room_file_add_tags = open('rooms/' + user_input_list[1] + '.txt', 'r')
  184. room_info_raw = room_file_add_tags.read()
  185. room_file_add_tags.close()
  186. room_info = room_info_raw.split('§%§%')
  187. tag_list = eval(room_info[-1])
  188. for tag in eval(user_input_list[2]):
  189. if tag not in tag_list:
  190. tag_list.append(tag)
  191. room_file_add_tags = open('rooms/' + user_input_list[1] + '.txt', 'w')
  192. room_file_add_tags.write( str(room_info[0]) + '§%§%' + str(room_info[1]) + '§%§%' + str(tag_list))
  193. room_file_add_tags.close()
  194. answer = 'the updated tag list is' + str(tag_list)
  195. except Exception as e:
  196. print('error opening, original error is:', e)
  197. answer = "The room_id to update the tags was not found"
  198. if user_input_list[0] == 'printtags':
  199. if len(user_input_list[1]) >= 1:
  200. try:
  201. room_file = open('rooms/' + user_input_list[1] + '.txt', 'r')
  202. room_info_raw = room_file.read()
  203. room_file.close()
  204. room_info = room_info_raw.split('§%§%')
  205. tag_list = eval(room_info[-1])
  206. answer = tag_list
  207. except Exception as e:
  208. print('error opening, original error is:', e)
  209. answer = "The room_id to get the tags was not found"
  210. if user_input_list[0] == 'printcommands':
  211. answer = """
  212. To print all available rooms and their configuration, use
  213. `printrooms`
  214. To print all available fdbs, use
  215. `printfdbs`
  216. To update all rooms use
  217. `updaterooms`
  218. To update one room use the room_id from the output of printrooms:
  219. `updaterooms <room_id>`
  220. To create a room use following command, but be sure to not have spaces in your lists, as a space indicates a new command argument:
  221. `createroom <room-name> <list-fdbs> <list-tags>`
  222. To delete a room use
  223. `deleteroom <room_id>`
  224. """
  225. if user_input_list[0] == 'printrooms':
  226. room_was_found = False
  227. # get all the rooms
  228. import os
  229. room_list = []
  230. rooms = os.listdir('rooms')
  231. for room in rooms:
  232. room_file = open('rooms/' + room, 'r')
  233. room_file_raw = room_file.read()
  234. room_file.close()
  235. room_file_list = room_file_raw.split('§%§%')
  236. room_list.append([room[:-4], room_file_list[0], room_file_list[1], room_file_list[2]])
  237. #print(room_list)
  238. answer = str(room_list)
  239. if user_input_list[0] == 'updaterooms':
  240. if len(user_input_list[1]) >= 1:
  241. room_to_update = user_input_list[1]
  242. else:
  243. room_to_update = 'NONE'
  244. room_list = []
  245. rooms = os.listdir('rooms')
  246. for room in rooms:
  247. room_file = open('rooms/' + room, 'r')
  248. room_file_raw = room_file.read()
  249. room_file.close()
  250. room_file_list = room_file_raw.split('§%§%')
  251. room_list.append([room[:-4], room_file_list[0], room_file_list[1], room_file_list[2]])
  252. with open(spider_directory + '/spiders/config.yaml' , "r") as stream:
  253. try:
  254. config = yaml.safe_load(stream)
  255. except yaml.YAMLError as exc:
  256. print(exc)
  257. fdb_list = []
  258. #print(config)
  259. for key in config:
  260. fdb_list.append(key)
  261. answer = "Ich update die Rooms auf Basis der Daten von heute morgen um 6 Uhr.."
  262. data = dict({})
  263. for room in room_list:
  264. if room[0] == room_to_update or room_to_update == 'NONE':
  265. room_was_found = True
  266. print(room[0])
  267. room_fdbs = room[2]
  268. room_tags = room[3]
  269. #subprocess.run(["python", spider_directory + 'main.py', fdbs])
  270. for room_fdb in eval(room_fdbs):
  271. #print('room_fdb',room_fdb, 'fdb_list',fdb_list)
  272. try:
  273. iteration_var_list = config.get(room_fdb).get("entry-list").get("iteration-var-list")
  274. except Exception as e:
  275. print('there was an error with the entry-list parameter in the config regarding the fdb ', room_fdb)
  276. if room_fdb in fdb_list and room_fdb not in [key for key in data]:
  277. iterdict = {}
  278. for i in eval(iteration_var_list):
  279. f = open(spider_directory + "/spiders/output/" + room_fdb + str(i) + "entryList.txt")
  280. text = f.read()
  281. dictionary_entry_list = eval(text)
  282. iterdict[i] = dictionary_entry_list
  283. data[room_fdb] = iterdict
  284. for i in eval(iteration_var_list):
  285. try:
  286. print(room_fdb, i)
  287. for key in data[room_fdb][i]:
  288. contains_tag = False
  289. name_data_lower = [word.lower() for word in data[room_fdb][i][key]["name"].split(' ')]
  290. info_data_lower = [word.lower() for word in data[room_fdb][i][key]["info"].split(' ')]
  291. text_data_lower = [word.lower() for word in data[room_fdb][i][key]["text"].split(' ')]
  292. for tag in eval(room_tags):
  293. if tag.lower() in (name_data_lower or info_data_lower or text_data_lower):
  294. contains_tag = True
  295. if contains_tag == True:
  296. try:
  297. url = data[room_fdb][i][key]["domain"]
  298. except:
  299. url = data[room_fdb][i][key]["link"]
  300. entry_message = '<' + url + '|' + data[room_fdb][i][key]["name"]+ '>' + '\n' + data[room_fdb][i][key]["info"]
  301. api.send_message(entry_message, room[0])
  302. except Exception as e:
  303. print("probably i was not there in last page, original error is:", e)
  304. if room_was_found == False:
  305. answer = 'Den angegebenen Raum gibt es nicht.. Versuche es noch einmal mit -> updaterooms <room-id>'
  306. if room_was_found == True:
  307. answer = "Ich update die Rooms auf Basis der Daten in fdb-spider/spiders/output.."
  308. if user_input_list[0] == 'printfdbs':
  309. answer = str(fdb_list)
  310. if len(user_input_list) > 1:
  311. answer = 'Die Syntax zum Ausgeben der konfigurierten Förderdatenbanken hat einen Fehler. Versuche es erneut, mit -> printfdbs'
  312. if user_input_list[0] == 'deleteroom':
  313. try:
  314. room_id_del = user_input_list[1]
  315. except Exception as e:
  316. room_id_del = 'NONE'
  317. print(e)
  318. os.remove("rooms/" + room_id_del + ".txt")
  319. anwer = 'deleting the requested room..'
  320. if len(user_input_list) > 2:
  321. answer = 'Die Syntax zum Löschen eines Raumes hat einen Fehler. Versuche es erneut, nach dem Muster -> deleteroom <room_id>'
  322. if user_input_list[0] == 'createroom':
  323. try:
  324. room_name = user_input_list[1]
  325. except Exception as e:
  326. room_name = 'NONE'
  327. print(e)
  328. try:
  329. databases = user_input_list[2]
  330. except Exception as e:
  331. databases = 'NONE'
  332. print(e)
  333. try:
  334. filters = user_input_list[3]
  335. except Exception as e:
  336. filters = 'NONE'
  337. print(e)
  338. if len(user_input_list) > 3:
  339. 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"]'
  340. if room_name == 'NONE' or databases == 'NONE' or filters == 'NONE':
  341. answer = 'Um einen Raum zu erstellen, in dem neueste Einträge geteilt werden, lautet die Syntax: createroom <Raumname> <Liste-Datenbanken> <Liste-Filtertags>'
  342. else:
  343. try:
  344. new_room = api.create_public_room(room_name,
  345. members=[latest_message_user_username],
  346. read_only=True)
  347. new_room_id = new_room['channel']['_id']
  348. room_file = open('rooms/' + new_room_id + '.txt', 'w')
  349. room_file.write( room_name + '§%§%' + databases + '§%§%' + filters)
  350. room_file.close()
  351. answer = 'Der Command wurde übermittelt'
  352. except Exception as e:
  353. print('it was not able to create the room, the original error message is:', e)
  354. answer = 'There was an error creating the room, look up the logs.. the original error was: ' + str(e)
  355. #print('oi', user_input)
  356. api.send_message('Die Anfrage wird bearbeitet..', room_id)
  357. # here comes the code interacting with the spiders output json
  358. #answer = 'the up to date entries are: oi Oi Oi!'
  359. api.send_message(answer, room_id)
  360. time.sleep(1)
  361. api.send_message('Ich bin wieder bereit für Konfigurationsinput : )', room_id)
  362. time.sleep(1)