From 59aaa4d42b8a0357ac4f024bfe0d70e240ef66cf Mon Sep 17 00:00:00 2001
From: angelblue05 <tamara.angel05@gmail.com>
Date: Tue, 4 Aug 2015 01:07:39 -0500
Subject: [PATCH] First attempt at fixing music

Pull the list of songs in batch of 200 items to prevent timeouts.
---
 resources/lib/DownloadUtils.py |  2 +-
 resources/lib/LibrarySync.py   |  2 +-
 resources/lib/ReadEmbyDB.py    | 19 +++++++++++++++++++
 3 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/resources/lib/DownloadUtils.py b/resources/lib/DownloadUtils.py
index 671c983e..436a19d5 100644
--- a/resources/lib/DownloadUtils.py
+++ b/resources/lib/DownloadUtils.py
@@ -27,7 +27,7 @@ class DownloadUtils():
 
     # Requests session
     s = None
-    timeout = 30
+    timeout = 60
 
     def __init__(self):
 
diff --git a/resources/lib/LibrarySync.py b/resources/lib/LibrarySync.py
index aeeb9feb..f4e6640d 100644
--- a/resources/lib/LibrarySync.py
+++ b/resources/lib/LibrarySync.py
@@ -448,7 +448,7 @@ class LibrarySync(threading.Thread):
         allKodiSongIds = list()
         allEmbySongIds = list()
         
-        allEmbySongs = ReadEmbyDB().getMusicSongs()
+        allEmbySongs = ReadEmbyDB().getMusicSongsTotal()
         allKodiSongs = ReadKodiDB().getKodiMusicSongs(connection, cursor)
         
         for kodisong in allKodiSongs:
diff --git a/resources/lib/ReadEmbyDB.py b/resources/lib/ReadEmbyDB.py
index 7e90d90a..0ee94fb9 100644
--- a/resources/lib/ReadEmbyDB.py
+++ b/resources/lib/ReadEmbyDB.py
@@ -96,6 +96,25 @@ class ReadEmbyDB():
                 result = self.filterbyId(result, itemList)
 
         return result
+
+    def getMusicSongsTotal(self):
+
+        result = []
+
+        url = "{server}/mediabrowser/Users/{UserId}/Items?Index=1&Limit=1&Fields=Etag,Path,Genres,SortName,Studios,Writer,ProductionYear,Taglines,CommunityRating,OfficialRating,CumulativeRunTimeTicks,Metascore,AirTime,DateCreated,MediaStreams,People,Overview&Recursive=true&IncludeItemTypes=Audio&format=json"
+        jsondata = self.doUtils.downloadUrl(url)
+
+        total = jsondata['TotalRecordCount']
+        index = 1
+        jump = 200
+
+        while index < total:
+            url = "{server}/mediabrowser/Users/{UserId}/Items?StartIndex=%s&Limit=%s&Fields=Etag,Path,Genres,SortName,Studios,Writer,ProductionYear,Taglines,CommunityRating,OfficialRating,CumulativeRunTimeTicks,Metascore,AirTime,DateCreated,MediaStreams,People,Overview&Recursive=true&IncludeItemTypes=Audio&format=json" % (index, jump)
+            jsondata = self.doUtils.downloadUrl(url)
+            result.extend(jsondata['Items'])
+            index += 200
+
+        return result
     
     def getMusicAlbums(self, itemList = []):