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.
 

393 lines
16 KiB

import os
from rocketchat.api import RocketChatAPI
import configparser
import json
import requests
import yaml
import subprocess
config = configparser.ConfigParser()
config.read('config.ini')
botname = config['Chat']['username']
botpassword = config['Chat']['password']
server_url = config['Chat']['URL']
room_id = config['Chat']['room_id']
bot_user_id = config['Chat']['bot_user_id']
spider_directory = config['Spider']['spider_directory']
# here comes the functions to talk to gpt
# For local streaming, the websockets are hosted without ssl - http://
HOST = 'localhost:5000'
URI = f'http://{HOST}/api/v1/chat'
#URI = f'http://{HOST}/api'
# http://192.168.9.197:5000/api/v1/chat
# For reverse-proxied streaming, the remote will likely host with ssl - https://
# URI = 'https://your-uri-here.trycloudflare.com/api/v1/chat'
if __name__ == '__main__':
api = RocketChatAPI(settings={'username': botname, 'password': botpassword, 'domain': server_url})
# api.send_message('Ciao, I am the fdb-spider', room_id)
# myinfo = api.get_my_info()
# print(myinfo)
# rooms = api.get_private_rooms()
# print(rooms)
# api.send_message('Ole', room_id)
n = 0
import time
import schedule
already_updated = False
import datetime
from datetime import timedelta
while True:
time.sleep(4)
#already_updated = True
now = datetime.datetime.now()
current_hour = now.strftime("%H")
if int(current_hour) > 6:
aftersix = True
if int(current_hour) < 6:
aftersix = False
already_updated == False
if aftersix == True and already_updated == False:
room_list = []
rooms = os.listdir('rooms')
for room in rooms:
room_file = open('rooms/' + room, 'r')
room_file_raw = room_file.read()
room_file.close()
room_file_list = room_file_raw.split('§%§%')
room_list.append([room[:-4], room_file_list[0], room_file_list[1], room_file_list[2]])
with open(spider_directory + '/spiders/config.yaml' , "r") as stream:
try:
config = yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(exc)
fdb_list = []
#print(config)
for key in config:
fdb_list.append(key)
data = dict({})
for room in room_list:
print(room[0])
room_fdbs = room[2]
room_tags = room[3]
#subprocess.run(["python", spider_directory + 'main.py', fdbs])
for room_fdb in eval(room_fdbs):
#print('room_fdb',room_fdb, 'fdb_list',fdb_list)
try:
iteration_var_list = config.get(room_fdb).get("entry-list").get("iteration-var-list")
except Exception as e:
print('there was an error with the entry-list parameter in the config regarding the fdb ', room_fdb)
if room_fdb in fdb_list and room_fdb not in [key for key in data]:
iterdict = {}
for i in eval(iteration_var_list):
f = open(spider_directory + "/spiders/output/" + room_fdb + str(i) + "entryList.txt")
text = f.read()
dictionary_entry_list = eval(text)
iterdict[i] = dictionary_entry_list
data[room_fdb] = iterdict
for i in eval(iteration_var_list):
try:
print(room_fdb, i)
for key in data[room_fdb][i]:
contains_tag = False
for tag in eval(room_tags):
if tag in (data[room_fdb][i][key]["name"].split(' ') or data[room_fdb][i][key]["info"].split(' ') or data[room_fdb][i][key]["text"].split(' ')):
contains_tag = True
if contains_tag == True:
try:
url = data[room_fdb][i][key]["domain"]
except:
url = data[room_fdb][i][key]["link"]
entry_message = '<' + url + '|' + data[room_fdb][i][key]["name"]+ '>' + '\n' + data[room_fdb][i][key]["info"]
api.send_message(entry_message, room[0])
except Exception as e:
print("probably i was not there in last page, original error is:", e)
#print('data',data['giz'][2],'data')
already_updated = True
# if datestime.split over etc and updated= true etc
n += 1
print(n)
try:
#print('getting the room history')
date = datetime.datetime.now() - timedelta(days=3)
room_history = api.get_private_room_history(room_id, oldest=date)
except Exception as e:
time.sleep(10)
api = RocketChatAPI(settings={'username': botname, 'password': botpassword, 'domain': server_url})
time.sleep(5)
room_history = api.get_private_room_history(room_id, oldest=date)
print('got a connection error, original message is:',e)
messages_list = []
for message in room_history['messages']:
messages_list.append(message)
if len(messages_list) >= 1:
#print('blub', messages_list)
latest_message_user_id = messages_list[0]['u']['_id']
latest_message_user_username = messages_list[0]['u']['username']
latest_message = messages_list[0]['msg']
latest_message_id = messages_list[0]['_id']
new_message_file = open('new_message_file.txt', 'r')
new_message = new_message_file.read()
new_message_file.close()
new_message_list = new_message.split('§%§%')
#print(latest_message, new_message_list[0])
if new_message_list[0] != latest_message and new_message_list[1] != latest_message_id and latest_message_user_id != bot_user_id:
answer = 'Ich habe kein Kommando erhalten.'
new_message_file = open('new_message_file.txt', 'w')
new_message_file.write(latest_message + '§%§%' + latest_message_id)
new_message_file.close()
user_input = latest_message
user_input_list = user_input.split(' ')
if user_input_list[0] == 'printrooms':
room_was_found = False
# get all the rooms
import os
room_list = []
rooms = os.listdir('rooms')
for room in rooms:
room_file = open('rooms/' + room, 'r')
room_file_raw = room_file.read()
room_file.close()
room_file_list = room_file_raw.split('§%§%')
room_list.append([room[:-4], room_file_list[0], room_file_list[1], room_file_list[2]])
#print(room_list)
answer = str(room_list)
if user_input_list[0] == 'updaterooms':
room_list = []
rooms = os.listdir('rooms')
for room in rooms:
room_file = open('rooms/' + room, 'r')
room_file_raw = room_file.read()
room_file.close()
room_file_list = room_file_raw.split('§%§%')
room_list.append([room[:-4], room_file_list[0], room_file_list[1], room_file_list[2]])
with open(spider_directory + '/spiders/config.yaml' , "r") as stream:
try:
config = yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(exc)
fdb_list = []
#print(config)
for key in config:
fdb_list.append(key)
answer = "Ich update die Rooms auf Basis der Daten von heute morgen um 6 Uhr.."
data = dict({})
for room in room_list:
if room[0] == room_to_update:
room_was_found = True
print(room[0])
room_fdbs = room[2]
room_tags = room[3]
#subprocess.run(["python", spider_directory + 'main.py', fdbs])
for room_fdb in eval(room_fdbs):
#print('room_fdb',room_fdb, 'fdb_list',fdb_list)
try:
iteration_var_list = config.get(room_fdb).get("entry-list").get("iteration-var-list")
except Exception as e:
print('there was an error with the entry-list parameter in the config regarding the fdb ', room_fdb)
if room_fdb in fdb_list and room_fdb not in [key for key in data]:
iterdict = {}
for i in eval(iteration_var_list):
f = open(spider_directory + "/spiders/output/" + room_fdb + str(i) + "entryList.txt")
text = f.read()
dictionary_entry_list = eval(text)
iterdict[i] = dictionary_entry_list
data[room_fdb] = iterdict
for i in eval(iteration_var_list):
try:
print(room_fdb, i)
for key in data[room_fdb][i]:
contains_tag = False
for tag in eval(room_tags):
if tag in (data[room_fdb][i][key]["name"].split(' ') or data[room_fdb][i][key]["info"].split(' ') or data[room_fdb][i][key]["text"].split(' ')):
contains_tag = True
if contains_tag == True:
try:
url = data[room_fdb][i][key]["domain"]
except:
url = data[room_fdb][i][key]["link"]
entry_message = '<' + url + '|' + data[room_fdb][i][key]["name"]+ '>' + '\n' + data[room_fdb][i][key]["info"]
api.send_message(entry_message, room[0])
except Exception as e:
print("probably i was not there in last page, original error is:", e)
if room_was_found == False:
answer = 'Den angegebenen Raum gibt es nicht.. Versuche es noch einmal mit -> updaterooms <room-id>'
if room_was_found == True:
answer = "Ich update die Rooms auf Basis der Daten in fdb-spider/spiders/output.."
if user_input_list[0] == 'printfdbs':
answer = str(fdb_list)
if len(user_input_list) > 1:
answer = 'Die Syntax zum Ausgeben der konfigurierten Förderdatenbanken hat einen Fehler. Versuche es erneut, mit -> printfdbs'
if user_input_list[0] == 'deleteroom':
try:
room_id_del = user_input_list[1]
except Exception as e:
room_id_del = 'NONE'
print(e)
os.remove("rooms/" + room_id_del + ".txt")
anwer = 'deleting the requested room..'
if len(user_input_list) > 2:
answer = 'Die Syntax zum Löschen eines Raumes hat einen Fehler. Versuche es erneut, nach dem Muster -> deleteroom <room_id>'
if user_input_list[0] == 'createroom':
try:
room_name = user_input_list[1]
except Exception as e:
room_name = 'NONE'
print(e)
try:
databases = user_input_list[2]
except Exception as e:
databases = 'NONE'
print(e)
try:
filters = user_input_list[3]
except Exception as e:
filters = 'NONE'
print(e)
if len(user_input_list) > 3:
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"]'
if room_name == 'NONE' or databases == 'NONE' or filters == 'NONE':
answer = 'Um einen Raum zu erstellen, in dem neueste Einträge geteilt werden, lautet die Syntax: createroom <Raumname> <Liste-Datenbanken> <Liste-Filtertags>'
else:
try:
new_room = api.create_public_room(room_name,
members=[latest_message_user_username],
read_only=True)
new_room_id = new_room['channel']['_id']
room_file = open('rooms/' + new_room_id + '.txt', 'w')
room_file.write( room_name + '§%§%' + databases + '§%§%' + filters)
room_file.close()
answer = 'Der Command wurde übermittelt'
except Exception as e:
print('it was not able to create the room, the original error message is:', e)
answer = 'There was an error creating the room, look up the logs.. the original error was: ' + str(e)
#print('oi', user_input)
api.send_message('Die Anfrage wird bearbeitet..', room_id)
# here comes the code interacting with the spiders output json
#answer = 'the up to date entries are: oi Oi Oi!'
api.send_message(answer, room_id)
time.sleep(1)
api.send_message('Ich bin wieder bereit für Konfigurationsinput : )', room_id)
time.sleep(1)