Skip to content

Instantly share code, notes, and snippets.

@JonnyWong16
Last active October 17, 2018 19:44
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonnyWong16/fab7e205f39c0782de24015e7f466710 to your computer and use it in GitHub Desktop.
Save JonnyWong16/fab7e205f39c0782de24015e7f466710 to your computer and use it in GitHub Desktop.
Updates history grouping in the Tautulli database with the new logic on v2.1.12.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Description: Updates history grouping in the Tautulli database with the new logic on v2.1.12.
# Author: /u/SwiftPanda16
# Requires: requests
import requests
### EDIT SETTINGS ###
## NOTE: Script requires 'api_sql = 1' to be enabled in the Tautulli config.ini file.
TAUTULLI_URL = 'http://localhost:8181'
TAUTULLI_APIKEY = 'xxxxxxxxxx'
DEFAULT_WATCHED_PERCENT = 85
DRY_RUN = True # True to dry run without making changes to the Tautulli database, False to make changes
## CODE BELOW ##
def main():
# Check for DRY_RUN. Backup Tautulli database if needed.
if DRY_RUN:
print("Dry run enabled. No changes will be made to the Tautulli database.")
else:
print("Not dry run. Creating a backup of the Tautulli database.")
params = {'cmd': 'backup_db',
'apikey': TAUTULLI_APIKEY,
}
requests.post(TAUTULLI_URL.rstrip('/') + '/api/v2', params=params)
# Retrieve watched percentages
print("Retrieving watched percentages from the Tautulli settings...")
params = {'cmd': 'get_settings',
'apikey': TAUTULLI_APIKEY,
}
r = requests.get(TAUTULLI_URL.rstrip('/') + '/api/v2', params=params).json()
monitoring_settings = r['response']['data']['Monitoring']
percentages = {'movie': int(monitoring_settings['movie_watched_percent']),
'episode': int(monitoring_settings['tv_watched_percent']),
'track': int(monitoring_settings['music_watched_percent']),
'default': DEFAULT_WATCHED_PERCENT
}
print("... Movie percent: {movie}\n"
"... TV episode percent: {episode}\n"
"... Music percent: {track}\n"
"... Default percent: {default}".format(**percentages))
# Update grouping
print("{}Updating grouping for history items in the Tautulli database...".format("(DRY RUN) " if DRY_RUN else ""))
if not DRY_RUN:
query = """UPDATE session_history
SET reference_id = (
SELECT COALESCE(
(
SELECT MIN(prev_entry.id, prev_entry.reference_id)
FROM (
SELECT MAX(id) AS id, reference_id, view_offset
FROM session_history
WHERE id < curr_entry.id
AND user_id = curr_entry.user_id
AND rating_key = curr_entry.rating_key
) AS prev_entry
JOIN session_history_metadata AS shm
ON prev_entry.id = shm.id
WHERE view_offset <= curr_entry.view_offset
AND (view_offset * 1.0 / (duration * 1.0) * 100) < (
CASE
WHEN shm.media_type = "movie" THEN {movie}
WHEN shm.media_type = "episode" THEN {episode}
WHEN shm.media_type = "track" THEN {track}
ELSE {default}
END
)
),
curr_entry.id
)
FROM session_history AS curr_entry
WHERE curr_entry.id = session_history.id
);""".format(**percentages)
params = {'cmd': 'sql',
'apikey': TAUTULLI_APIKEY,
'query': query
}
r = requests.get(TAUTULLI_URL.rstrip('/') + '/api/v2', params=params).json()
if r['response']['result'] == 'success':
print("...new grouping logic applied successfully.")
else:
print("...failed to apply grouping logic: {}.".format(r['response']['message']))
if __name__ == "__main__":
main()
print("Done.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment