From a4397e7e4fc258e79f532f037f03aa692fdfa916 Mon Sep 17 00:00:00 2001 From: Jakub Fortunka Date: Mon, 16 Dec 2024 12:21:51 +0100 Subject: [PATCH] fix shows import Trakt sync api returns `episodes` field even when sync is for whole shows. This makes using `options.type` as key in `result['added']` dict impossible, since result won't have this key. To solve this issue, I'm using `Counter` object (instead of standard dict), which sums `added` and `updated`. For `not_found` I decided that it would make sense to aggregate data instead of only showing counter (since it should be useful to know what wasn't imported). --- .gitignore | 3 +++ README.md | 32 +++++++++++++------------------- import_trakt.py | 32 +++++++++++++++++--------------- requirements.txt | 3 +++ 4 files changed, 36 insertions(+), 34 deletions(-) create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore index 4d38ae7..3cd3404 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,6 @@ *.csv *.ini *.pyc + +config/ +.venv diff --git a/README.md b/README.md index 6a77b68..e52dcb1 100644 --- a/README.md +++ b/README.md @@ -91,18 +91,12 @@ $ python -V Python 3.5 ``` -Install need module dependencies, `python3-openssl` and `jq` are optional +Setup [`venv`](https://docs.python.org/3/library/venv.html) and install dependencies ``` -$ apt-get install python3-dateutil python3-simplejson python3-requests python3-openssl jq -``` - -### On Arch/Manjaro Linux system - -Install dependencies with pacman, `python-pyopenssl` and `jq` are optional - -``` -$ pacman -S python python-dateutil python-simplejson python-requests python-pyopenssl jq +python -m venv .venv +source .venv/bin/activate +pip3 install -r requirements.txt ``` ### On Windows system @@ -115,10 +109,12 @@ Ensure you are running Python 3 Python 3.5 ``` -Install need module dependencies +Setup [`venv`](https://docs.python.org/3/library/venv.html) and install dependencies ``` -\Scripts\pip3.exe install requests simplejson +python -m venv .venv +.venv\Scripts\activate.bat +pip3 install -r requirements.txt ``` ### On macOS system @@ -146,14 +142,12 @@ $ pip3 install certifi Open a new Terminal session so that certificates will be available -Install need module dependencies, `pyopenssl` and `jq` are optional +Setup [`venv`](https://docs.python.org/3/library/venv.html) and install dependencies -```shell -$ pip3 install python-dateutil -$ pip3 install simplejson -$ pip3 install requests -$ pip3 install pyopenssl -$ pip3 install jq +``` +python -m venv .venv +source .venv/bin/activate +pip3 install -r requirements.txt ``` ## Usage diff --git a/import_trakt.py b/import_trakt.py index a7c1d0f..4982f0c 100755 --- a/import_trakt.py +++ b/import_trakt.py @@ -37,6 +37,7 @@ import collections import pprint import time +from collections import Counter pp = pprint.PrettyPrinter(indent=4) @@ -449,7 +450,7 @@ def main(): # if IDs make the list into trakt format data = [] - results = {'sentids' : 0, 'added' : 0, 'existing' : 0, 'not_found' : 0} + results = {'sentids' : 0, 'added' : Counter({}), 'updated' : Counter({}), 'not_found' : {}} if read_ids: print("Found {0} items to import".format(len(read_ids))) @@ -482,12 +483,7 @@ def main(): result = api_add_to_list(options, data) if result: print("Result: {0}".format(result)) - if 'added' in result and result['added']: - results['added'] += result['added'][options.type] - if 'existing' in result and result['existing']: - results['existing'] += result['existing'][options.type] - if 'not_found' in result and result['not_found']: - results['not_found'] += len(result['not_found'][options.type]) + __update_counters(results, result) data = [] # Import the rest if len(data) > 0: @@ -496,20 +492,26 @@ def main(): result = api_add_to_list(options, data) if result: print("Result: {0}".format(result)) - if 'added' in result and result['added']: - results['added'] += result['added'][options.type] - if 'existing' in result and result['existing']: - results['existing'] += result['existing'][options.type] - if 'not_found' in result and result['not_found']: - results['not_found'] += len(result['not_found'][options.type]) + __update_counters(results, result) else: # TODO Read STDIN to ID print("No items found, nothing to do.") sys.exit(0) - print("Overall imported {sent} {type}, results added:{added}, existing:{existing}, not_found:{not_found}".format( + print("Overall imported {sent} {type}, results added:{added}, updated:{updated}, not_found:{not_found}".format( sent=results['sentids'], type=options.type, added=results['added'], - existing=results['existing'], not_found=results['not_found'])) + updated=results['updated'], not_found=results['not_found'])) + +def __update_counters(results, result): + if 'added' in result and result['added']: + results['added'].update(Counter(result['added'])) + if 'updated' in result and result['updated']: + results['updated'].update(Counter(result['updated'])) + if 'not_found' in result and result['not_found']: + for key, value in result['not_found'].items(): + if key not in results['not_found']: + results['not_found'][key] = [] + results['not_found'][key].extend(value) if __name__ == '__main__': main() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..72696d0 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +python-dateutil +simplejson +requests