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.

533 lines
23 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. try:
  200. room_id_to_print_tags = user_input_list[1]
  201. except:
  202. answer = "after the command printtags, the second argument has to be the room id.. use printrooms and look up the id of the room you want to print the tags"
  203. room_id_to_print_tags = 'NONE'
  204. if room_id_to_print_tags != 'NONE':
  205. try:
  206. room_file = open('rooms/' + room_id_to_print_tags + '.txt', 'r')
  207. room_info_raw = room_file.read()
  208. room_file.close()
  209. room_info = room_info_raw.split('§%§%')
  210. tag_list = eval(room_info[-1])
  211. answer = tag_list
  212. except Exception as e:
  213. print('error opening, original error is:', e)
  214. answer = "The room_id to get the tags was not found"
  215. if user_input_list[0] == 'printcommands':
  216. answer = """
  217. To print all available rooms and their configuration, use
  218. command : `printrooms`
  219. -----------------------------------------------------------------------------------------
  220. To print all available fdbs, use
  221. command : `printfdbs`
  222. -----------------------------------------------------------------------------------------
  223. To update all rooms use
  224. command : `updaterooms`
  225. -----------------------------------------------------------------------------------------
  226. To update one room use the room_id from the output of printrooms:
  227. command : `updaterooms <room-id>`
  228. example : `updaterooms 6572012bebb39dd248d08320`
  229. -----------------------------------------------------------------------------------------
  230. To create a room use following command, but be sure to not have spaces in your lists,
  231. as a space indicates a new command argument:
  232. command : `createroom <room-name> <list-fdbs> <list-tags>`
  233. example : `createroom room-test-1 ['giz','fdb2'] ['tag1','tag2','tag3']`
  234. -----------------------------------------------------------------------------------------
  235. To delete a room use
  236. command : `deleteroom <room-id>`
  237. example : `deleteroom 6572012bebb39dd248d08320`
  238. -----------------------------------------------------------------------------------------
  239. To print the tags of a room use
  240. command : `printtags <room-id>`
  241. example : `printtags 6572012bebb39dd248d08320`
  242. -----------------------------------------------------------------------------------------
  243. To add tags to the existing tags use
  244. command : `addtags <room-id> <tag-list-without-spaces>`
  245. example : `updaterooms 6572012bebb39dd248d08320 ['tag1','tag2','tag3']`
  246. """
  247. if user_input_list[0] == 'printrooms':
  248. room_was_found = False
  249. # get all the rooms
  250. import os
  251. room_list = []
  252. rooms = os.listdir('rooms')
  253. for room in rooms:
  254. room_file = open('rooms/' + room, 'r')
  255. room_file_raw = room_file.read()
  256. room_file.close()
  257. room_file_list = room_file_raw.split('§%§%')
  258. room_list.append([room[:-4], room_file_list[0], room_file_list[1], room_file_list[2]])
  259. #print(room_list)
  260. answer = str(room_list)
  261. if user_input_list[0] == 'updaterooms':
  262. try:
  263. if len(user_input_list[1]) >= 1:
  264. room_to_update = user_input_list[1]
  265. except Exception as e:
  266. room_to_update = 'NONE'
  267. room_list = []
  268. rooms = os.listdir('rooms')
  269. for room in rooms:
  270. room_file = open('rooms/' + room, 'r')
  271. room_file_raw = room_file.read()
  272. room_file.close()
  273. room_file_list = room_file_raw.split('§%§%')
  274. room_list.append([room[:-4], room_file_list[0], room_file_list[1], room_file_list[2]])
  275. with open(spider_directory + '/spiders/config.yaml' , "r") as stream:
  276. try:
  277. config = yaml.safe_load(stream)
  278. except yaml.YAMLError as exc:
  279. print(exc)
  280. fdb_list = []
  281. #print(config)
  282. for key in config:
  283. fdb_list.append(key)
  284. answer = "Ich update die Rooms auf Basis der Daten von heute morgen um 6 Uhr.."
  285. data = dict({})
  286. for room in room_list:
  287. if room[0] == room_to_update or room_to_update == 'NONE':
  288. room_was_found = True
  289. print(room[0])
  290. room_fdbs = room[2]
  291. room_tags = room[3]
  292. #subprocess.run(["python", spider_directory + 'main.py', fdbs])
  293. for room_fdb in eval(room_fdbs):
  294. #print('room_fdb',room_fdb, 'fdb_list',fdb_list)
  295. try:
  296. iteration_var_list = config.get(room_fdb).get("entry-list").get("iteration-var-list")
  297. except Exception as e:
  298. print('there was an error with the entry-list parameter in the config regarding the fdb ', room_fdb)
  299. if room_fdb in fdb_list and room_fdb not in [key for key in data]:
  300. iterdict = {}
  301. for i in eval(iteration_var_list):
  302. f = open(spider_directory + "/spiders/output/" + room_fdb + str(i) + "entryList.txt")
  303. text = f.read()
  304. dictionary_entry_list = eval(text)
  305. iterdict[i] = dictionary_entry_list
  306. data[room_fdb] = iterdict
  307. for i in eval(iteration_var_list):
  308. try:
  309. print(room_fdb, i)
  310. for key in data[room_fdb][i]:
  311. contains_tag = False
  312. name_data_lower = [word.lower() for word in data[room_fdb][i][key]["name"].split(' ')]
  313. info_data_lower = [word.lower() for word in data[room_fdb][i][key]["info"].split(' ')]
  314. text_data_lower = [word.lower() for word in data[room_fdb][i][key]["text"].split(' ')]
  315. for tag in eval(room_tags):
  316. if tag.lower() in (name_data_lower or info_data_lower or text_data_lower):
  317. contains_tag = True
  318. if contains_tag == True:
  319. try:
  320. url = data[room_fdb][i][key]["domain"]
  321. except:
  322. url = data[room_fdb][i][key]["link"]
  323. entry_message = '<' + url + '|' + data[room_fdb][i][key]["name"]+ '>' + '\n' + data[room_fdb][i][key]["info"]
  324. api.send_message(entry_message, room[0])
  325. except Exception as e:
  326. print("probably i was not there in last page, original error is:", e)
  327. if room_to_update == 'NONE':
  328. answer = 'No room was specified, all rooms will get an update based on the data available'
  329. if room_was_found == False and room_to_update != 'NONE':
  330. answer = 'The room you specified does not exist, or there are no rooms configured, try the command without argument -> updaterooms'
  331. if room_was_found == True and room_to_update != 'NONE':
  332. answer = "I am updating the specified room based on the data in fdb-spider/spiders/output.."
  333. if user_input_list[0] == 'printfdbs':
  334. answer = str(fdb_list)
  335. if len(user_input_list) > 1:
  336. answer = 'Die Syntax zum Ausgeben der konfigurierten Förderdatenbanken hat einen Fehler. Versuche es erneut, mit -> printfdbs'
  337. if user_input_list[0] == 'deleteroom':
  338. try:
  339. room_id_del = user_input_list[1]
  340. except Exception as e:
  341. room_id_del = 'NONE'
  342. print(e)
  343. os.remove("rooms/" + room_id_del + ".txt")
  344. anwer = 'deleting the requested room..'
  345. if len(user_input_list) > 2:
  346. answer = 'Die Syntax zum Löschen eines Raumes hat einen Fehler. Versuche es erneut, nach dem Muster -> deleteroom <room_id>'
  347. if user_input_list[0] == 'createroom':
  348. try:
  349. room_name = user_input_list[1]
  350. except Exception as e:
  351. room_name = 'NONE'
  352. print(e)
  353. try:
  354. databases = user_input_list[2]
  355. except Exception as e:
  356. databases = 'NONE'
  357. print(e)
  358. try:
  359. filters = user_input_list[3]
  360. except Exception as e:
  361. filters = 'NONE'
  362. print(e)
  363. if len(user_input_list) > 3:
  364. 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"]'
  365. if room_name == 'NONE' or databases == 'NONE' or filters == 'NONE':
  366. answer = 'Um einen Raum zu erstellen, in dem neueste Einträge geteilt werden, lautet die Syntax: createroom <Raumname> <Liste-Datenbanken> <Liste-Filtertags>'
  367. else:
  368. try:
  369. new_room = api.create_public_room(room_name,
  370. members=[latest_message_user_username],
  371. read_only=True)
  372. new_room_id = new_room['channel']['_id']
  373. room_file = open('rooms/' + new_room_id + '.txt', 'w')
  374. room_file.write( room_name + '§%§%' + databases + '§%§%' + filters)
  375. room_file.close()
  376. answer = 'Der Command wurde übermittelt'
  377. except Exception as e:
  378. print('it was not able to create the room, the original error message is:', e)
  379. answer = 'There was an error creating the room, look up the logs.. the original error was: ' + str(e)
  380. #print('oi', user_input)
  381. api.send_message('Die Anfrage wird bearbeitet..', room_id)
  382. # here comes the code interacting with the spiders output json
  383. #answer = 'the up to date entries are: oi Oi Oi!'
  384. api.send_message(answer, room_id)
  385. time.sleep(1)
  386. api.send_message('Ich bin wieder bereit für Konfigurationsinput : )', room_id)
  387. time.sleep(1)