Repository: Flexget/Flexget

Issue:
Pirate Bay Plugin: Error searching with piratebay: Expecting value: line 1 column 1 (char 0)
When searching with the Pirate Bay plugin for Movies I get an error every time. I didn't change anything in my Config. It used to search just fine. Been getting this error since Version 3.1.75 or .76. 


#### Config:
```yaml
download-movies: 
    priority: 4   
    imdb_lookup: yes  # can also use imdb_lookup or tmdb_lookup
    discover:
      what:
        - movie_list: watchlist
      from:
        - piratebay:
            sort_by: seeds
            category: video
            url: https://www1.thepiratebay3.to/
        - rarbg:
            category: [48, 44, 14, 45, 17]
            sorted_by: seeders
      interval: 30 minutes        
```
  
#### Log:
<details>
 <summary>(click to expand)</summary>

```
2020-10-18 13:42:42 VERBOSE  discover      download-movies Searching for `The Net (2016)` with plugin `piratebay` (1 of 3)
2020-10-18 13:42:43 ERROR    discover      download-movies Error searching with piratebay: Expecting value: line 1 column 1 (char 0)
2020-10-18 13:42:43 VERBOSE  discover      download-movies Searching for `The Net (2016)` with plugin `rarbg` (1 of 3)
2020-10-18 13:42:46 VERBOSE  discover      download-movies No search results for `The Net (2016)`
2020-10-18 13:42:46 VERBOSE  discover      download-movies Searching for `Angelo (2019)` with plugin `piratebay` (2 of 3)
2020-10-18 13:42:46 ERROR    discover      download-movies Error searching with piratebay: Expecting value: line 1 column 1 (char 0)
2020-10-18 13:42:46 VERBOSE  discover      download-movies Searching for `Angelo (2019)` with plugin `rarbg` (2 of 3)
```
</details>

### Additional information:

- FlexGet version: 3.1.77
- Python version:
- Installation method: 
- Using daemon (yes/no): yes
- OS and version: DietPi rasperryPi Debian


Discussion:
This issue is stale because it has been open 150 days with no activity. Remove stale label or comment or this will be closed in 30 days.

Resolving patch:
diff --git a/flexget/components/sites/sites/piratebay.py b/flexget/components/sites/sites/piratebay.py
--- a/flexget/components/sites/sites/piratebay.py
+++ b/flexget/components/sites/sites/piratebay.py
@@ -1,11 +1,12 @@
 import re
 from urllib.parse import urlencode, urlparse
+from json import JSONDecodeError
 
 from loguru import logger
 
 from flexget import plugin
 from flexget.components.sites.urlrewriting import UrlRewritingError
-from flexget.components.sites.utils import normalize_unicode, torrent_availability
+from flexget.components.sites.utils import torrent_availability
 from flexget.entry import Entry
 from flexget.event import event
 
@@ -108,23 +109,23 @@ def url_rewritable(self, task, entry):
     # urlrewriter API
     def url_rewrite(self, task, entry):
         if 'url' not in entry:
-            logger.error("Didn't actually get a URL...")
+            logger.error('Didn\'t actually get a URL...')
         else:
             logger.debug('Got the URL: {}', entry['url'])
         if self.url_search.match(entry['url']):
             # use search
             results = self.search(task, entry)
             if not results:
-                raise UrlRewritingError("No search results found")
+                raise UrlRewritingError('No search results found')
             # TODO: Close matching was taken out of search methods, this may need to be fixed to be more picky
             entry['url'] = results[0]['url']
         else:
             torrent_id = self.url_match.match(entry['url']).group(1)
-            url = f"{self.url}/t.php?id={torrent_id}"
+            url = f'{self.url}/t.php?id={torrent_id}'
             logger.debug('Getting info for torrent ID {}', torrent_id)
             json_result = task.requests.get(url).json()
             if json_result['id'] == '0':
-                raise UrlRewritingError(f"Torrent with ID {torrent_id} does not exist.")
+                raise UrlRewritingError(f'Torrent with ID {torrent_id} does not exist.')
             entry['url'] = self.info_hash_to_magnet(json_result['info_hash'], json_result['name'])
 
     @plugin.internet(logger)
@@ -146,10 +147,16 @@ def search(self, task, entry, config=None):
         for search_string in entry.get('search_strings', [entry['title']]):
             # query = normalize_unicode(search_string)
             params = {'q': search_string, 'cat': category}
-            url = f"{self.url}/q.php"
-            json_results = task.requests.get(url, params=params).json()
+            url = f'{self.url}/q.php'
+            try:
+                json_results = task.requests.get(url, params=params).json()
+            except JSONDecodeError as e:
+                raise plugin.PluginError(
+                    f'Error while searching piratebay for {search_string}, invalid json response',
+                )
+
             if not json_results:
-                raise plugin.PluginError("Error while searching piratebay for %s.", search_string)
+                raise plugin.PluginError(f'Error while searching piratebay for {search_string}.')
             for result in json_results:
                 if result['id'] == '0':
                     # JSON when no results found: [{ "id":"0", "name":"No results returned", ... }]
@@ -162,9 +169,9 @@ def search(self, task, entry, config=None):
     # convert an info_hash string to a magnet uri
     @staticmethod
     def info_hash_to_magnet(info_hash: str, name: str) -> str:
-        magnet = {'xt': f"urn:btih:{info_hash}", 'dn': name, 'tr': TRACKERS}
+        magnet = {'xt': f'urn:btih:{info_hash}', 'dn': name, 'tr': TRACKERS}
         magnet_qs = urlencode(magnet, doseq=True, safe=':')
-        magnet_uri = f"magnet:?{magnet_qs}"
+        magnet_uri = f'magnet:?{magnet_qs}'
         return magnet_uri
 
     # convert a single json result to an Entry


Test patch: