🔃 Sync Hub v0.6

This commit is contained in:
jokob-sk
2024-06-05 12:57:29 +10:00
parent 96f18b40d1
commit 5f59097b0d
2 changed files with 78 additions and 38 deletions

View File

@@ -860,6 +860,7 @@ def collect_lang_strings(json, pref, stringSqlParams):
# return decrypted_data # return decrypted_data
# pycryptodome ------------------------------------------------------------------------- # pycryptodome -------------------------------------------------------------------------
def prepare_key(encryption_key): def prepare_key(encryption_key):
key = hashlib.sha256(encryption_key.encode()).digest() key = hashlib.sha256(encryption_key.encode()).digest()
return key return key

View File

@@ -11,7 +11,7 @@ from collections import namedtuple
import conf import conf
from const import pluginsPath, logPath, applicationPath, reportTemplatesPath from const import pluginsPath, logPath, applicationPath, reportTemplatesPath
from logger import mylog from logger import mylog
from helper import timeNowTZ, updateState, get_file_content, write_file, get_setting, get_setting_value from helper import timeNowTZ, updateState, get_file_content, write_file, get_setting, get_setting_value, decrypt_data
from api import update_api from api import update_api
from plugin_utils import logEventStatusCounts, get_plugin_string, get_plugin_setting_obj, print_plugin_info, list_to_csv, combine_plugin_objects, resolve_wildcards_arr, handle_empty, custom_plugin_decoder from plugin_utils import logEventStatusCounts, get_plugin_string, get_plugin_setting_obj, print_plugin_info, list_to_csv, combine_plugin_objects, resolve_wildcards_arr, handle_empty, custom_plugin_decoder
from notification import Notification_obj from notification import Notification_obj
@@ -225,47 +225,86 @@ def execute_plugin(db, all_plugins, plugin, pluginsState = plugins_state() ):
newLines = [] newLines = []
# Create the file path # Create the file path
file_path = os.path.join(pluginsPath, plugin["code_name"], 'last_result.log') file_dir = os.path.join(pluginsPath, plugin["code_name"])
file_prefix = 'last_result'
# Check if the file exists # key to decrypt data if available
if os.path.exists(file_path): encryption_key = get_setting_value('SYNC_encryption_key')
# File exists, open it and read its contents
with open(file_path, 'r+') as f:
newLines = f.read().split('\n')
# if the script produced some outpout, clean it up to ensure it's the correct format # Check for files starting with the specified prefix
# cleanup - select only lines containing a separator to filter out unnecessary data matching_files = [f for f in os.listdir(file_dir) if f.startswith(file_prefix)]
newLines = list(filter(lambda x: '|' in x, newLines))
for line in newLines: for filename in matching_files:
columns = line.split("|") # Create the full file path
# There has to be always 9 columns file_path = os.path.join(file_dir, filename)
if len(columns) == 9:
# Create a tuple containing values to be inserted into the database. # Check if the file exists
# Each value corresponds to a column in the table in the order of the columns. if os.path.exists(file_path):
# must match the Plugins_Objects and Plugins_Events database tables and can be used as input for the plugin_object_class.
sqlParams.append( # Check if the file name contains "encoded"
( if '.encoded.' in filename and encryption_key != '':
0, # "Index" placeholder
plugin["unique_prefix"], # "Plugin" column value from the plugin dictionary # Decrypt the entire file
columns[0], # "Object_PrimaryID" value from columns list with open(file_path, 'r+') as f:
columns[1], # "Object_SecondaryID" value from columns list encrypted_data = f.read()
'null', # Placeholder for "DateTimeCreated" column decrypted_data = decrypt_data(encrypted_data, encryption_key)
columns[2], # "DateTimeChanged" value from columns list
columns[3], # "Watched_Value1" value from columns list # Write the decrypted data back to the file
columns[4], # "Watched_Value2" value from columns list f.seek(0)
columns[5], # "Watched_Value3" value from columns list f.write(decrypted_data)
columns[6], # "Watched_Value4" value from columns list f.truncate()
'not-processed', # "Status" column (placeholder)
columns[7], # "Extra" value from columns list # Rename the file
'null', # Placeholder for "UserData" column new_filename = filename.replace('.encoded.', '.decoded.')
columns[8] # "ForeignKey" value from columns list os.rename(file_path, os.path.join(file_dir, new_filename))
)
) elif filename == 'last_result.log' :
new_filename = filename
else: else:
mylog('none', ['[Plugins] Skipped invalid line in the output: ', line]) # skipping decoded and other files
else: continue
mylog('debug', [f'[Plugins] The file {file_path} does not exist'])
# Open the decrypted file and process its contents
with open(os.path.join(file_dir, new_filename), 'r') as f:
newLines = f.read().split('\n')
# if the script produced some outpout, clean it up to ensure it's the correct format
# cleanup - select only lines containing a separator to filter out unnecessary data
newLines = list(filter(lambda x: '|' in x, newLines))
for line in newLines:
columns = line.split("|")
# There has to be always 9 columns
if len(columns) == 9:
# Create a tuple containing values to be inserted into the database.
# Each value corresponds to a column in the table in the order of the columns.
# must match the Plugins_Objects and Plugins_Events database tables and can be used as input for the plugin_object_class.
sqlParams.append(
(
0, # "Index" placeholder
plugin["unique_prefix"], # "Plugin" column value from the plugin dictionary
columns[0], # "Object_PrimaryID" value from columns list
columns[1], # "Object_SecondaryID" value from columns list
'null', # Placeholder for "DateTimeCreated" column
columns[2], # "DateTimeChanged" value from columns list
columns[3], # "Watched_Value1" value from columns list
columns[4], # "Watched_Value2" value from columns list
columns[5], # "Watched_Value3" value from columns list
columns[6], # "Watched_Value4" value from columns list
'not-processed', # "Status" column (placeholder)
columns[7], # "Extra" value from columns list
'null', # Placeholder for "UserData" column
columns[8] # "ForeignKey" value from columns list
)
)
else:
mylog('none', ['[Plugins] Skipped invalid line in the output: ', line])
else:
mylog('debug', [f'[Plugins] The file {file_path} does not exist'])
# TODO: delete processed files
# os.rename(file_path, os.path.join(file_dir, new_filename))
# app-db-query # app-db-query
if plugin['data_source'] == 'app-db-query': if plugin['data_source'] == 'app-db-query':