from __future__ import unicode_literals, division, absolute_import
import logging
import urllib

from flexget import plugin
from flexget.entry import Entry
from flexget.event import event
from flexget.utils import requests
from flexget.utils.search import torrent_availability, normalize_unicode

log = logging.getLogger('yts')


class UrlRewriteYTS(object):
    """YTS search"""

    schema = {
        'type': 'boolean'
    }

    def search(self, task, entry, config=None):
        entries = set()
        search_strings = [normalize_unicode(s) for s in entry.get('search_strings', [entry['title']])]
        for search_string in search_strings:
            url = 'https://yts.to/api/v2/list_movies.json?query_term=%s' % (
                urllib.quote(search_string.encode('utf-8')))

            log.debug('requesting: %s' % url)

            try:
                result = requests.get(url)
                try:
                    data = result.json()
                except ValueError:
                    log.debug('Could not decode json from response: %s', result.text)
                    raise plugin.PluginError('Error getting result from yts.')
            except requests.RequestException as e:
                raise plugin.PluginError('Could not retrieve query from yts (%s)' % e.args[0])
            if not data['status'] == 'ok':
                raise plugin.PluginError('failed to query YTS')

            for item in data['data']['movies']:
                for torrent in item['torrents']:
                    entry = Entry()
                    entry['title'] = item['title']
                    entry['year'] = item['year']
                    entry['url'] = torrent['url']
                    entry['content_size'] = torrent['size']
                    entry['torrent_seeds'] = torrent['seeds']
                    entry['torrent_leeches'] = torrent['peers']
                    entry['torrent_info_hash'] = torrent['hash']
                    entry['search_sort'] = torrent_availability(entry['torrent_seeds'], entry['torrent_leeches'])
                    entry['quality'] = torrent['quality']
                    entry['imdb_id'] = item['imdb_code']
                    entries.add(entry)

        log.debug('Search got %d results' % len(entries))
        return entries


@event('plugin.register')
def register_plugin():
    plugin.register(UrlRewriteYTS, 'yts', groups=['search'], api_ver=2)

@@ -1448,24 +1448,49 @@
 %5B'movies'%5D:%0A
+                try:%0A    
             
@@ -1542,24 +1542,28 @@
             
+  
+  
 entry = Entr
@@ -1566,16 +1566,20 @@
 Entry()%0A
+    
         
@@ -1637,24 +1637,28 @@
             
+    
 entry%5B'year'
@@ -1690,24 +1690,28 @@
             
+   
+ 
 entry%5B'url'%5D
@@ -1728,16 +1728,20 @@
 %5B'url'%5D%0A
+    
         
@@ -1804,32 +1804,36 @@
                 
+  
+  
 entry%5B'torrent_s
@@ -1854,24 +1854,28 @@
 nt%5B'seeds'%5D%0A
+    
             
@@ -1938,32 +1938,36 @@
                 
+    
 entry%5B'torrent_i
@@ -1995,16 +1995,20 @@
 'hash'%5D%0A
+    
         
@@ -2129,24 +2129,28 @@
             
+    
 entry%5B'quali
@@ -2191,24 +2191,28 @@
             
+  
+  
 entry%5B'imdb_
@@ -2260,25 +2260,168 @@
     
-entries.add(entry
+    if entry.isvalid():%0A                            entries.add(entry)%0A                except:%0A                    log.debug('invalid return structure from YTS'
 )%0A%0A 

Update search_yts.py