from __future__ import unicode_literals, division, absolute_import
from nose.tools import assert_true
from flexget.plugin import get_plugin_by_name


class TestURLRewriters(object):
    """
        Bad example, does things manually, you should use task.find_entry to check existance
    """

    config = """
        tasks:
          test:
            # make test data
            mock:
              - {title: 'tpb page', url: 'http://thepiratebay.org/tor/8492471/Test.avi'}
              - {title: 'tbp search', url: 'http://thepiratebay.com/search/something'}
              - {title: 'tbp torrent', url: 'http://torrents.thepiratebay.se/8492471/Test.torrent'}
              - {title: 'tbp torrent subdomain', url: 'http://torrents.thepiratebay.se/8492471/Test.avi'}
              - {title: 'tbp torrent bad subdomain', url: 'http://torrent.thepiratebay.se/8492471/Test.avi'}
              - {title: 'nyaa', url: 'http://www.nyaa.eu/?page=torrentinfo&tid=12345'}
              - {title: 'isohunt search', url: 'http://isohunt.com/torrents/?ihq=Query.Here'}
              - {title: 'isohunt direct', url: 'http://isohunt.com/torrent_details/123456789/Name.Here'}
              - {title: 'cinemageddon download', url: 'http://cinemageddon.net/details.php?id=1234'}
    """

    def setup(self):
        FlexGetBase.setup(self)
        task = execute_task('test')

    def get_urlrewriter(self, name):
        info = get_plugin_by_name(name)
        return info.instance

    def test_piratebay(self, execute_task):
        # test with piratebay entry
        urlrewriter = self.get_urlrewriter('piratebay')
        entry = task.find_entry(title='tpb page')
        assert_true(urlrewriter.url_rewritable(task, entry))
        entry = task.find_entry(title='tbp torrent')
        assert not urlrewriter.url_rewritable(task, entry), \
            'TPB direct torrent link should not be url_rewritable'
        entry = task.find_entry(title='tbp torrent subdomain')
        assert_true(urlrewriter.url_rewritable(task, entry))
        entry = task.find_entry(title='tbp torrent bad subdomain')
        assert not urlrewriter.url_rewritable(task, entry), \
            'TPB link with invalid subdomain should not be url_rewritable'

    def test_piratebay_search(self, execute_task):
        # test with piratebay entry
        urlrewriter = self.get_urlrewriter('piratebay')
        entry = task.find_entry(title='tbp search')
        assert_true(urlrewriter.url_rewritable(task, entry))

    def test_nyaa_torrents(self, execute_task):
        entry = task.find_entry(title='nyaa')
        urlrewriter = self.get_urlrewriter('nyaa')
        assert entry['url'] == 'http://www.nyaa.eu/?page=torrentinfo&tid=12345'
        assert_true(urlrewriter.url_rewritable(task, entry))
        urlrewriter.url_rewrite(task, entry)
        assert entry['url'] == 'http://www.nyaa.eu/?page=download&tid=12345'

    def test_isohunt(self, execute_task):
        entry = task.find_entry(title='isohunt search')
        urlrewriter = self.get_urlrewriter('isohunt')
        assert not urlrewriter.url_rewritable(task, entry), \
            'search entry should not be url_rewritable'
        entry = task.find_entry(title='isohunt direct')
        assert urlrewriter.url_rewritable(task, entry), \
            'direct entry should be url_rewritable'

    def test_cinemageddon(self, execute_task):
        entry = task.find_entry(title='cinemageddon download')
        urlrewriter = self.get_urlrewriter('cinemageddon')
        assert urlrewriter.url_rewritable(task, entry)
        urlrewriter.url_rewrite(task, entry)
        assert entry['url'] == 'http://cinemageddon.net/download.php?id=1234&name=cinemageddon%20download.torrent'


class TestRegexpurlrewriter(object):
    # TODO: this test is broken?

    config = """
        tasks:
          test:
            mock:
              - {title: 'irrelevant', url: 'http://newzleech.com/?p=123'}
            accept_all: yes
            urlrewrite:
              newzleech:
                regexp: 'http://newzleech.com/\?p=(?P<id>\d+)'
                format: 'http://newzleech.com/?m=gen&dl=1&post=\g<id>'
    """

    def test_newzleech(self, execute_task):
        task = execute_task('test')
        assert task.find_entry(url='http://newzleech.com/?m=gen&dl=1&post=123'), \
            'did not url_rewrite properly'

@@ -1271,98 +1271,8 @@
 %22%22%0A%0A
-    def setup(self):%0A        FlexGetBase.setup(self)%0A        task = execute_task('test')%0A%0A
     
@@ -1304,16 +1304,16 @@
  name):%0A
+
         
@@ -1410,32 +1410,68 @@
  execute_task):%0A
+        task = execute_task('test')%0A
         # test w
@@ -2211,32 +2211,68 @@
  execute_task):%0A
+        task = execute_task('test')%0A
         # test w
@@ -2501,32 +2501,68 @@
  execute_task):%0A
+        task = execute_task('test')%0A
         entry = 
@@ -2940,32 +2940,68 @@
  execute_task):%0A
+        task = execute_task('test')%0A
         entry = 
@@ -3371,32 +3371,32 @@
 rl_rewritable'%0A%0A
-
     def test_cin
@@ -3418,32 +3418,68 @@
  execute_task):%0A
+        task = execute_task('test')%0A
         entry = 

fix test_urlrewriting