From 6255439a3a4ed9ed4d8690dfe8dba66306717cfd Mon Sep 17 00:00:00 2001
From: Thraxis <slthraxis@gmail.com>
Date: Tue, 1 Dec 2015 22:09:53 -0800
Subject: [PATCH] Extend and rename addTraktShow to allow adding by specific
 Indexer / IndexerID

* Allows Adding shows automatically from the IMDB View Popular page
* Adds ability to add a show using IMDB, Zap2It or TVMaze ID
---
 gui/slick/views/addShows_popularShows.mako |  2 +-
 gui/slick/views/trendingShows.mako         |  2 +-
 sickbeard/helpers.py                       | 39 ++++++++++++++++++++++
 sickbeard/webserve.py                      |  8 ++++-
 4 files changed, 48 insertions(+), 3 deletions(-)

diff --git a/gui/slick/views/addShows_popularShows.mako b/gui/slick/views/addShows_popularShows.mako
index ec9ee0681..635862674 100644
--- a/gui/slick/views/addShows_popularShows.mako
+++ b/gui/slick/views/addShows_popularShows.mako
@@ -69,7 +69,7 @@
                         <p>${int(float(cur_rating)*10)}% <img src="${srRoot}/images/heart.png"></p>
                         <i>${cur_votes} votes</i>
                         <div class="traktShowTitleIcons">
-                            <a href="${srRoot}/addShows/newShow/?search_string=${cur_result['name']}" class="btn btn-xs">Add Show</a>
+                            <a href="${srRoot}/addShows/addShowByID?indexer_id=${cur_result['imdb_tt']}&amp;showName=${cur_result['name']}&amp;indexer=IMDB" class="btn btn-xs" data-no-redirect>Add Show</a>
                         </div>
                     </div>
                 </div>
diff --git a/gui/slick/views/trendingShows.mako b/gui/slick/views/trendingShows.mako
index 672b52e7e..ccd207d0c 100644
--- a/gui/slick/views/trendingShows.mako
+++ b/gui/slick/views/trendingShows.mako
@@ -38,7 +38,7 @@
             <p>${int(cur_show['show']['rating']*10)}% <img src="${srRoot}/images/heart.png"></p>
             <i>${cur_show['show']['votes']} votes</i>
             <div class="traktShowTitleIcons">
-                <a href="${srRoot}/addShows/addTraktShow?indexer_id=${cur_show['show']['ids']['tvdb']}&amp;showName=${cur_show['show']['title']}" class="btn btn-xs" data-no-redirect>Add Show</a>
+                <a href="${srRoot}/addShows/addShowByID?indexer_id=${cur_show['show']['ids']['tvdb']}&amp;showName=${cur_show['show']['title']}" class="btn btn-xs" data-no-redirect>Add Show</a>
                 % if blacklist:
                 <a href="${srRoot}/addShows/addShowToBlacklist?indexer_id=${cur_show['show']['ids']['tvdb'] or cur_show['show']['ids']['tvrage']}" class="btn btn-xs">Remove Show</a>
                 % endif
diff --git a/sickbeard/helpers.py b/sickbeard/helpers.py
index fee409f69..4c21d00bd 100644
--- a/sickbeard/helpers.py
+++ b/sickbeard/helpers.py
@@ -62,6 +62,9 @@ from itertools import izip, cycle
 import shutil
 import shutil_custom
 
+import xml.etree.ElementTree as ET
+import json
+
 shutil.copyfile = shutil_custom.copyfile_custom
 
 # pylint: disable=W0212
@@ -1758,3 +1761,39 @@ def getDiskSpaceUsage(diskPath=None):
             return pretty_file_size(st.f_bavail * st.f_frsize)
     else:
         return False
+
+
+def getTVDBFromID(indexer_id, indexer):
+    tvdb_id = ''
+    if indexer == 'IMDB':
+        url = "http://www.thetvdb.com/api/GetSeriesByRemoteID.php?imdbid=%s" % (indexer_id)
+        data = urllib.urlopen(url)
+        try:
+            tree = ET.parse(data)
+            for show in tree.getiterator("Series"):
+                tvdb_id = show.findtext("seriesid")
+
+        except SyntaxError:
+            pass
+
+        return tvdb_id
+    elif indexer == 'ZAP2IT':
+        url = "http://www.thetvdb.com/api/GetSeriesByRemoteID.php?zap2it=%s" % (indexer_id)
+        data = urllib.urlopen(url)
+        try:
+            tree = ET.parse(data)
+            for show in tree.getiterator("Series"):
+                tvdb_id = show.findtext("seriesid")
+
+        except SyntaxError:
+            pass
+
+        return tvdb_id
+    elif indexer == 'TVMAZE':
+        url = "http://api.tvmaze.com/shows/%s" % (indexer_id)
+        response = urllib2.urlopen(url)
+        data = json.load(response)
+        tvdb_id = data['externals']['thetvdb']
+        return tvdb_id
+    else:
+        return tvdb_id
diff --git a/sickbeard/webserve.py b/sickbeard/webserve.py
index ee89f8ad8..8e95b1d44 100644
--- a/sickbeard/webserve.py
+++ b/sickbeard/webserve.py
@@ -2589,7 +2589,13 @@ class HomeAddShows(Home):
         return t.render(enable_anime_options=False, title='Existing Show', header='Existing Show', topmenu="home",
                 controller="addShows", action="addExistingShow")
 
-    def addTraktShow(self, indexer_id, showName):
+    def addShowByID(self, indexer_id, showName, indexer="TVDB"):
+
+        if indexer is not "TVDB":
+            tvdb_id = helpers.getTVDBFromID(indexer_id, indexer.upper())
+            if tvdb_id is not '':
+                indexer_id = tvdb_id
+
         if Show.find(sickbeard.showList, int(indexer_id)):
             return
 
-- 
GitLab