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.

832 lines
42 KiB

4 months ago
  1. import os
  2. from rocketchat.api import RocketChatAPI
  3. import configparser
  4. import json
  5. import requests
  6. import yaml
  7. import dateutil.parser
  8. import subprocess
  9. config = configparser.ConfigParser()
  10. config.read('config.ini')
  11. botname = config['Chat']['username']
  12. botpassword = config['Chat']['password']
  13. server_url = config['Chat']['URL']
  14. room_id = config['Chat']['room_id']
  15. bot_user_id = config['Chat']['bot_user_id']
  16. spider_directory = config['Spider']['spider_directory']
  17. # here comes the functions to talk to gpt
  18. # For local streaming, the websockets are hosted without ssl - http://
  19. HOST = 'localhost:5000'
  20. URI = f'http://{HOST}/api/v1/chat'
  21. #URI = f'http://{HOST}/api'
  22. # http://192.168.9.197:5000/api/v1/chat
  23. # For reverse-proxied streaming, the remote will likely host with ssl - https://
  24. # URI = 'https://your-uri-here.trycloudflare.com/api/v1/chat'
  25. if __name__ == '__main__':
  26. def name2id(name):
  27. # get all the rooms
  28. import os
  29. room_list = []
  30. rooms = os.listdir('rooms')
  31. for room in rooms:
  32. room_file = open('rooms/' + room, 'r')
  33. room_file_raw = room_file.read()
  34. room_file.close()
  35. room_file_list = room_file_raw.split('§%§%')
  36. room_list.append([room[:-4], room_file_list[0], room_file_list[1], room_file_list[2], room_file_list[3]])
  37. outputid = 'NONE'
  38. for roo in room_list:
  39. if roo[1] == name:
  40. outputid = roo[0]
  41. return str(outputid)
  42. api = RocketChatAPI(settings={'username': botname, 'password': botpassword, 'domain': server_url})
  43. # api.send_message('Ciao, I am the fdb-spider', room_id)
  44. #myinfo = api.get_my_info()
  45. #room_history = api.get_private_room_history(room_id)
  46. #print(room_history['messages'][0]['msg'])
  47. # print(myinfo)
  48. rooms = api.get_private_rooms()
  49. print('blubidab oioioi', rooms)
  50. # api.send_message('Ole', room_id)
  51. n = 0
  52. import time
  53. import schedule
  54. #change to False here, if you want to have the update run at server start
  55. already_updated = True
  56. start = True
  57. import datetime
  58. from datetime import timedelta
  59. while True:
  60. time.sleep(3)
  61. #already_updated = True
  62. now = datetime.datetime.now()
  63. current_hour = now.strftime("%H")
  64. #print(current_hour)
  65. # run variable update and creation at start
  66. if start == True:
  67. room_list = []
  68. rooms = os.listdir('rooms')
  69. for room in rooms:
  70. room_file = open('rooms/' + room, 'r')
  71. room_file_raw = room_file.read()
  72. room_file.close()
  73. room_file_list = room_file_raw.split('§%§%')
  74. #print(room_list)
  75. #print(room)
  76. #print(room_file_list)
  77. room_list.append([room[:-4], room_file_list[0], room_file_list[1], room_file_list[2]])
  78. with open(spider_directory + '/spiders/config.yaml' , "r") as stream:
  79. try:
  80. config = yaml.safe_load(stream)
  81. except yaml.YAMLError as exc:
  82. print(exc)
  83. fdb_list = []
  84. #print(config)
  85. for key in config:
  86. fdb_list.append(key)
  87. start = False
  88. if int(current_hour) > 11:
  89. aftersix = True
  90. if int(current_hour) <= 11:
  91. aftersix = False
  92. already_updated = False
  93. if aftersix == True and already_updated == False and aftersix == False:
  94. room_list = []
  95. rooms = os.listdir('rooms')
  96. for room in rooms:
  97. room_file = open('rooms/' + room, 'r')
  98. room_file_raw = room_file.read()
  99. room_file.close()
  100. room_file_list = room_file_raw.split('§%§%')
  101. room_list.append([room[:-4], room_file_list[0], room_file_list[1], room_file_list[2], room_file_list[3]])
  102. with open(spider_directory + '/spiders/config.yaml' , "r") as stream:
  103. try:
  104. config = yaml.safe_load(stream)
  105. except yaml.YAMLError as exc:
  106. print(exc)
  107. fdb_list = []
  108. #print(config)
  109. for key in config:
  110. fdb_list.append(key)
  111. data = dict({})
  112. for room in room_list:
  113. #print(room[0])
  114. room_fdbs = room[2]
  115. room_tags = room[3]
  116. room_deadline_days = room[4]
  117. #subprocess.run(["python", spider_directory + 'main.py', fdbs])
  118. room_history_list = []
  119. try:
  120. room_history_file = open('roomhistories/' + room[0] + '.txt', 'x')
  121. room_history_file.close()
  122. except:
  123. print('reading from roomhistory')
  124. with open('roomhistories/' + room[0] + '.txt') as room_history_file:
  125. lines = room_history_file.readlines()
  126. for line in lines:
  127. room_history_list.append(line)
  128. #date = datetime.datetime.now() - timedelta(days=3)
  129. #room_history = api.get_room_history(room[0], oldest=date, latest=datetime.datetime.now())
  130. for room_fdb in eval(room_fdbs):
  131. #print('room_fdb',room_fdb, 'fdb_list',fdb_list)
  132. try:
  133. iteration_var_list = config.get(room_fdb).get("entry-list").get("iteration-var-list")
  134. except Exception as e:
  135. print('there was an error with the entry-list parameter in the config regarding the fdb ', room_fdb)
  136. if room_fdb in fdb_list and room_fdb not in [key for key in data]:
  137. iterdict = {}
  138. for i in eval(iteration_var_list):
  139. f = open(spider_directory + "/spiders/output/" + room_fdb + str(i) + "entryList.txt")
  140. text = f.read()
  141. dictionary_entry_list = eval(text)
  142. iterdict[i] = dictionary_entry_list
  143. data[room_fdb] = iterdict
  144. for i in eval(iteration_var_list):
  145. try:
  146. #print(room_fdb, i)
  147. for key in data[room_fdb][i]:
  148. contains_tag = False
  149. period_data_formatted = dateutil.parser.parse(data[room_fdb][i][key]["period"])
  150. name_data_lower = [word.lower() for word in data[room_fdb][i][key]["name"].split(' ')]
  151. info_data_lower = [word.lower() for word in data[room_fdb][i][key]["info"].split(' ')]
  152. try:
  153. text_data_lower = [word.lower() for word in data[room_fdb][i][key]["text"].split(' ')]
  154. except Exception as e:
  155. print(e)
  156. text_data_lower = ['none']
  157. tag_list = []
  158. for tag in eval(room_tags):
  159. if '_' in tag:
  160. ntags = tag.split('_')
  161. ntags_length = len(ntags)
  162. ntag_count = 0
  163. for nword in name_data_lower:
  164. for ntag in ntags:
  165. if ntag.lower() in nword:
  166. ntag_count += 1
  167. if ntag_count == ntags_length:
  168. contains_tag = True
  169. tag_list.append(tag + ' in name')
  170. ntag_count = 0
  171. for iword in info_data_lower:
  172. for ntag in ntags:
  173. if ntag.lower() in iword:
  174. ntag_count += 1
  175. if ntag_count == ntags_length:
  176. contains_tag = True
  177. tag_list.append(tag + ' in info')
  178. ntag_count = 0
  179. for tword in text_data_lower:
  180. ntag_count = 0
  181. for ntag in ntags:
  182. if ntag.lower() in tword:
  183. ntag_count += 1
  184. if ntag_count == ntags_length:
  185. contains_tag = True
  186. tag_list.append(tag + ' in text')
  187. else:
  188. for nword in name_data_lower:
  189. if tag.lower() in nword:
  190. contains_tag = True
  191. tag_list.append(tag + ' in name')
  192. for iword in info_data_lower:
  193. if tag.lower() in iword:
  194. contains_tag = True
  195. tag_list.append(tag + ' in info')
  196. for tword in text_data_lower:
  197. if tag.lower() in tword:
  198. contains_tag = True
  199. tag_list.append(tag + ' in text')
  200. if contains_tag == True:
  201. try:
  202. url = data[room_fdb][i][key]["domain"]
  203. except:
  204. url = data[room_fdb][i][key]["link"]
  205. entry_message = '<' + url + '|' + data[room_fdb][i][key]["name"]+ '>' + '\n' + data[room_fdb][i][key]["info"] + '\n' + 'The period of the entry is:' + str(period_data_formatted) + '\n' + str(tag_list)
  206. entry_message_identifier = '<' + url + '|' + data[room_fdb][i][key]["name"]+ '>'
  207. message_was_already_there = False
  208. for message in room_history_list:
  209. #print('message that gets checked with identifier ', message[:-1])
  210. #print('the entry message identifier is:', entry_message_identifier)
  211. if url in message[:-1] or data[room_fdb][i][key]["name"] in message:
  212. message_was_already_there = True
  213. now = datetime.datetime.now()
  214. now_formatted = dateutil.parser.parse(str(now))
  215. delta = period_data_formatted - now_formatted
  216. if message_was_already_there == False and delta.days < int(room_deadline_days):
  217. #print('went into already there false')
  218. api.send_message(entry_message, room[0])
  219. #print('before writing')
  220. try:
  221. room_file = open('roomhistories/' + room[0] + '.txt', 'x')
  222. room_file.close()
  223. except:
  224. print('appending to roomhistory')
  225. room_file = open('roomhistories/' + room[0] + '.txt', 'a')
  226. room_file.write(entry_message_identifier + '\n')
  227. room_file.close()
  228. print('after writing')
  229. except Exception as e:
  230. print("probably i was not there in last page, original error is:", e)
  231. #print('data',data['giz'][2],'data')
  232. already_updated = True
  233. # if datestime.split over etc and updated= true etc
  234. n += 1
  235. if n%100 == 0:
  236. print(n)
  237. try:
  238. #print('getting the room history')
  239. date = datetime.datetime.now() - timedelta(days=3)
  240. room_history = api.get_private_room_history(room_id, oldest=date)
  241. except Exception as e:
  242. time.sleep(10)
  243. api = RocketChatAPI(settings={'username': botname, 'password': botpassword, 'domain': server_url})
  244. time.sleep(5)
  245. room_history = api.get_private_room_history(room_id, oldest=date)
  246. print('got a connection error, original message is:',e)
  247. messages_list = []
  248. for message in room_history['messages']:
  249. messages_list.append(message)
  250. if len(messages_list) >= 1:
  251. #print('blub', messages_list)
  252. latest_message_user_id = messages_list[0]['u']['_id']
  253. latest_message_user_username = messages_list[0]['u']['username']
  254. latest_message = messages_list[0]['msg']
  255. latest_message_id = messages_list[0]['_id']
  256. new_message_file = open('new_message_file.txt', 'r')
  257. new_message = new_message_file.read()
  258. new_message_file.close()
  259. new_message_list = new_message.split('§%§%')
  260. #print(latest_message, new_message_list[0])
  261. if new_message_list[0] != latest_message and new_message_list[1] != latest_message_id and latest_message_user_id != bot_user_id:
  262. answer = 'Ich habe kein Kommando erhalten.'
  263. new_message_file = open('new_message_file.txt', 'w')
  264. new_message_file.write(latest_message + '§%§%' + latest_message_id)
  265. new_message_file.close()
  266. user_input = latest_message
  267. user_input_list = user_input.split(' ')
  268. if user_input_list[0] == 'addtags':
  269. try:
  270. room_name_add_tags = user_input_list[1]
  271. room_id_add_tags = name2id(room_name_add_tags)
  272. #print(room_id_add_tags)
  273. except:
  274. room_id_add_tags = 'NONE'
  275. try:
  276. new_tags = user_input_list[2]
  277. except:
  278. new_tags = 'NONE'
  279. try:
  280. thirdarg = user_input_list[3]
  281. except:
  282. thirdarg = 'NONE'
  283. if room_id_add_tags != 'NONE' and len(new_tags) >= 1 and thirdarg == 'NONE':
  284. try:
  285. room_file_add_tags = open('rooms/' + room_id_add_tags + '.txt', 'r')
  286. room_info_raw = room_file_add_tags.read()
  287. room_file_add_tags.close()
  288. room_info = room_info_raw.split('§%§%')
  289. tag_list = eval(room_info[-2])
  290. for tag in eval(user_input_list[2]):
  291. if tag not in tag_list:
  292. tag_list.append(tag)
  293. room_file_add_tags = open('rooms/' + room_id_add_tags + '.txt', 'w')
  294. room_file_add_tags.write( str(room_info[0]) + '§%§%' + str(room_info[1]) + '§%§%' + str(tag_list) + '§%§%' + str(room_info[-1]))
  295. room_file_add_tags.close()
  296. answer = 'the updated tag list is' + str(tag_list)
  297. except Exception as e:
  298. print('error opening, original error is:', e)
  299. answer = "The room_id to update the tags was not found"
  300. if user_input_list[0] == 'printtags':
  301. try:
  302. room_name_add_tags = user_input_list[1]
  303. room_id_to_print_tags = name2id(room_name_add_tags)
  304. except:
  305. answer = "after the command printtags, the second argument has to be the room name.. use printrooms and look up the id of the room you want to print the tags"
  306. room_id_to_print_tags = 'NONE'
  307. if room_id_to_print_tags != 'NONE':
  308. try:
  309. room_file = open('rooms/' + room_id_to_print_tags + '.txt', 'r')
  310. room_info_raw = room_file.read()
  311. room_file.close()
  312. room_info = room_info_raw.split('§%§%')
  313. tag_list = eval(room_info[-2])
  314. answer = tag_list
  315. except Exception as e:
  316. print('error opening, original error is:', e)
  317. answer = "The room_id to get the tags was not found"
  318. if user_input_list[0] == 'printcommands':
  319. answer = """
  320. To print all available rooms and their configuration, use
  321. command : `printrooms`
  322. -----------------------------------------------------------------------------------------
  323. To print all available fdbs, use
  324. command : `printfdbs`
  325. -----------------------------------------------------------------------------------------
  326. To update all rooms use
  327. command : `updaterooms all`
  328. -----------------------------------------------------------------------------------------
  329. To update one room use the name of the room from the output of printrooms:
  330. command : `updaterooms <room-name>`
  331. example : `updaterooms test42`
  332. -----------------------------------------------------------------------------------------
  333. To create a room use following command, but be sure to **not have spaces** in your lists,
  334. as a space indicates a new command argument:
  335. command : `createroom <room-name> <list-fdbs> <list-tags> <days-to-deadline>`
  336. example : `createroom room-test-1 ['giz','fdb2'] ['tag1','tag2','tag3'] 7`
  337. -----------------------------------------------------------------------------------------
  338. To delete a room use
  339. command : `deleteroom <room-name>`
  340. example : `deleteroom test42`
  341. -----------------------------------------------------------------------------------------
  342. To print the tags of a room use
  343. command : `printtags <room-name>`
  344. example : `printtags test42`
  345. -----------------------------------------------------------------------------------------
  346. To add tags to the existing tags use
  347. command : `addtags <room-name> <tag-list-without-spaces>`
  348. example : `addtags test42 ['tag1','tag2','tag3']`
  349. """
  350. if user_input_list[0] == 'printrooms':
  351. room_was_found = False
  352. # get all the rooms
  353. import os
  354. room_list = []
  355. rooms = os.listdir('rooms')
  356. for room in rooms:
  357. room_file = open('rooms/' + room, 'r')
  358. room_file_raw = room_file.read()
  359. room_file.close()
  360. room_file_list = room_file_raw.split('§%§%')
  361. room_list.append([room[:-4], room_file_list[0], room_file_list[1], room_file_list[2], room_file_list[3]])
  362. room_list_string = ''
  363. for room in room_list:
  364. room_list_string += str(room) + '\n' + '------------------------------------------------------------------------------' + '\n'
  365. #print(room_list)
  366. answer = room_list_string
  367. if user_input_list[0] == 'updaterooms':
  368. try:
  369. if len(user_input_list[1]) >= 1:
  370. room_name_to_update = user_input_list[1]
  371. room_to_update = name2id(room_name_to_update)
  372. #room_to_update = user_input_list[1]
  373. except Exception as e:
  374. room_to_update = 'NONE'
  375. room_list = []
  376. rooms = os.listdir('rooms')
  377. for room in rooms:
  378. room_file = open('rooms/' + room, 'r')
  379. room_file_raw = room_file.read()
  380. room_file.close()
  381. room_file_list = room_file_raw.split('§%§%')
  382. room_list.append([room[:-4], room_file_list[0], room_file_list[1], room_file_list[2], room_file_list[3]])
  383. with open(spider_directory + '/spiders/config.yaml' , "r") as stream:
  384. try:
  385. config = yaml.safe_load(stream)
  386. except yaml.YAMLError as exc:
  387. print(exc)
  388. fdb_list = []
  389. #print(config)
  390. for key in config:
  391. fdb_list.append(key)
  392. answer = "Ich update die Rooms auf Basis der Daten von heute morgen um 6 Uhr.."
  393. data = dict({})
  394. for room in room_list:
  395. if room[0] == room_to_update or room_to_update == 'all':
  396. room_was_found = True
  397. print('oioioiOI', room[0])
  398. room_fdbs = room[2]
  399. room_tags = room[3]
  400. room_deadline_days = room[4]
  401. # not running get_rooms because disfunctional
  402. #subprocess.run(["python", spider_directory + 'main.py', fdbs])
  403. #myinfo = api.get_room_info(room[0])
  404. #print(myinfo)
  405. #roomid = "'657cbeccebb39dd248d38ec3'"
  406. #roomoioioi = api.get_user_info(bot_user_id)
  407. #print(roomoioioi)
  408. #room_history_updateroom = api.get_private_room_history(room[0])
  409. # reading from txt state history instead
  410. try:
  411. room_history_file = open('roomhistories/' + room[0] + '.txt', 'x')
  412. room_history_file.close()
  413. except:
  414. print('reading from roomhistory which is already there')
  415. room_history_list = []
  416. with open('roomhistories/' + room[0] + '.txt') as room_history_file:
  417. lines = room_history_file.readlines()
  418. for line in lines:
  419. room_history_list.append(line)
  420. #
  421. #for message in room_history_raw:
  422. # print(message)
  423. for room_fdb in eval(room_fdbs):
  424. print('room_fdb',room_fdb, 'fdb_list',fdb_list)
  425. try:
  426. iteration_var_list = config.get(room_fdb).get("entry-list").get("iteration-var-list")
  427. except Exception as e:
  428. print('there was an error with the entry-list parameter in the config regarding the fdb ', room_fdb)
  429. if room_fdb in fdb_list and room_fdb not in [key for key in data]:
  430. iterdict = {}
  431. for i in eval(iteration_var_list):
  432. f = open(spider_directory + "/spiders/output/" + room_fdb + str(i) + "entryList.txt")
  433. text = f.read()
  434. dictionary_entry_list = eval(text)
  435. iterdict[i] = dictionary_entry_list
  436. data[room_fdb] = iterdict
  437. for i in eval(iteration_var_list):
  438. try:
  439. print('roomfdb and i', room_fdb, i)
  440. #print('oioioioioiOIOIOI')
  441. for key in data[room_fdb][i]:
  442. #print('the fdb', roomfdb, ' is getting searched')
  443. contains_tag = False
  444. try:
  445. period_data_formatted = dateutil.parser.parse(data[room_fdb][i][key]["period"])
  446. except Exception as e:
  447. period_data_formatted = 'NONE'
  448. #print('getting the period did not work for', room_fdb, i, key, ' ori err is:', e)
  449. name_data_lower = [word.lower() for word in data[room_fdb][i][key]["name"].split(' ')]
  450. info_data_lower = [word.lower() for word in data[room_fdb][i][key]["info"].split(' ')]
  451. try:
  452. text_data_lower = [word.lower() for word in data[room_fdb][i][key]["text"].split(' ')]
  453. except Exception as e:
  454. #print(e, 'there was an exception converting to lowercase')
  455. #try:
  456. # print(data[room_fdb][i][key]["text"].split(' '))
  457. #except Exception as e:
  458. # print(e, 'even printing the data was not possible')
  459. text_data_lower = ['NONE']
  460. #print('got until ONE')
  461. tag_list = []
  462. #print(str(name_data_lower) + ' is getting searched..')
  463. for tag in eval(room_tags):
  464. #print('the tag ' + tag + ' is getting searched')
  465. if '_' in tag:
  466. ntags = tag.split('_')
  467. ntags_length = len(ntags)
  468. #print(ntags)
  469. ntag_count = 0
  470. for ntag in ntags:
  471. #print('searching for ntag: ', ntag)
  472. for nword in name_data_lower:
  473. if ntag.lower() in nword:
  474. if nword != '':
  475. ntag_count += 1
  476. #print(ntag, ' ntag was found in name')
  477. break
  478. #print('ntag count is ', ntag_count, 'ntag_length is ', ntags_length)
  479. if ntag_count == ntags_length:
  480. contains_tag = True
  481. tag_list.append('_'.join(ntags) + ' in name')
  482. ntag_count = 0
  483. for ntag in ntags:
  484. for iword in info_data_lower:
  485. if ntag.lower() in iword:
  486. if iword != '':
  487. ntag_count += 1
  488. #print(ntag, ' ntag was found in info')
  489. break
  490. if ntag_count == ntags_length:
  491. contains_tag = True
  492. tag_list.append('_'.join(ntags) + ' in info')
  493. ntag_count = 0
  494. for ntag in ntags:
  495. for tword in text_data_lower:
  496. if ntag.lower() in tword:
  497. if tword != '':
  498. ntag_count += 1
  499. break
  500. if ntag_count == ntags_length:
  501. contains_tag = True
  502. tag_list.append('_'.join(ntags) + ' in text')
  503. else:
  504. #print('------------------')
  505. #print(name_data_lower, info_data_lower, text_data_lower, room[0], room_tags)
  506. nword_contains_tag = False
  507. for nword in name_data_lower:
  508. #print(tag.lower(), nword)
  509. if tag.lower() in nword:
  510. if tag == 'senegal':
  511. print(tag.lower(), nword)
  512. #print('goooot heeeere')
  513. #print(nword)
  514. if nword != '':
  515. #print('goot behind nword check')
  516. nword_contains_tag = True
  517. if nword_contains_tag == True:
  518. contains_tag = True
  519. tag_list.append(tag + ' in name')
  520. iword_contains_tag = False
  521. for iword in info_data_lower:
  522. if tag.lower() in iword:
  523. if iword != '':
  524. iword_contains_tag = True
  525. #print('oioiOIOIOIoioioiOIOIword', iword)
  526. if iword_contains_tag == True:
  527. contains_tag = True
  528. tag_list.append(tag + ' in info')
  529. tword_contains_tag = False
  530. for tword in text_data_lower:
  531. if tag.lower() in tword:
  532. if tword != '':
  533. tword_contains_tag = True
  534. if tword_contains_tag == True:
  535. contains_tag = True
  536. tag_list.append(tag + ' in text')
  537. #print('got until THREE')
  538. if contains_tag == True:
  539. print('------------------> the tag list is', str(tag_list))
  540. #print(name_data_lower, info_data_lower, text_data_lower)
  541. try:
  542. url = data[room_fdb][i][key]["domain"]
  543. except:
  544. url = data[room_fdb][i][key]["link"]
  545. #print('101110001101010010010101000111')
  546. entry_message = '<' + url + '|' + data[room_fdb][i][key]["name"]+ '>' + '\n' + data[room_fdb][i][key]["info"] + '\n' + 'The period of the entry is:' + str(period_data_formatted) + '\n' + str(tag_list)
  547. entry_message_identifier = '<' + url + '|' + data[room_fdb][i][key]["name"]+ '>'
  548. print(entry_message , ' is the entrymessage that will be sent to the room')
  549. message_was_already_there = False
  550. for message in room_history_list:
  551. #print('message', message[:-1], 'identifier', entry_message_identifier)
  552. #if url in message[:-1] or data[room_fdb][i][key]["name"] in message:
  553. if data[room_fdb][i][key]["name"] in message:
  554. message_was_already_there = True
  555. print('message_was_already_there set to true')
  556. now = datetime.datetime.now()
  557. now_formatted = dateutil.parser.parse(str(now))
  558. try:
  559. delta = period_data_formatted - now_formatted
  560. days_to_check = delta.days
  561. except Exception as e:
  562. days_to_check = int(room_deadline_days) - 1
  563. print('calc of delta did now work, original error is:', e)
  564. print('delta', days_to_check, int(room_deadline_days))
  565. if message_was_already_there == False and int(days_to_check) > int(room_deadline_days):
  566. api.send_message(entry_message, room[0])
  567. try:
  568. room_file = open('roomhistories/' + room[0] + '.txt', 'x')
  569. room_file.close()
  570. except:
  571. print('appending to roomhistory')
  572. room_file = open('roomhistories/' + room[0] + '.txt', 'a')
  573. room_file.write(entry_message_identifier + '\n')
  574. room_file.close()
  575. except Exception as e:
  576. print("probably i was not there in last page, original error is:", e)
  577. if room_to_update == 'all':
  578. answer = 'I am updating all rooms based on the data in fdb-spider/spiders/output..'
  579. if room_was_found == False or room_to_update == 'NONE':
  580. answer = 'The room you specified does not exist, or there are no rooms configured, try printcommands or contact the administrator'
  581. if room_was_found == True and room_to_update != 'NONE':
  582. answer = "I am updating the specified room based on the data in fdb-spider/spiders/output.."
  583. if user_input_list[0] == 'printfdbs':
  584. answer = str(fdb_list)
  585. if len(user_input_list) > 1:
  586. answer = 'Die Syntax zum Ausgeben der konfigurierten Förderdatenbanken hat einen Fehler. Versuche es erneut, mit -> printfdbs'
  587. if user_input_list[0] == 'deleteroom':
  588. try:
  589. room_name_del = user_input_list[1]
  590. room_id_del = name2id(room_name_del)
  591. #room_id_del = user_input_list[1]
  592. except Exception as e:
  593. room_id_del = 'NONE'
  594. print(e)
  595. os.remove("rooms/" + room_id_del + ".txt")
  596. answer = 'deleting the requested room..'
  597. if len(user_input_list) > 2:
  598. answer = 'Die Syntax zum Löschen eines Raumes hat einen Fehler. Versuche es erneut, nach dem Muster -> deleteroom <room_id>'
  599. if user_input_list[0] == 'createroom':
  600. try:
  601. room_name_from_input = user_input_list[1]
  602. room_id_check = name2id(room_name_from_input)
  603. if room_id_check == 'NONE':
  604. room_name = user_input_list[1]
  605. else:
  606. if len(room_id_check) > 8:
  607. room_name = 'NONE'
  608. except Exception as e:
  609. room_name = 'NONE'
  610. print(e)
  611. try:
  612. databases = user_input_list[2]
  613. except Exception as e:
  614. databases = 'NONE'
  615. print(e)
  616. try:
  617. filters = user_input_list[3]
  618. except Exception as e:
  619. filters = 'NONE'
  620. print(e)
  621. try:
  622. days_to_deadline = user_input_list[4]
  623. except Exception as e:
  624. days_to_deadline = 'NONE'
  625. print(e)
  626. if len(user_input_list) > 4:
  627. 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"]'
  628. if room_name == 'NONE' or databases == 'NONE' or filters == 'NONE' or days_to_deadline == 'NONE':
  629. answer = 'Um einen Raum zu erstellen, in dem neueste Einträge geteilt werden, lautet die Syntax: createroom <Raumname> <Liste-Datenbanken> <Liste-Filtertags> <Tage-bis-zur-Frist> \n Dabei darf der Name noch nicht vergeben sein. Im Zweifel einfach nochmal printrooms ausgeben lassen..'
  630. else:
  631. try:
  632. new_room = api.create_public_room(room_name,
  633. members=[latest_message_user_username],
  634. read_only=False)
  635. new_room_id = new_room['channel']['_id']
  636. room_file = open('rooms/' + new_room_id + '.txt', 'w')
  637. room_file.write( room_name + '§%§%' + databases + '§%§%' + filters + '§%§%' + days_to_deadline)
  638. room_file.close()
  639. answer = 'Der Command wurde übermittelt'
  640. except Exception as e:
  641. print('it was not able to create the room, the original error message is:', e)
  642. answer = 'There was an error creating the room, look up the logs.. the original error was: ' + str(e)
  643. #print('oi', user_input)
  644. api.send_message('Die Anfrage wird bearbeitet..', room_id)
  645. # here comes the code interacting with the spiders output json
  646. #answer = 'the up to date entries are: oi Oi Oi!'
  647. api.send_message(answer, room_id)
  648. time.sleep(1)
  649. api.send_message('Ich bin wieder bereit für Konfigurationsinput : )', room_id)
  650. time.sleep(1)