Repository: pymedusa/Medusa

Issue:
[APP SUBMITTED]: No JSON object could be decoded
### INFO

Python Version: **2.7.6 (default, Jun 22 2015, 18:00:18) [GCC 4.8.2]**
Operating System: **Linux-3.19.0-56-generic-i686-with-Ubuntu-14.04-trusty**
Locale: UTF-8
Branch: **develop**
Commit: PyMedusa/SickRage@3351698fd0d3b554f0dd6bbd8d0cbee0b60ce128
Link to Log: https://gist.github.com/0f9bcafda8153bec981bf232c004d72e
### ERROR

```
SEARCHQUEUE-MANUAL-311937 :: [TorrentDay] :: [3351698] Unknown exception while searching TorrentDay. Error: 'No JSON object could be decoded'
Traceback (most recent call last):
  File "/home/home/SickRage/sickbeard/search.py", line 547, in searchProviders
    forced_search, downCurQuality, manual_search, manual_search_type)
  File "/home/home/SickRage/sickrage/providers/GenericProvider.py", line 149, in find_search_results
    items_list += self.search(search_string, ep_obj=episode)
  File "/home/home/SickRage/sickbeard/providers/torrentday.py", line 123, in search
    parsedJSON = self.get_url(self.urls['search'], post_data=post_data, returns='json')
  File "/home/home/SickRage/sickrage/providers/GenericProvider.py", line 356, in get_url
    return getURL(url, post_data, params, self.headers, timeout, self.session, **kwargs)
  File "/home/home/SickRage/sickbeard/helpers.py", line 1472, in getURL
    return resp if response_type == u'response' or response_type is None else resp.json() if response_type == u'json' else getattr(resp, response_type, resp)
  File "/home/home/SickRage/lib/requests/models.py", line 808, in json
    return complexjson.loads(self.text, **kwargs)
  File "/home/home/SickRage/lib/simplejson/__init__.py", line 307, in loads
    return _default_decoder.decode(s)
  File "/home/home/SickRage/lib/simplejson/decoder.py", line 335, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/home/home/SickRage/lib/simplejson/decoder.py", line 353, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
```

---

_STAFF NOTIFIED_: @pymedusa/support @pymedusa/moderators



Discussion:

Resolving patch:
diff --git a/sickbeard/providers/torrentday.py b/sickbeard/providers/torrentday.py
--- a/sickbeard/providers/torrentday.py
+++ b/sickbeard/providers/torrentday.py
@@ -20,6 +20,7 @@
 
 import re
 from requests.compat import urljoin
+from requests.exceptions import RequestException
 from requests.utils import add_dict_to_cookiejar, dict_from_cookiejar
 
 from sickbeard import logger, tvcache
@@ -120,19 +121,25 @@ def search(self, search_params, age=0, ep_obj=None):  # pylint: disable=too-many
                 if self.freeleech:
                     post_data.update({'free': 'on'})
 
-                parsedJSON = self.get_url(self.urls['search'], post_data=post_data, returns='json')
-                if not parsedJSON:
-                    logger.log(u"No data returned from provider", logger.DEBUG)
+                try:
+                    response = self.get_url(self.urls['search'], post_data=post_data)
+                    response.raise_for_status()
+                except RequestException as msg:
+                    logger.log(u'Error while connecting to provider: {error}'.format(error=msg), logger.ERROR)
                     continue
 
                 try:
-                    torrents = parsedJSON.get('Fs', [])[0].get('Cn', {}).get('torrents', [])
-                except Exception:
+                    jdata = response.json()
+                except ValueError:  # also catches JSONDecodeError if simplejson is installed
+                    logger.log(u"Data returned from provider is not json", logger.ERROR)
+                    continue
+
+                torrents = jdata.get('Fs', [dict()])[0].get('Cn', {}).get('torrents', [])
+                if not torrents:
                     logger.log(u"Data returned from provider does not contain any torrents", logger.DEBUG)
                     continue
 
                 for torrent in torrents:
-
                     title = re.sub(r"\[.*\=.*\].*\[/.*\]", "", torrent['name']) if torrent['name'] else None
                     download_url = urljoin(self.urls['download'], '{}/{}'.format(torrent['id'], torrent['fname'])) if torrent['id'] and torrent['fname'] else None
 


Test patch: