alpharatio.py 5.85 KB
Newer Older
1
# Author: echel0n <[email protected]>
echel0n's avatar
echel0n committed
2
# URL: https://sickrage.ca
BillNasty's avatar
BillNasty committed
3
#
4
# This file is part of SickRage.
BillNasty's avatar
BillNasty committed
5
6
7
8
9
10
11
12
#
# 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
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
BillNasty's avatar
BillNasty committed
14
15
16
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
17
18
19
# along with SickRage.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import unicode_literals
BillNasty's avatar
BillNasty committed
20
21
22

import re

23
import sickrage
echel0n's avatar
echel0n committed
24
from sickrage.core.caches.tv_cache import TVCache
25
from sickrage.core.helpers import bs4_parser, try_int
26
from sickrage.providers import TorrentProvider
BillNasty's avatar
BillNasty committed
27

28

29
class AlphaRatioProvider(TorrentProvider):
BillNasty's avatar
BillNasty committed
30
    def __init__(self):
echel0n's avatar
echel0n committed
31
        super(AlphaRatioProvider, self).__init__("AlphaRatio", 'http://alpharatio.cc', True)
BillNasty's avatar
BillNasty committed
32
33
        self.username = None
        self.password = None
echel0n's avatar
echel0n committed
34

BillNasty's avatar
BillNasty committed
35
36
37
        self.minseed = None
        self.minleech = None

38
        self.urls.update({
echel0n's avatar
echel0n committed
39
40
41
42
            'login': '{base_url}/login.php'.format(**self.urls),
            'detail': '{base_url}/torrents.php?torrentid=%s'.format(**self.urls),
            'search': '{base_url}/torrents.php?searchstr=%s%s'.format(**self.urls),
            'download': '{base_url}/%s'.format(**self.urls)
43
        })
Dustyn Gibson's avatar
Dustyn Gibson committed
44

BillNasty's avatar
BillNasty committed
45
46
        self.catagories = "&filter_cat[1]=1&filter_cat[2]=1&filter_cat[3]=1&filter_cat[4]=1&filter_cat[5]=1"

Dustyn Gibson's avatar
Dustyn Gibson committed
47
48
        self.proper_strings = ['PROPER', 'REPACK']

echel0n's avatar
echel0n committed
49
        self.cache = TVCache(self, min_time=20)
BillNasty's avatar
BillNasty committed
50

echel0n's avatar
echel0n committed
51
    def login(self):
BillNasty's avatar
BillNasty committed
52
53
54
        login_params = {'username': self.username,
                        'password': self.password,
                        'remember_me': 'on',
55
                        'login': 'submit'}
BillNasty's avatar
BillNasty committed
56

57
        try:
58
            response = sickrage.srCore.srWebSession.post(self.urls['login'], data=login_params, timeout=30).text
59
        except Exception:
60
            sickrage.srCore.srLogger.warning("[{}]: Unable to connect to provider".format(self.name))
BillNasty's avatar
BillNasty committed
61
62
            return False

63
64
        if re.search('Invalid Username/password', response) \
                or re.search('<title>Login :: AlphaRatio.cc</title>', response):
echel0n's avatar
echel0n committed
65
66
            sickrage.srCore.srLogger.warning(
                "[{}]: Invalid username or password. Check your settings".format(self.name))
BillNasty's avatar
BillNasty committed
67
68
69
70
            return False

        return True

71
    def search(self, search_strings, age=0, ep_obj=None):
BillNasty's avatar
BillNasty committed
72
73
        results = []

echel0n's avatar
echel0n committed
74
        if not self.login():
BillNasty's avatar
BillNasty committed
75
76
            return results

fernandog's avatar
fernandog committed
77
        for mode in search_strings.keys():
78
            sickrage.srCore.srLogger.debug("Search Mode: %s" % mode)
fernandog's avatar
fernandog committed
79
            for search_string in search_strings[mode]:
BillNasty's avatar
BillNasty committed
80

81
                if mode != 'RSS':
82
                    sickrage.srCore.srLogger.debug("Search string: %s " % search_string)
BillNasty's avatar
BillNasty committed
83

84
                searchURL = self.urls['search'] % (search_string, self.catagories)
85
                sickrage.srCore.srLogger.debug("Search URL: %s" % searchURL)
fernandog's avatar
fernandog committed
86

87
                try:
88
                    data = sickrage.srCore.srWebSession.get(searchURL).text
89
                except Exception:
90
                    sickrage.srCore.srLogger.debug("No data returned from provider")
BillNasty's avatar
BillNasty committed
91
92
93
                    continue

                try:
94
                    with bs4_parser(data) as html:
BillNasty's avatar
BillNasty committed
95
96
97
                        torrent_table = html.find('table', attrs={'id': 'torrent_table'})
                        torrent_rows = torrent_table.find_all('tr') if torrent_table else []

labrys's avatar
labrys committed
98
                        # Continue only if one Release is found
BillNasty's avatar
BillNasty committed
99
                        if len(torrent_rows) < 2:
100
                            sickrage.srCore.srLogger.debug("Data returned from provider does not contain any torrents")
BillNasty's avatar
BillNasty committed
101
102
103
104
                            continue

                        for result in torrent_rows[1:]:
                            cells = result.find_all('td')
105
106
                            link = result.find('a', attrs={'dir': 'ltr'})
                            url = result.find('a', attrs={'title': 'Download'})
BillNasty's avatar
BillNasty committed
107
108

                            try:
BillNasty's avatar
BillNasty committed
109
                                title = link.contents[0]
110
                                download_url = self.urls['download'] % (url['href'])
111
112
                                seeders = cells[len(cells) - 2].contents[0]
                                leechers = cells[len(cells) - 1].contents[0]
labrys's avatar
labrys committed
113
                                # FIXME
fernandog's avatar
fernandog committed
114
                                size = -1
BillNasty's avatar
BillNasty committed
115
116
117
                            except (AttributeError, TypeError):
                                continue

fernandog's avatar
fernandog committed
118
                            if not all([title, download_url]):
BillNasty's avatar
BillNasty committed
119
                                continue
fernandog's avatar
fernandog committed
120

labrys's avatar
labrys committed
121
                            # Filter unseeded torrent
fernandog's avatar
fernandog committed
122
                            if seeders < self.minseed or leechers < self.minleech:
123
                                if mode != 'RSS':
124
                                    sickrage.srCore.srLogger.debug(
125
126
                                        "Discarding torrent because it doesn't meet the minimum seeders or leechers: {0} (S:{1} L:{2})".format(
                                            title, seeders, leechers))
BillNasty's avatar
BillNasty committed
127
128
                                continue

129
130
                            item = {'title': title, 'link': download_url, 'size': size, 'seeders': seeders,
                                    'leechers': leechers, 'hash': ''}
BillNasty's avatar
BillNasty committed
131

132
133
                            if mode != 'RSS':
                                sickrage.srCore.srLogger.debug("Found result: {}".format(title))
BillNasty's avatar
BillNasty committed
134

135
                            results.append(item)
136
                except Exception:
137
                    sickrage.srCore.srLogger.error("Failed parsing provider")
BillNasty's avatar
BillNasty committed
138

139
140
        # Sort all the items by seeders if available
        results.sort(key=lambda k: try_int(k.get('seeders', 0)), reverse=True)
BillNasty's avatar
BillNasty committed
141

echel0n's avatar
echel0n committed
142
        return results