Repository: pymedusa/Medusa

Issue:
[APP SUBMITTED]: 'year' ::: NOTE:  This error happened when updating :::
### INFO

Python Version: **2.7.10 (default, Jun 26 2015, 16:00:40) [GCC 4.8.2]**
Operating System: **Linux-4.1.18-unRAID-x86_64-AMD_C-60_APU_with_Radeon-tm-_HD_Graphics-with-slackware-14.1**
Locale: UTF-8
Branch: **develop**
Commit: PyMedusa/SickRage@316d27cc00f0cef7f4dd0840e9c5881bf4a00eaa
No Log available with ERRORS:
### ERROR

```
EVENT-QUEUE :: [316d27c] Exception generated in thread EVENT-QUEUE: year
Traceback (most recent call last):
  File "/usr/local/Sickrage/sickbeard/event_queue.py", line 45, in run
    self.callback(event_type)
  File "/usr/local/Sickrage/SickBeard.py", line 519, in shutdown
    sickbeard.saveAll()  # save all shows to DB
  File "/usr/local/Sickrage/sickbeard/__init__.py", line 1655, in saveAll
    show.saveToDB()
  File "/usr/local/Sickrage/sickbeard/tv.py", line 1171, in saveToDB
    if self.imdbid and self.imdb_info['year']:
KeyError: 'year'
```

---

_STAFF NOTIFIED_: @pymedusa/support @pymedusa/moderators



Discussion:
@Thraxis We can go for the quick fix. self.imdb_info.get('year'), but i'd like to know more about the RC.
Can you find out which show was updating?

As far as I know there was no specific show updating at that time.  all I did was click the link to update to the latest version and it never finished.  I checked the logs and that was the error that prevented the update from happening.  

I'll check to see if I still have the full log file.

The error was for Ah! My Buddha:

```
2016-04-14 18:11:18 DEBUG    EVENT-QUEUE :: [f358c8b] 80075: Saving to database: Ah! My Buddha
2016-04-14 18:11:18 ERROR    EVENT-QUEUE :: [f358c8b] Exception generated in thread EVENT-QUEUE: year
Traceback (most recent call last):
  File "/usr/local/Sickrage/sickbeard/event_queue.py", line 45, in run
    self.callback(event_type)
  File "/usr/local/Sickrage/SickBeard.py", line 519, in shutdown
    sickbeard.saveAll()  # save all shows to DB
  File "/usr/local/Sickrage/sickbeard/__init__.py", line 1655, in saveAll
    show.saveToDB()
  File "/usr/local/Sickrage/sickbeard/tv.py", line 1171, in saveToDB
    if self.imdbid and self.imdb_info['year']:
KeyError: 'year'
2016-04-14 18:11:18 DEBUG    EVENT-QUEUE :: [f358c8b] 'Traceback (most recent call last):\n  File "/usr/local/Sickrage/sickbeard/event_queue.py", line 45, in run\n    self.callback(event_type)\n  File "/usr/local/Sickrage/SickBeard.py", line 519, in shutdown\n    sickbeard.saveAll()  # save all shows to DB\n  File "/usr/local/Sickrage/sickbeard/__init__.py", line 1655, in saveAll\n    show.saveToDB()\n  File "/usr/local/Sickrage/sickbeard/tv.py", line 1171, in saveToDB\n    if self.imdbid and self.imdb_info[\'year\']:\nKeyError: \'year\'\n'
```

It also errors on the show display page

```
File /usr/local/Sickrage/gui/slick/views/displayShow.mako:132, in render_content:
                ${show.imdb_info['runtimes']} minutes
KeyError: 'runtimes'
```

[IMDB Page](http://www.imdb.com/title/tt0978531/)
[TVDB Page](http://thetvdb.com/?tab=season&seriesid=80075)
[trakt Page](https://trakt.tv/shows/ah-my-buddha)

IMDb lists the seasons separately ([tt0978531](http://www.imdb.com/title/tt0978531/), [tt0978530](http://www.imdb.com/title/tt0978530/)), and both IMDb IDs are listed under the single series on [TVDB](http://thetvdb.com/?tab=series&id=80075).  

When checking IMDb for show info, we see there is an IMDb ID on TVDB.   However, since we don't detect multiple IMDb IDs, the listed value `tt0978531, tt0978530` is tried which is an invalid IMDb ID.  Medusa still thinks there is valid IMDb information and proceeds to process the IMDb info which is non-existent, resulting in the `KeyError`s.  The IMDb link on the displayShow page is also invalid.

This is part of the problem in making assumptions about data without proper input validation.  A more robust implementation would (among other things):
- Validate that the IMDb ID is valid
- Not make assumptions about the returned information
- Use robust dictionary methods such as `get` and provide a default value when appropriate
- Catch `KeyError`s and `ValueError`s when appropriate


Resolving patch:
diff --git a/sickbeard/tv.py b/sickbeard/tv.py
--- a/sickbeard/tv.py
+++ b/sickbeard/tv.py
@@ -1168,7 +1168,7 @@ def saveToDB(self, forceSave=False):
 
         helpers.update_anime_support()
 
-        if self.imdbid and self.imdb_info['year']:
+        if self.imdbid and self.imdb_info.get('year'):
             controlValueDict = {"indexer_id": self.indexerid}
             newValueDict = self.imdb_info
 


Test patch: