From cd65d78e81a19c31b2e9e2cb6e1a3dfb71fed2c1 Mon Sep 17 00:00:00 2001 From: blacktwin Date: Fri, 8 Dec 2017 12:42:13 -0500 Subject: [PATCH] Create plays_by_library.py --- reporting/plays_by_library.py | 74 +++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 reporting/plays_by_library.py diff --git a/reporting/plays_by_library.py b/reporting/plays_by_library.py new file mode 100644 index 0000000..2ac56ed --- /dev/null +++ b/reporting/plays_by_library.py @@ -0,0 +1,74 @@ +""" +Use PlexPy to pull plays by library + +optional arguments: + -h, --help show this help message and exit + -l [ ...], --libraries [ ...] + Space separated list of case sensitive names to process. Allowed names are: + (choices: All Library Names) + + +Usage: + plays_by_library.py -l "TV Shows" Movies + TV Shows - Plays: 2859 + Movies - Plays: 379 + +""" + +import requests +import sys +import argparse +import json + + + +## EDIT THESE SETTINGS ## + +PLEXPY_APIKEY = 'xxxxxx' # Your PlexPy API key +PLEXPY_URL = 'http://localhost:8181/' # Your PlexPy URL + +OUTPUT = '{section} - Plays: {plays}' + +## CODE BELOW ## + +def get_libraries_table(sections=None): + # Get a list of new rating keys for the PMS of all of the item's parent/children. + payload = {'apikey': PLEXPY_APIKEY, + 'cmd': 'get_libraries_table', + 'order_column': 'plays'} + + try: + r = requests.get(PLEXPY_URL.rstrip('/') + '/api/v2', params=payload) + response = r.json() + # print(json.dumps(response, indent=4, sort_keys=True)) + + res_data = response['response']['data']['data'] + if sections: + return [d for d in res_data if d['section_name'] in sections] + else: + return [d for d in res_data if d['section_name']] + + except Exception as e: + sys.stderr.write("PlexPy API 'get_libraries_table' request failed: {0}.".format(e)) + + +def main(): + + lib_lst = [section['section_name'] for section in get_libraries_table()] + + parser = argparse.ArgumentParser(description="Use PlexPy to pull plays by library", + formatter_class=argparse.RawTextHelpFormatter) + parser.add_argument('-l', '--libraries', nargs='+', type=str, choices=lib_lst, metavar='', + help='Space separated list of case sensitive names to process. Allowed names are: \n' + '(choices: %(choices)s)') + + opts = parser.parse_args() + + for section in get_libraries_table(opts.libraries): + sec_name = section['section_name'] + sec_plays = section['plays'] + print(OUTPUT.format(section=sec_name, plays=sec_plays)) + + +if __name__ == "__main__": + main()