Skip to content

Commit

Permalink
Code error fixes
Browse files Browse the repository at this point in the history
- Fixed some spelling errors
- Extended tests
- Fixed stuff from PR03
  • Loading branch information
j54j6 committed Jun 8, 2024
1 parent eda20b0 commit afbf0ce
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 30 deletions.
48 changes: 43 additions & 5 deletions .github/workflows/functionality_tests.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Subscriptions

on:
on:
- push
- pull_request

Expand All @@ -11,7 +11,7 @@ jobs:
os: [ubuntu-latest, windows-latest, macos-latest]
python-version: ['3.10', '3.11', '3.12']
runs-on: ${{matrix.os}}

steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
Expand Down Expand Up @@ -48,6 +48,45 @@ jobs:
exit 1
fi
shell: bash
- name: Export subscription
run: |
python3 ./yt_manager.py export-subscriptions
EXIT_CODE=$?
if [ $EXIT_CODE -eq 0 ]; then
echo "Command succeeded with exit code 0"
elif [ $EXIT_CODE -eq -1 ]; then
echo "Command failed with exit code -1"
else
echo "Command exited with unexpected code $EXIT_CODE"
exit 1
fi
shell: bash
- name: Import subscription
run: |
python3 ./yt_manager.py import-subscriptions ../ytdownloader/subscriptions_export.json True
EXIT_CODE=$?
if [ $EXIT_CODE -eq 0 ]; then
echo "Command succeeded with exit code 0"
elif [ $EXIT_CODE -eq -1 ]; then
echo "Command failed with exit code -1"
else
echo "Command exited with unexpected code $EXIT_CODE"
exit 1
fi
shell: bash
- name: List Subscriptions (after import)
run: |
python3 ./yt_manager.py list-subscriptions
EXIT_CODE=$?
if [ $EXIT_CODE -eq 0 ]; then
echo "Command succeeded with exit code 0"
elif [ $EXIT_CODE -eq -1 ]; then
echo "Command failed with exit code -1"
else
echo "Command exited with unexpected code $EXIT_CODE"
exit 1
fi
shell: bash
- name: Delete Subscription
run: |
python3 ./yt_manager.py del-subscription @PracticalEngineeringChannel
Expand All @@ -61,6 +100,5 @@ jobs:
exit 1
fi
shell: bash





34 changes: 18 additions & 16 deletions database_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ def insert_value(table:str, data:dict):
logger.error("Statemet: Insert into %s (%s) VALUES (?), %s", table, keys, values)
return False

def delete_value(table:str, conditions: dict|list):
def delete_value(table:str, conditions: dict|list, delete_all_content=False):
""" Delete a value from db. Conditions are passed as json with columnname as key
and column value as value
Expand All @@ -458,23 +458,25 @@ def delete_value(table:str, conditions: dict|list):
- False -> Failed
"""
logging.debug("Remove from table %s", table)
if not delete_all_content:
query = f"DELETE FROM {table} WHERE "
conditions_part = ""

query = f"DELETE FROM {table} WHERE "
conditions_part = ""

if isinstance(conditions, dict):
for condition in conditions:
conditions_part += condition + f"=\"{conditions[condition]}\" AND "
conditions_part = conditions_part[:-5]
elif isinstance(conditions, list):
for condition_set in conditions:
#Iterate over all conditions
for condition in condition_set:
conditions_part += condition + f"=\"{condition_set[condition]}\" AND "
if isinstance(conditions, dict):
for condition in conditions:
conditions_part += condition + f"=\"{conditions[condition]}\" AND "
conditions_part = conditions_part[:-5]
conditions_part += " OR "
conditions_part = conditions_part[:-4]
query = query + conditions_part
elif isinstance(conditions, list):
for condition_set in conditions:
#Iterate over all conditions
for condition in condition_set:
conditions_part += condition + f"=\"{condition_set[condition]}\" AND "
conditions_part = conditions_part[:-5]
conditions_part += " OR "
conditions_part = conditions_part[:-4]
query = query + conditions_part
else:
query = f"TRUNCATE TABLE {table}"
try:
cursor = ENGINE.cursor()
cursor.execute(query)
Expand Down
24 changes: 16 additions & 8 deletions project_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,16 @@ def export_subscriptions():
return False
return True

def import_subscriptions(path="./"):
def import_subscriptions(path="./", delelte_current_subscriptions=False):
""" This function imports subscriptions based on a json file (generated by export_subscriptions function)"""

if delelte_current_subscriptions:
logger.info("Current subscriptions will be deleted before import!")
if(delete_value("subscriptions", None, True)):
logger.info("Subscriptions removed!")
else:
logger.error("Error while removing old subscriptions! - Abort")
return False
logging.info("Import subscriptions from %s", path)

if not os.path.exists(path):
Expand Down Expand Up @@ -1987,10 +1995,10 @@ def show_help():
'''Create a backup and export all subscriptions and items into json files.'''])
help_table.add_row(['export-items',
'',
'''Creeate a backup file with all items in the db.'''])
'''Create a backup file with all items in the db.'''])
help_table.add_row(['import-items',
'<<path>>',
'''Creeate a backup file with all items in the db.'''])
'''Create a backup file with all items in the db.'''])
help_table.add_row(['show-duplicates',
'',
'''Show duplicates (use command validate before!)'''])
Expand Down Expand Up @@ -2523,7 +2531,7 @@ def add_duplicate_file(hash_value, c_filename, c_filepath= None, db_id=None, db_
if not os.path.isfile(os.path.abspath(os.path.join(db_filepath, db_filename))):
logger.info("Remove file from db - since it don't exist!")
delete_value("items", {"id": db_id})

else:
if hash_value in duplicates_json:
listing = duplicates_json[hash_value]
Expand All @@ -2535,7 +2543,7 @@ def add_duplicate_file(hash_value, c_filename, c_filepath= None, db_id=None, db_
delete_value("items", {"id": db_id})
else:
new_listing.append({"file_id": entry["file_id"], "file_name": entry["file_name"], "file_path": entry["file_path"]})

if entry["file_name"] == c_filename and entry["file_path"] == c_filepath:
found_same = True

Expand Down Expand Up @@ -2649,13 +2657,13 @@ def insert_missing_file_data_in_db(file_id, url, metadata):
error_occured = True

#check if metadata added
if db_entry[2] is None or db_entry[1].strip() == "":
logger.debug("Tags not added - Add to db entry")
if db_entry[2] is None or db_entry[2].strip() == "":
logger.debug("Metadata not added - Add to db entry")

added_tags = update_value("items", {"data": metadata},
{"id": file_id})
if not added_tags:
logging.error("Error while adding tags to %s", file_id)
logging.error("Error while adding metadata to %s", file_id)
error_occured = True

#Check if url is in db
Expand Down
7 changes: 6 additions & 1 deletion yt_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,13 @@
case "export-subscriptions":
NO_ERROR = export_subscriptions()
case "import-subscriptions":
if sys.argv[2] is not None:
if sys.argv[2] is not None and sys.argv[3] is None:
NO_ERROR = import_subscriptions(sys.argv[2])
elif sys.argv[2] is not None and sys.argv[3] is not None:
if sys.argv[3] == "True":
NO_ERROR = import_subscriptions(sys.argv[2], True)
else:
NO_ERROR = import_subscriptions(sys.argv[2])
else:
logging.error("Please provide a path to import subscriptions")
show_help()
Expand Down

0 comments on commit afbf0ce

Please sign in to comment.