From 82ab2f23119425c7cc1f7b6c085b200b0bb0d4c5 Mon Sep 17 00:00:00 2001
From: angelblue05 <tamara.angel05@gmail.com>
Date: Thu, 27 Jul 2017 22:51:24 -0500
Subject: [PATCH] Add recently added filter

To ignore playcount, now supports

plugin://plugin.video.emby/?id=Tag&mode=recentepisodes&limit=25&filters=playcount
---
 default.py                  |  8 ++++++--
 resources/lib/entrypoint.py | 15 ++++++++-------
 2 files changed, 14 insertions(+), 9 deletions(-)

diff --git a/default.py b/default.py
index 87237518..aa042b6c 100644
--- a/default.py
+++ b/default.py
@@ -44,7 +44,7 @@ class Main(object):
         base_url = sys.argv[0]
         path = sys.argv[2]
         params = urlparse.parse_qs(path[1:])
-        log.warn("Parameter string: %s", path)
+        log.warn("Parameter string: %s params: %s", path, params)
         try:
             mode = params['mode'][0]
         except (IndexError, KeyError):
@@ -111,7 +111,11 @@ class Main(object):
                 database_id = params.get('dbid')
                 action(item_id, database_id)
 
-            elif mode in ('nextup', 'inprogressepisodes', 'recentepisodes'):
+            elif mode == 'recentepisodes':
+                limit = int(params['limit'][0])
+                action(item_id, limit, params.get('filters', [""])[0])
+
+            elif mode in ('nextup', 'inprogressepisodes'):
                 limit = int(params['limit'][0])
                 action(item_id, limit)
 
diff --git a/resources/lib/entrypoint.py b/resources/lib/entrypoint.py
index f5b7ae2b..5f8e5ee8 100644
--- a/resources/lib/entrypoint.py
+++ b/resources/lib/entrypoint.py
@@ -1060,9 +1060,10 @@ def getInProgressEpisodes(tagname, limit):
     xbmcplugin.endOfDirectory(handle=int(sys.argv[1]))
 
 ##### GET RECENT EPISODES FOR TAGNAME #####    
-def getRecentEpisodes(tagname, limit):
+def getRecentEpisodes(tagname, limit, filters=""):
     
     count = 0
+    filters = filters.split(',') if filters else []
     # if the addon is called with recentepisodes parameter,
     # we return the recentepisodes list of the given tagname
     xbmcplugin.setContent(int(sys.argv[1]), 'episodes')
@@ -1076,7 +1077,7 @@ def getRecentEpisodes(tagname, limit):
 
             'sort': {'order': "descending", 'method': "dateadded"},
             'filter': {'operator': "is", 'field': "tag", 'value': "%s" % tagname},
-            'properties': ["title","sorttitle"]
+            'properties': ["title", "sorttitle"]
         }
     }
     result = xbmc.executeJSONRPC(json.dumps(query))
@@ -1087,9 +1088,7 @@ def getRecentEpisodes(tagname, limit):
     except (KeyError, TypeError):
         pass
     else:
-        allshowsIds = set()
-        for item in items:
-            allshowsIds.add(item['tvshowid'])
+        allshowsIds = set(item['tvshowid'] for item in items)
 
         query = {
 
@@ -1099,15 +1098,17 @@ def getRecentEpisodes(tagname, limit):
             'params': {
 
                 'sort': {'order': "descending", 'method': "dateadded"},
-                'filter': {'operator': "lessthan", 'field': "playcount", 'value': "1"},
                 'properties': [
                     "title", "playcount", "season", "episode", "showtitle", "plot",
                     "file", "rating", "resume", "tvshowid", "art", "streamdetails",
                     "firstaired", "runtime", "cast", "writer", "dateadded", "lastplayed"
                 ],
-                "limits": {"end": limit}
+                "limits": {"end": limit*5}
             }
         }
+        if 'playcount' not in filters:
+            query['params']['filter'] = {'operator': "lessthan", 'field': "playcount", 'value': "1"}
+
         result = xbmc.executeJSONRPC(json.dumps(query))
         result = json.loads(result)
         try: