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.

870 lines
44 KiB

2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 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] == 'addfdbs':
  301. print('oi')
  302. print(user_input_list)
  303. try:
  304. room_name_add_fdbs = user_input_list[1]
  305. print(room_name_add_fdbs)
  306. room_id_add_fdbs = name2id(room_name_add_fdbs)
  307. print(room_name_add_fdbs)
  308. print(room_id_add_fdbs)
  309. except Exception as e:
  310. room_id_add_fdbs = 'NONE'
  311. print(e, 'did not work')
  312. try:
  313. new_fdbs = user_input_list[2]
  314. except:
  315. new_fdbs = 'NONE'
  316. try:
  317. thirdarg = user_input_list[3]
  318. except:
  319. thirdarg = 'NONE'
  320. if room_id_add_fdbs != 'NONE' and len(new_fdbs) >= 1 and thirdarg == 'NONE':
  321. try:
  322. room_file_add_fdbs = open('rooms/' + room_id_add_fdbs + '.txt', 'r')
  323. room_info_raw = room_file_add_fdbs.read()
  324. room_file_add_fdbs.close()
  325. room_info = room_info_raw.split('§%§%')
  326. fdb_list = eval(room_info[-3])
  327. for fdb in eval(user_input_list[2]):
  328. if fdb not in fdb_list:
  329. fdb_list.append(fdb)
  330. room_file_add_fdbs = open('rooms/' + room_id_add_fdbs + '.txt', 'w')
  331. room_file_add_fdbs.write( str(room_info[0]) + '§%§%' + str(fdb_list) + '§%§%' + str(room_info[2]) + '§%§%' + str(room_info[-1]))
  332. room_file_add_fdbs.close()
  333. answer = 'the updated fdb list is' + str(fdb_list)
  334. except Exception as e:
  335. print('error opening, original error is:', e)
  336. answer = "The room_id to update the fdbs was not found"
  337. if user_input_list[0] == 'printtags':
  338. try:
  339. room_name_add_tags = user_input_list[1]
  340. room_id_to_print_tags = name2id(room_name_add_tags)
  341. except:
  342. 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"
  343. room_id_to_print_tags = 'NONE'
  344. if room_id_to_print_tags != 'NONE':
  345. try:
  346. room_file = open('rooms/' + room_id_to_print_tags + '.txt', 'r')
  347. room_info_raw = room_file.read()
  348. room_file.close()
  349. room_info = room_info_raw.split('§%§%')
  350. tag_list = eval(room_info[-2])
  351. answer = tag_list
  352. except Exception as e:
  353. print('error opening, original error is:', e)
  354. answer = "The room_id to get the tags was not found"
  355. if user_input_list[0] == 'printcommands':
  356. answer = """
  357. To print all available rooms and their configuration, use
  358. command : `printrooms`
  359. -----------------------------------------------------------------------------------------
  360. To print all available fdbs, use
  361. command : `printfdbs`
  362. -----------------------------------------------------------------------------------------
  363. To update all rooms use
  364. command : `updaterooms all`
  365. -----------------------------------------------------------------------------------------
  366. To update one room use the name of the room from the output of printrooms:
  367. command : `updaterooms <room-name>`
  368. example : `updaterooms test42`
  369. -----------------------------------------------------------------------------------------
  370. To create a room use following command, but be sure to **not have spaces** in your lists,
  371. as a space indicates a new command argument:
  372. command : `createroom <room-name> <list-fdbs> <list-tags> <days-to-deadline>`
  373. example : `createroom room-test-1 ['giz','fdb2'] ['tag1','tag2','tag3'] 7`
  374. -----------------------------------------------------------------------------------------
  375. To delete a room use
  376. command : `deleteroom <room-name>`
  377. example : `deleteroom test42`
  378. -----------------------------------------------------------------------------------------
  379. To print the tags of a room use
  380. command : `printtags <room-name>`
  381. example : `printtags test42`
  382. -----------------------------------------------------------------------------------------
  383. To add tags to the existing tags use
  384. command : `addtags <room-name> <tag-list-without-spaces>`
  385. example : `addtags test42 ['tag1','tag2','tag3']`
  386. """
  387. if user_input_list[0] == 'printrooms':
  388. room_was_found = False
  389. # get all the rooms
  390. import os
  391. room_list = []
  392. rooms = os.listdir('rooms')
  393. for room in rooms:
  394. room_file = open('rooms/' + room, 'r')
  395. room_file_raw = room_file.read()
  396. room_file.close()
  397. room_file_list = room_file_raw.split('§%§%')
  398. room_list.append([room[:-4], room_file_list[0], room_file_list[1], room_file_list[2], room_file_list[3]])
  399. room_list_string = ''
  400. for room in room_list:
  401. room_list_string += str(room) + '\n' + '------------------------------------------------------------------------------' + '\n'
  402. #print(room_list)
  403. answer = room_list_string
  404. if user_input_list[0] == 'updaterooms':
  405. try:
  406. if len(user_input_list[1]) >= 1:
  407. room_name_to_update = user_input_list[1]
  408. room_to_update = name2id(room_name_to_update)
  409. #room_to_update = user_input_list[1]
  410. except Exception as e:
  411. room_to_update = 'NONE'
  412. room_list = []
  413. rooms = os.listdir('rooms')
  414. for room in rooms:
  415. room_file = open('rooms/' + room, 'r')
  416. room_file_raw = room_file.read()
  417. room_file.close()
  418. room_file_list = room_file_raw.split('§%§%')
  419. room_list.append([room[:-4], room_file_list[0], room_file_list[1], room_file_list[2], room_file_list[3]])
  420. with open(spider_directory + '/spiders/config.yaml' , "r") as stream:
  421. try:
  422. config = yaml.safe_load(stream)
  423. except yaml.YAMLError as exc:
  424. print(exc)
  425. fdb_list = []
  426. #print(config)
  427. for key in config:
  428. fdb_list.append(key)
  429. answer = "Ich update die Rooms auf Basis der Daten von heute morgen um 6 Uhr.."
  430. data = dict({})
  431. for room in room_list:
  432. if room[0] == room_to_update or room_to_update == 'all':
  433. room_was_found = True
  434. print('oioioiOI', room[0])
  435. room_fdbs = room[2]
  436. room_tags = room[3]
  437. room_deadline_days = room[4]
  438. # not running get_rooms because disfunctional
  439. #subprocess.run(["python", spider_directory + 'main.py', fdbs])
  440. #myinfo = api.get_room_info(room[0])
  441. #print(myinfo)
  442. #roomid = "'657cbeccebb39dd248d38ec3'"
  443. #roomoioioi = api.get_user_info(bot_user_id)
  444. #print(roomoioioi)
  445. #room_history_updateroom = api.get_private_room_history(room[0])
  446. # reading from txt state history instead
  447. try:
  448. room_history_file = open('roomhistories/' + room[0] + '.txt', 'x')
  449. room_history_file.close()
  450. except:
  451. print('reading from roomhistory which is already there')
  452. room_history_list = []
  453. with open('roomhistories/' + room[0] + '.txt') as room_history_file:
  454. lines = room_history_file.readlines()
  455. for line in lines:
  456. room_history_list.append(line)
  457. #
  458. #for message in room_history_raw:
  459. # print(message)
  460. for room_fdb in eval(room_fdbs):
  461. print('room_fdb',room_fdb, 'fdb_list',fdb_list)
  462. try:
  463. iteration_var_list = config.get(room_fdb).get("entry-list").get("iteration-var-list")
  464. except Exception as e:
  465. print('there was an error with the entry-list parameter in the config regarding the fdb ', room_fdb)
  466. if room_fdb in fdb_list and room_fdb not in [key for key in data]:
  467. iterdict = {}
  468. for i in eval(iteration_var_list):
  469. f = open(spider_directory + "/spiders/output/" + room_fdb + str(i) + "entryList.txt")
  470. text = f.read()
  471. dictionary_entry_list = eval(text)
  472. iterdict[i] = dictionary_entry_list
  473. data[room_fdb] = iterdict
  474. for i in eval(iteration_var_list):
  475. try:
  476. print('roomfdb and i', room_fdb, i)
  477. #print('oioioioioiOIOIOI')
  478. for key in data[room_fdb][i]:
  479. #print('the fdb', roomfdb, ' is getting searched')
  480. contains_tag = False
  481. try:
  482. period_data_formatted = dateutil.parser.parse(data[room_fdb][i][key]["period"])
  483. except Exception as e:
  484. period_data_formatted = 'NONE'
  485. #print('getting the period did not work for', room_fdb, i, key, ' ori err is:', e)
  486. name_data_lower = [word.lower() for word in data[room_fdb][i][key]["name"].split(' ')]
  487. info_data_lower = [word.lower() for word in data[room_fdb][i][key]["info"].split(' ')]
  488. try:
  489. text_data_lower = [word.lower() for word in data[room_fdb][i][key]["text"].split(' ')]
  490. except Exception as e:
  491. #print(e, 'there was an exception converting to lowercase')
  492. #try:
  493. # print(data[room_fdb][i][key]["text"].split(' '))
  494. #except Exception as e:
  495. # print(e, 'even printing the data was not possible')
  496. text_data_lower = ['NONE']
  497. #print('got until ONE')
  498. tag_list = []
  499. #print(str(name_data_lower) + ' is getting searched..')
  500. for tag in eval(room_tags):
  501. #print('the tag ' + tag + ' is getting searched')
  502. if '_' in tag:
  503. ntags = tag.split('_')
  504. ntags_length = len(ntags)
  505. #print(ntags)
  506. ntag_count = 0
  507. for ntag in ntags:
  508. #print('searching for ntag: ', ntag)
  509. for nword in name_data_lower:
  510. if ntag.lower() in nword:
  511. if nword != '':
  512. ntag_count += 1
  513. #print(ntag, ' ntag was found in name')
  514. break
  515. #print('ntag count is ', ntag_count, 'ntag_length is ', ntags_length)
  516. if ntag_count == ntags_length:
  517. contains_tag = True
  518. tag_list.append('_'.join(ntags) + ' in name')
  519. ntag_count = 0
  520. for ntag in ntags:
  521. for iword in info_data_lower:
  522. if ntag.lower() in iword:
  523. if iword != '':
  524. ntag_count += 1
  525. #print(ntag, ' ntag was found in info')
  526. break
  527. if ntag_count == ntags_length:
  528. contains_tag = True
  529. tag_list.append('_'.join(ntags) + ' in info')
  530. ntag_count = 0
  531. for ntag in ntags:
  532. for tword in text_data_lower:
  533. if ntag.lower() in tword:
  534. if tword != '':
  535. ntag_count += 1
  536. break
  537. if ntag_count == ntags_length:
  538. contains_tag = True
  539. tag_list.append('_'.join(ntags) + ' in text')
  540. else:
  541. #print('------------------')
  542. #print(name_data_lower, info_data_lower, text_data_lower, room[0], room_tags)
  543. nword_contains_tag = False
  544. for nword in name_data_lower:
  545. #print(tag.lower(), nword)
  546. if tag.lower() in nword:
  547. if tag == 'senegal':
  548. print(tag.lower(), nword)
  549. #print('goooot heeeere')
  550. #print(nword)
  551. if nword != '':
  552. #print('goot behind nword check')
  553. nword_contains_tag = True
  554. if nword_contains_tag == True:
  555. contains_tag = True
  556. tag_list.append(tag + ' in name')
  557. iword_contains_tag = False
  558. for iword in info_data_lower:
  559. if tag.lower() in iword:
  560. if iword != '':
  561. iword_contains_tag = True
  562. #print('oioiOIOIOIoioioiOIOIword', iword)
  563. if iword_contains_tag == True:
  564. contains_tag = True
  565. tag_list.append(tag + ' in info')
  566. tword_contains_tag = False
  567. for tword in text_data_lower:
  568. if tag.lower() in tword:
  569. if tword != '':
  570. tword_contains_tag = True
  571. if tword_contains_tag == True:
  572. contains_tag = True
  573. tag_list.append(tag + ' in text')
  574. #print('got until THREE')
  575. if contains_tag == True:
  576. print('------------------> the tag list is', str(tag_list))
  577. #print(name_data_lower, info_data_lower, text_data_lower)
  578. try:
  579. url = data[room_fdb][i][key]["domain"]
  580. except:
  581. url = data[room_fdb][i][key]["link"]
  582. #print('101110001101010010010101000111')
  583. 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)
  584. entry_message_identifier = '<' + url + '|' + data[room_fdb][i][key]["name"]+ '>'
  585. print(entry_message , ' is the entrymessage that will be sent to the room')
  586. message_was_already_there = False
  587. for message in room_history_list:
  588. #print('message', message[:-1], 'identifier', entry_message_identifier)
  589. #if url in message[:-1] or data[room_fdb][i][key]["name"] in message:
  590. if data[room_fdb][i][key]["name"] in message:
  591. message_was_already_there = True
  592. print('message_was_already_there set to true')
  593. now = datetime.datetime.now()
  594. now_formatted = dateutil.parser.parse(str(now))
  595. try:
  596. delta = period_data_formatted - now_formatted
  597. days_to_check = delta.days
  598. except Exception as e:
  599. days_to_check = int(room_deadline_days) - 1
  600. print('calc of delta did now work, original error is:', e)
  601. print('delta', days_to_check, int(room_deadline_days))
  602. if message_was_already_there == False and int(days_to_check) > int(room_deadline_days):
  603. api.send_message(entry_message, room[0])
  604. try:
  605. room_file = open('roomhistories/' + room[0] + '.txt', 'x')
  606. room_file.close()
  607. except:
  608. print('appending to roomhistory')
  609. room_file = open('roomhistories/' + room[0] + '.txt', 'a')
  610. room_file.write(entry_message_identifier + '\n')
  611. room_file.close()
  612. except Exception as e:
  613. print("probably i was not there in last page, original error is:", e)
  614. if room_to_update == 'all':
  615. answer = 'I am updating all rooms based on the data in fdb-spider/spiders/output..'
  616. if room_was_found == False or room_to_update == 'NONE':
  617. answer = 'The room you specified does not exist, or there are no rooms configured, try printcommands or contact the administrator'
  618. if room_was_found == True and room_to_update != 'NONE':
  619. answer = "I am updating the specified room based on the data in fdb-spider/spiders/output.."
  620. if user_input_list[0] == 'printfdbs':
  621. answer = str(fdb_list)
  622. if len(user_input_list) > 1:
  623. answer = 'Die Syntax zum Ausgeben der konfigurierten Förderdatenbanken hat einen Fehler. Versuche es erneut, mit -> printfdbs'
  624. if user_input_list[0] == 'deleteroom':
  625. try:
  626. room_name_del = user_input_list[1]
  627. room_id_del = name2id(room_name_del)
  628. #room_id_del = user_input_list[1]
  629. except Exception as e:
  630. room_id_del = 'NONE'
  631. print(e)
  632. os.remove("rooms/" + room_id_del + ".txt")
  633. answer = 'deleting the requested room..'
  634. if len(user_input_list) > 2:
  635. answer = 'Die Syntax zum Löschen eines Raumes hat einen Fehler. Versuche es erneut, nach dem Muster -> deleteroom <room_id>'
  636. if user_input_list[0] == 'createroom':
  637. try:
  638. room_name_from_input = user_input_list[1]
  639. room_id_check = name2id(room_name_from_input)
  640. if room_id_check == 'NONE':
  641. room_name = user_input_list[1]
  642. else:
  643. if len(room_id_check) > 8:
  644. room_name = 'NONE'
  645. except Exception as e:
  646. room_name = 'NONE'
  647. print(e)
  648. try:
  649. databases = user_input_list[2]
  650. except Exception as e:
  651. databases = 'NONE'
  652. print(e)
  653. try:
  654. filters = user_input_list[3]
  655. except Exception as e:
  656. filters = 'NONE'
  657. print(e)
  658. try:
  659. days_to_deadline = user_input_list[4]
  660. except Exception as e:
  661. days_to_deadline = 'NONE'
  662. print(e)
  663. if len(user_input_list) > 4:
  664. 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"]'
  665. if room_name == 'NONE' or databases == 'NONE' or filters == 'NONE' or days_to_deadline == 'NONE':
  666. 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..'
  667. else:
  668. try:
  669. new_room = api.create_public_room(room_name,
  670. members=[latest_message_user_username],
  671. read_only=False)
  672. new_room_id = new_room['channel']['_id']
  673. room_file = open('rooms/' + new_room_id + '.txt', 'w')
  674. room_file.write( room_name + '§%§%' + databases + '§%§%' + filters + '§%§%' + days_to_deadline)
  675. room_file.close()
  676. answer = 'Der Command wurde übermittelt'
  677. except Exception as e:
  678. print('it was not able to create the room, the original error message is:', e)
  679. answer = 'There was an error creating the room, look up the logs.. the original error was: ' + str(e)
  680. #print('oi', user_input)
  681. api.send_message('Die Anfrage wird bearbeitet..', room_id)
  682. # here comes the code interacting with the spiders output json
  683. #answer = 'the up to date entries are: oi Oi Oi!'
  684. api.send_message(answer, room_id)
  685. time.sleep(1)
  686. api.send_message('Ich bin wieder bereit für Konfigurationsinput : )', room_id)
  687. time.sleep(1)