flexget.plugins.input.tvtorrents
Covered: 27 lines
Missed: 41 lines
Skipped 22 lines
Percent: 39 %
 1
import urlparse
 2
import logging
 3
from flexget.entry import Entry
 4
from flexget.plugin import *
 5
from flexget.utils.soup import get_soup
 6
from flexget.utils.cached_input import cached
 7
from flexget.utils.tools import urlopener
 9
log = logging.getLogger('tvtorrents')
12
class InputTVTorrents(object):
13
    """
14
        A customized HTML input plugin. Parses out full torrent URLs from
15
        TVTorrents' page for Recently Aired TV shows.
17
        A bit fragile right now, because it depends heavily on the exact
18
        structure of the HTML.
20
        Just set tvt: true in your config, and provide the path to your login
21
        cookie by using the cookies plugin.
23
        Note: Of yourse, you need to configure patterns filter to match only
24
        desired content. The series filter does NOT appear to work well with
25
        this plugin yet - just use a pattern like (lost|csi).*?720p until we
26
        figure out why.
28
        Plugin-specific code by Fredrik Braenstroem.
29
    """
31
    def validator(self):
32
        from flexget import validator
33
        return validator.factory('url')
35
    @cached('tvt')
36
    @internet(log)
37
    def on_feed_input(self, feed):
38
        pageurl = "http://tvtorrents.com/loggedin/recently_aired.do"
39
        log.debug("InputPlugin tvtorrents requesting url %s" % pageurl)
41
        page = urlopener(pageurl, log)
42
        soup = get_soup(page)
44
        hscript = soup.find('script', src=None).contents[0]
45
        hlines = hscript.splitlines()
46
        hash = hlines[15].strip().split("'")[1]
47
        digest = hlines[16].strip().split("'")[1]
48
        hurl = hlines[17].strip().split("'")
49
        hashurl = hurl[1] + "%s" + hurl[3] + digest + hurl[5] + hash
51
        for link in soup.findAll('a'):
52
            if not 'href' in link:
53
                continue
54
            url = link['href']
55
            title = link.contents[0]
57
            if link.has_key('onclick') and link['onclick'].find("loadTorrent") != -1:
58
                infohash = link['onclick'].split("'")[1]
59
                td = link.parent.parent.contents[4]
60
                sname = td.contents[0].strip()
61
                epi = td.contents[2].contents[0].strip()
62
                title = "%s - %s" % (sname, epi)
63
                url = hashurl % (infohash,)
64
            else:
65
                continue
66
            if title is None:
67
                continue
69
            title = title.strip()
70
            if not title:
71
                continue
74
            if url.startswith('//'):
75
                url = "http:" + url
76
            elif not url.startswith('http://') or not url.startswith('https://'):
77
                url = urlparse.urljoin(pageurl, url)
80
            if title.lower().find('.torrent') > 0:
81
                title = title[:title.lower().find(".torrent")]
83
            entry = Entry()
84
            entry['url'] = url
85
            entry['title'] = title
87
            feed.entries.append(entry)
89
register_plugin(InputTVTorrents, 'tvt')