Skip to content

Instantly share code, notes, and snippets.

@blacktwin
Last active January 31, 2017 19:35
Show Gist options
  • Save blacktwin/ef3e704387dd54b1ee0692f1892d3dd8 to your computer and use it in GitHub Desktop.
Save blacktwin/ef3e704387dd54b1ee0692f1892d3dd8 to your computer and use it in GitHub Desktop.
Find when media was added between STARTFRAME and ENDFRAME to Plex through PlexPy.
"""
Find when media was added between STARTFRAME and ENDFRAME to Plex through PlexPy.
This will only show for what has been wached.
api_sql must be manually enabled in the config file.
"""
import requests
import sys
import time
TODAY = time.time()
## EDIT THESE SETTINGS ###
PLEXPY_APIKEY = 'XXXXXXX' # Your PlexPy API key
PLEXPY_URL = 'http://localhost:8181/' # Your PlexPy URL
STARTFRAME = 1483228800 # 2017, Jan 1
ENDFRAME = 1485907200 # 2017, February 1
## DO NOT EDIT BELOW ##
class UserHIS(object):
def __init__(self, data=None):
d = data or {}
self.title = d['title']
self.full_title = d['full_title']
self.rating_key = d['rating_key']
self.added_at = d['added_at']
self.media_type = d['media_type']
self.grandparent_title = d['grandparent_title']
def sql_look(query):
try:
payload = {'apikey': PLEXPY_APIKEY,
'cmd': 'sql',
'query': query}
r = requests.get(PLEXPY_URL.rstrip('/') + '/api/v2', params=payload)
response = r.json()
res_data = response['response']['data']
return [UserHIS(data=d) for d in res_data]
except Exception as e:
sys.stderr.write("PlexPy API 'get_sql' request failed: {0}.".format(e))
if __name__ == "__main__":
sess_met = "SELECT DISTINCT rating_key, grandparent_title, title, full_title, added_at, media_type " \
"FROM session_history_metadata WHERE added_at BETWEEN %s AND %s " \
"ORDER BY media_type, added_at ASC;" % (STARTFRAME, ENDFRAME)
sl = sql_look(sess_met)
for i in sl:
added = time.ctime(float(i.added_at))
if i.grandparent_title == '':
title = i.title
elif i.title == '':
title = i.grandparent_title
else:
title = i.full_title
print("{t} ({i.rating_key} {i.media_type}) was added {when}").format(i=i, t=title, when=added)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment