torrentbytes.py 7.9 KB
Newer Older
1
2
3
4
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# Author: echel0n <[email protected]>
# URL: http://www.github.com/sickragetv/sickrage/
root's avatar
root committed
5
#
6
# This file is part of SickRage.
root's avatar
root committed
7
8
9
10
11
12
13
14
15
#
# SickRage is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# SickRage is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
# GNU General Public License for more details.
root's avatar
root committed
17
18
19
20
#
# You should have received a copy of the GNU General Public License
# along with SickRage.  If not, see <http://www.gnu.org/licenses/>.

21
22
from __future__ import unicode_literals

root's avatar
root committed
23
import re
24
import traceback
25
import urllib
26

27
import sickrage
28
29
30
from sickrage.core.caches import tv_cache
from sickrage.core.helpers import bs4_parser
from sickrage.providers import TorrentProvider
root's avatar
root committed
31
32


33
class TorrentBytesProvider(TorrentProvider):
root's avatar
root committed
34
35
    def __init__(self):

36
        super(TorrentBytesProvider, self).__init__("TorrentBytes", 'www.torrentbytes.net')
root's avatar
root committed
37
38
39
40
41
42
43
44

        self.supportsBacklog = True

        self.username = None
        self.password = None
        self.ratio = None
        self.minseed = None
        self.minleech = None
45
        self.freeleech = False
root's avatar
root committed
46

47
        self.urls.update({
echel0n's avatar
echel0n committed
48
49
50
51
            'login': '{base_url}/takelogin.php'.format(base_url=self.urls['base_url']),
            'detail': '{base_url}/details.php?id=%s'.format(base_url=self.urls['base_url']),
            'search': '{base_url}/browse.php?search=%s%s'.format(base_url=self.urls['base_url']),
            'download': '{base_url}/download.php?id=%s&name=%s'.format(base_url=self.urls['base_url'])
52
        })
root's avatar
root committed
53
54
55

        self.categories = "&c41=1&c33=1&c38=1&c32=1&c37=1"

Dustyn Gibson's avatar
Dustyn Gibson committed
56
57
58
59
        self.proper_strings = ['PROPER', 'REPACK']

        self.cache = TorrentBytesCache(self)

root's avatar
root committed
60
61
62
63
    def _doLogin(self):

        login_params = {'username': self.username,
                        'password': self.password,
64
                        'login': 'Log in!'}
root's avatar
root committed
65

66
67
68
        try:
            response = self.session.post(self.urls['login'], data=login_params, timeout=30).content
        except Exception:
echel0n's avatar
echel0n committed
69
            sickrage.srLogger.warning("[{}]: Unable to connect to provider".format(self.name))
root's avatar
root committed
70
71
            return False

72
        if re.search('Username or password incorrect', response):
echel0n's avatar
echel0n committed
73
            sickrage.srLogger.warning("[{}]: Invalid username or password. Check your settings".format(self.name))
root's avatar
root committed
74
75
76
77
            return False

        return True

78
    def search(self, search_params, search_mode='eponly', epcount=0, age=0, epObj=None):
root's avatar
root committed
79
80
81
82
83

        results = []
        items = {'Season': [], 'Episode': [], 'RSS': []}

        if not self._doLogin():
84
            return results
root's avatar
root committed
85
86

        for mode in search_params.keys():
87
            sickrage.srLogger.debug("Search Mode: %s" % mode)
root's avatar
root committed
88
89
            for search_string in search_params[mode]:

90
                if mode is not 'RSS':
91
                    sickrage.srLogger.debug("Search string: %s " % search_string)
root's avatar
root committed
92

93
                searchURL = self.urls['search'] % (urllib.quote(search_string), self.categories)
94
                sickrage.srLogger.debug("Search URL: %s" % searchURL)
root's avatar
root committed
95

96
97
98
                try:
                    data = self.session.get(searchURL).content
                except Exception:
root's avatar
root committed
99
100
101
                    continue

                try:
102
                    with bs4_parser(data) as html:
labrys's avatar
labrys committed
103
                        # Continue only if one Release is found
104
105
                        empty = html.find('Nothing found!')
                        if empty:
106
                            sickrage.srLogger.debug("Data returned from provider does not contain any torrents")
root's avatar
root committed
107
108
                            continue

109
110
111
                        torrent_table = html.find('table', attrs={'border': '1'})
                        torrent_rows = torrent_table.find_all('tr') if torrent_table else []

112
113
                        for result in torrent_rows[1:]:
                            cells = result.find_all('td')
114
                            size = None
115
116
                            link = cells[1].find('a', attrs={'class': 'index'})

117
                            full_id = link['href'].replace('details.php?id=', '')
118
                            torrent_id = full_id.split("&")[0]
119

labrys's avatar
labrys committed
120
                            # Free leech torrents are marked with green [F L] in the title (i.e. <font color=green>[F&nbsp;L]</font>)
121
                            freeleechTag = cells[1].find('font', attrs={'color': 'green'})
122
                            if freeleechTag and freeleechTag.text == '[F&nbsp;L]':
123
124
125
126
127
128
129
                                isFreeleechTorrent = True
                            else:
                                isFreeleechTorrent = False

                            if self.freeleech and not isFreeleechTorrent:
                                continue

130
131
132
133
134
                            try:
                                if link.has_key('title'):
                                    title = cells[1].find('a', {'class': 'index'})['title']
                                else:
                                    title = link.contents[0]
135
                                download_url = self.urls['download'] % (torrent_id, link.contents[0])
136
                                seeders = int(cells[8].find('span').contents[0])
137
138
                                leechers = int(cells[9].find('span').contents[0])

139
140
141
142
143
144
                                # Need size for failed downloads handling
                                if size is None:
                                    if re.match(r'[0-9]+,?\.?[0-9]*[KkMmGg]+[Bb]+', cells[6].text):
                                        size = self._convertSize(cells[6].text)
                                        if not size:
                                            size = -1
145

146
147
148
                            except (AttributeError, TypeError):
                                continue

fernandog's avatar
fernandog committed
149
                            if not all([title, download_url]):
150
                                continue
fernandog's avatar
fernandog committed
151

labrys's avatar
labrys committed
152
                            # Filter unseeded torrent
fernandog's avatar
fernandog committed
153
                            if seeders < self.minseed or leechers < self.minleech:
154
                                if mode is not 'RSS':
155
                                    sickrage.srLogger.debug(
156
157
                                        "Discarding torrent because it doesn't meet the minimum seeders or leechers: {} (S:{} L:{})".format(
                                            title, seeders, leechers))
158
                                continue
root's avatar
root committed
159

fernandog's avatar
fernandog committed
160
                            item = title, download_url, size, seeders, leechers
161
                            if mode is not 'RSS':
162
                                sickrage.srLogger.debug("Found result: %s " % title)
root's avatar
root committed
163

164
                            items[mode].append(item)
root's avatar
root committed
165

166
                except Exception as e:
167
                    sickrage.srLogger.error("Failed parsing provider. Traceback: %s" % traceback.format_exc())
root's avatar
root committed
168

labrys's avatar
labrys committed
169
            # For each search mode sort all the items by seeders if available
root's avatar
root committed
170
171
172
173
174
175
176
177
            items[mode].sort(key=lambda tup: tup[3], reverse=True)

            results += items[mode]

        return results

    def seedRatio(self):
        return self.ratio
178

179
180
    @staticmethod
    def _convertSize(sizeString):
181
182
183
184
        size = sizeString[:-2]
        modifier = sizeString[-2:]
        size = float(size)
        if modifier in 'KB':
185
            size *= 1024
186
        elif modifier in 'MB':
187
            size *= 1024 ** 2
188
        elif modifier in 'GB':
189
            size *= 1024 ** 3
190
        elif modifier in 'TB':
191
            size *= 1024 ** 4
192
        return int(size)
root's avatar
root committed
193
194


195
class TorrentBytesCache(tv_cache.TVCache):
196
    def __init__(self, provider_obj):
197
        tv_cache.TVCache.__init__(self, provider_obj)
root's avatar
root committed
198
199
200
201

        # only poll TorrentBytes every 20 minutes max
        self.minTime = 20

202
    def _getRSSData(self):
root's avatar
root committed
203
        search_params = {'RSS': ['']}
204
        return {'entries': self.provider.search(search_params)}