JBOPS/reporting/plays_by_library.py

79 lines
2.3 KiB
Python
Raw Normal View History

#!/usr/bin/env python
# -*- coding: utf-8 -*-
2017-12-08 17:42:13 +00:00
"""
Use Tautulli to pull plays by library
2017-12-08 17:42:13 +00:00
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)
2017-12-08 17:42:13 +00:00
Usage:
plays_by_library.py -l "TV Shows" Movies
TV Shows - Plays: 2859
Movies - Plays: 379
"""
2020-07-04 20:08:59 +00:00
from __future__ import print_function
2020-07-04 20:31:02 +00:00
from __future__ import unicode_literals
2017-12-08 17:42:13 +00:00
import requests
import sys
import argparse
# import json
2017-12-08 17:42:13 +00:00
# ## EDIT THESE SETTINGS ##
2017-12-08 17:42:13 +00:00
TAUTULLI_APIKEY = 'xxxxxx' # Your Tautulli API key
TAUTULLI_URL = 'http://localhost:8181/' # Your Tautulli URL
2017-12-08 17:42:13 +00:00
OUTPUT = '{section} - Plays: {plays}'
# ## CODE BELOW ##
2017-12-08 17:42:13 +00:00
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': TAUTULLI_APIKEY,
2017-12-08 17:42:13 +00:00
'cmd': 'get_libraries_table',
'order_column': 'plays'}
try:
r = requests.get(TAUTULLI_URL.rstrip('/') + '/api/v2', params=payload)
2017-12-08 17:42:13 +00:00
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("Tautulli API 'get_libraries_table' request failed: {0}.".format(e))
2017-12-08 17:42:13 +00:00
def main():
lib_lst = [section['section_name'] for section in get_libraries_table()]
parser = argparse.ArgumentParser(description="Use Tautulli to pull plays by library",
2017-12-08 17:42:13 +00:00
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()