JBOPS/utility/purge_removed_plex_friends.py

42 lines
1.2 KiB
Python
Raw Normal View History

2018-08-23 10:36:35 +00:00
#!/usr/bin/env python
"""
Description: Purge Tautulli users that no longer exist as a friend in Plex
Author: DirtyCajunRice
Requires: requests, plexapi
"""
2018-03-14 17:44:50 +00:00
import requests
from plexapi.myplex import MyPlexAccount
TAUTULLI_BASE_URL = ''
TAUTULLI_API_KEY = ''
2018-03-14 17:44:50 +00:00
PLEX_USERNAME = ''
PLEX_PASSWORD = ''
2018-03-14 17:44:50 +00:00
2018-03-14 18:07:56 +00:00
# Do you want to back up the database before deleting?
2018-03-14 18:07:56 +00:00
BACKUP_DB = True
2018-03-14 17:44:50 +00:00
# Do not edit past this line #
account = MyPlexAccount(PLEX_USERNAME, PLEX_PASSWORD)
payload = {'apikey': TAUTULLI_API_KEY, 'cmd': 'get_user_names'}
tautulli_users = requests.get('http://{}/api/v2'
.format(TAUTULLI_BASE_URL), params=payload).json()['response']['data']
plex_friend_ids = [friend.id for friend in account.users()]
tautulli_user_ids = [user['user_id'] for user in tautulli_users]
removed_user_ids = [user_id for user_id in tautulli_user_ids if user_id not in plex_friend_ids]
2018-03-14 18:07:56 +00:00
if BACKUP_DB:
payload['cmd'] = 'backup_db'
backup = requests.get('http://{}/api/v2'.format(TAUTULLI_BASE_URL), params=payload)
2018-03-14 17:44:50 +00:00
if removed_user_ids:
payload['cmd'] = 'delete_user'
for user_id in removed_user_ids:
payload['user_id'] = user_id
2018-03-14 17:44:50 +00:00
remove_user = requests.get('http://{}/api/v2'.format(TAUTULLI_BASE_URL), params=payload)