import urllib2
import logging
import zlib
from BeautifulSoup import NavigableString
from flexget.entry import Entry
from flexget.plugin import register_plugin, internet, get_plugin_by_name, PluginError
from flexget.utils.log import log_once
from flexget.utils.soup import get_soup
from flexget.utils.cached_input import cached
from flexget.utils.tools import urlopener

log = logging.getLogger('rlslog')


class RlsLog(object):
    """
    Adds support for rlslog.net as a feed.
    """

    def validator(self):
        from flexget import validator
        return validator.factory('url')

    def parse_rlslog(self, rlslog_url, feed):
        """
        :param rlslog_url: Url to parse from
        :param feed: Feed instance
        :return: List of release dictionaries
        """

        page = urlopener(rlslog_url, log)
        if page.headers.get('content-encoding') in ('gzip', 'x-gzip', 'deflate'):
            page = zlib.decompressobj(15 + 32).decompress(page.read())
        soup = get_soup(page)

        releases = []
        for entry in soup.findAll('div', attrs={'class': 'entry'}):
            release = {}
            h3 = entry.find('h3', attrs={'class': 'entrytitle'})
            if not h3:
                log.debug('FAIL: No h3 entrytitle')
                continue
            release['title'] = h3.a.contents[0].strip()
            entrybody = entry.find('div', attrs={'class': 'entrybody'})
            if not entrybody:
                log.debug('FAIL: No entrybody')
                continue

            log.trace('Processing title %s' % (release['title']))

            for link in entrybody.findAll('a'):
                if not link.contents:
                    log.trace('link content empty, skipping')
                    continue
                if not link.has_key('href'):
                    log.trace('link %s missing href' % link)
                    continue

                link_name = link.contents[0]
                link_name_ok = True
                if link_name is None:
                    log.trace('link_name is none')
                    link_name_ok = False
                if not isinstance(link_name, NavigableString):
                    log.trace('link_name is not NavigableString')
                    link_name_ok = False

                link_href = link['href']

                # parse imdb link
                if link_name_ok:
                    link_name = link_name.strip().lower()
                    if link_name == 'imdb':
                        release['imdb_url'] = link_href

                # test if entry with this url would be recognized
                temp = {'title': release['title'], 'url': link_href}
                urlrewriting = get_plugin_by_name('urlrewriting')
                if urlrewriting['instance'].url_rewritable(feed, temp):
                    release['url'] = link_href
                    log.trace('--> accepting %s (known url pattern)' % link_href)
                else:
                    log.trace('<-- ignoring %s (unknown url pattern)' % link_href)

            # reject if no torrent link
            if not 'url' in release:
                log_once('%s skipped due to missing or unsupported download link' % (release['title']), log)
            else:
                releases.append(release)

        return releases

    @cached('rlslog')
    @internet(log)
    def on_feed_input(self, feed, config):
        url = config
        if url.endswith('feed/'):
            raise PluginError('Invalid URL. Remove trailing feed/ from the url.')

        releases = []
        entries = []

        # retry rlslog (badly responding) up to 6 times (our urlopener tries 3 times per each of our tries here)
        for number in range(2):
            try:
                releases = self.parse_rlslog(url, feed)
            except urllib2.HTTPError, e:
                if number == 1:
                    raise
                else:
                    import time
                    log.verbose('Error recieving content, retrying in 5s. Try [%s of 3]. HTTP Error Code: %s' % (str(number + 1), str(e.code)))
                    time.sleep(5)
            except urllib2.URLError, e:
                if number == 1:
                    raise
                else:
                    import time
                    log.verbose('Error retrieving the URL, retrying in 5s. Try [%s of 3]. Error: %s' % (str(number + 1), str(e.reason)))
                    time.sleep(5)
            else:
                break

        # Construct entry from release
        for release in releases:
            entry = Entry()

            def apply_field(d_from, d_to, f):
                if f in d_from:
                    if d_from[f] is None:
                        return # None values are not wanted!
                    d_to[f] = d_from[f]

            for field in ('title', 'url', 'imdb_url'):
                apply_field(release, entry, field)

            entries.append(entry)

        return entries

register_plugin(RlsLog, 'rlslog', api_ver=2)

@@ -4,23 +4,23 @@
 ort 
-urllib2
+logging
 %0Aimport 
 logg
@@ -19,27 +19,50 @@
 ort 
-logging%0Aimport zlib
+time%0Afrom requests import RequestException
 %0Afro
@@ -346,50 +346,8 @@
 ched
-%0Afrom flexget.utils.tools import urlopener
 %0A%0Alo
@@ -775,223 +775,193 @@
     
-page = urlopener(rlslog_url, log)%0A        if page.headers.get('content-encoding') in ('gzip', 'x-gzip', 'deflate'):%0A            page = zlib.decompressobj(15 + 32).decompress(page.read())%0A        soup = get_soup(page
+# BeautifulSoup doesn't seem to work if data is already decoded to unicode :/%0A        soup = get_soup(feed.requests.get(rlslog_url, timeout=25, config=%7B'decode_unicode': False%7D).content
 )%0A%0A 
@@ -3598,17 +3598,17 @@
  to 
-6
+4
  times (
 our 
@@ -3607,29 +3607,24 @@
 es (
-our urlopener
+requests
  tries 
-3
+2
  tim
@@ -3759,16 +3759,38 @@
 , feed)%0A
+                break%0A
         
@@ -3804,25 +3804,24 @@
 ept 
-urllib2.HTTPError
+RequestException
 , e:
@@ -3905,40 +3905,8 @@
 se:%0A
-                    import time%0A
     
@@ -3943,18 +3943,18 @@
 rror rec
-i
 e
+i
 ving con
@@ -3990,340 +3990,9 @@
  of 
-3%5D. HTTP Error Code: %25s' %25 (str(number + 1), str(e.code)))%0A                    time.sleep(5)%0A            except urllib2.URLError, e:%0A                if number == 1:%0A                    raise%0A                else:%0A                    import time%0A                    log.verbose('Error retrieving the URL, retrying in 5s. Try %5B%25s of 3
+2
 %5D. E
@@ -4007,12 +4007,8 @@
 ' %25 
-(str
 (num
@@ -4018,24 +4018,11 @@
  + 1
-)
 , 
-str(e.reason)
+e
 ))%0A 
@@ -4057,48 +4057,8 @@
 p(5)
-%0A            else:%0A                break
 %0A%0A  

Convert rlslog to requests.