|
| 1 | +# coding=utf-8 |
| 2 | + |
| 3 | +"""Provider code for GimmePeers.""" |
| 4 | + |
| 5 | +from __future__ import unicode_literals |
| 6 | + |
| 7 | +import logging |
| 8 | +import re |
| 9 | + |
| 10 | +from medusa import tv |
| 11 | +from medusa.bs4_parser import BS4Parser |
| 12 | +from medusa.helper.common import convert_size |
| 13 | +from medusa.logger.adapters.style import BraceAdapter |
| 14 | +from medusa.providers.torrent.torrent_provider import TorrentProvider |
| 15 | + |
| 16 | +from requests.compat import urljoin |
| 17 | +from requests.utils import dict_from_cookiejar |
| 18 | + |
| 19 | +log = BraceAdapter(logging.getLogger(__name__)) |
| 20 | +log.logger.addHandler(logging.NullHandler()) |
| 21 | + |
| 22 | + |
| 23 | +class GimmePeersProvider(TorrentProvider): |
| 24 | + """GimmePeers Torrent provider.""" |
| 25 | + |
| 26 | + def __init__(self): |
| 27 | + """Initialize the class.""" |
| 28 | + super(GimmePeersProvider, self).__init__('GimmePeers') |
| 29 | + |
| 30 | + self.username = None |
| 31 | + self.password = None |
| 32 | + |
| 33 | + self.url = 'https://www.gimmepeers.com' |
| 34 | + self.urls = { |
| 35 | + 'login': urljoin(self.url, 'takelogin.php'), |
| 36 | + 'search': urljoin(self.url, 'browse.php'), |
| 37 | + } |
| 38 | + |
| 39 | + # Proper Strings |
| 40 | + self.proper_strings = ['PROPER', 'REPACK', 'REAL', 'RERIP'] |
| 41 | + |
| 42 | + self.cache = tv.Cache(self) |
| 43 | + |
| 44 | + def search(self, search_strings, age=0, ep_obj=None, **kwargs): |
| 45 | + """ |
| 46 | + Search a provider and parse the results. |
| 47 | +
|
| 48 | + :param search_strings: A dict with mode (key) and the search value (value) |
| 49 | + :param age: Not used |
| 50 | + :param ep_obj: Not used |
| 51 | + :returns: A list of search results (structure) |
| 52 | + """ |
| 53 | + results = [] |
| 54 | + if not self.login(): |
| 55 | + return results |
| 56 | + |
| 57 | + search_params = { |
| 58 | + 'c20': 1, |
| 59 | + 'c21': 1, |
| 60 | + 'c25': 1, |
| 61 | + 'c24': 1, |
| 62 | + 'c23': 1, |
| 63 | + 'c22': 1, |
| 64 | + 'c1': 1, |
| 65 | + } |
| 66 | + |
| 67 | + for mode in search_strings: |
| 68 | + log.debug('Search Mode: {0}', mode) |
| 69 | + |
| 70 | + for search_string in search_strings[mode]: |
| 71 | + |
| 72 | + if mode != 'RSS': |
| 73 | + log.debug('Search string: {search}', |
| 74 | + {'search': search_string}) |
| 75 | + |
| 76 | + search_params['search'] = search_string |
| 77 | + |
| 78 | + response = self.session.get(self.urls['search'], params=search_params) |
| 79 | + if not response or not response.text: |
| 80 | + log.debug('No data returned from provider') |
| 81 | + continue |
| 82 | + |
| 83 | + results += self.parse(response.text, mode) |
| 84 | + |
| 85 | + return results |
| 86 | + |
| 87 | + def parse(self, data, mode): |
| 88 | + """ |
| 89 | + Parse search results for items. |
| 90 | +
|
| 91 | + :param data: The raw response from a search |
| 92 | + :param mode: The current mode used to search, e.g. RSS |
| 93 | + :return: A list of items found |
| 94 | + """ |
| 95 | + items = [] |
| 96 | + |
| 97 | + with BS4Parser(data, 'html5lib') as html: |
| 98 | + torrent_table = html.find('table', class_='browsetable') |
| 99 | + torrent_rows = torrent_table('tr') if torrent_table else [] |
| 100 | + |
| 101 | + # Continue only if one release is found |
| 102 | + if len(torrent_rows) < 2: |
| 103 | + log.debug('Data returned from provider does not contain any torrents') |
| 104 | + return items |
| 105 | + |
| 106 | + for result in torrent_rows[1:]: |
| 107 | + cells = result('td') |
| 108 | + |
| 109 | + try: |
| 110 | + link = cells[1].find('a') |
| 111 | + download_url = urljoin(self.url, cells[2].find('a')['href']) |
| 112 | + title = link.get_text() |
| 113 | + if not all([title, download_url]): |
| 114 | + continue |
| 115 | + |
| 116 | + seeders = int(cells[10].get_text().replace(',', '')) |
| 117 | + leechers = int(cells[11].get_text().replace(',', '')) |
| 118 | + |
| 119 | + # Filter unseeded torrent |
| 120 | + if seeders < self.minseed: |
| 121 | + if mode != 'RSS': |
| 122 | + log.debug("Discarding torrent because it doesn't meet the" |
| 123 | + ' minimum seeders: {0}. Seeders: {1}', |
| 124 | + title, seeders) |
| 125 | + continue |
| 126 | + |
| 127 | + torrent_size = cells[5].get_text(' ') |
| 128 | + size = convert_size(torrent_size) or -1 |
| 129 | + |
| 130 | + pubdate_raw = cells[6].get_text() |
| 131 | + pubdate = self.parse_pubdate(pubdate_raw) |
| 132 | + |
| 133 | + item = { |
| 134 | + 'title': title, |
| 135 | + 'link': download_url, |
| 136 | + 'size': size, |
| 137 | + 'seeders': seeders, |
| 138 | + 'leechers': leechers, |
| 139 | + 'pubdate': pubdate, |
| 140 | + } |
| 141 | + if mode != 'RSS': |
| 142 | + log.debug('Found result: {0} with {1} seeders and {2} leechers', |
| 143 | + title, seeders, leechers) |
| 144 | + |
| 145 | + items.append(item) |
| 146 | + except (AttributeError, TypeError, KeyError, ValueError, IndexError): |
| 147 | + log.exception('Failed parsing provider.') |
| 148 | + |
| 149 | + return items |
| 150 | + |
| 151 | + def login(self): |
| 152 | + """Login method used for logging in before doing search and torrent downloads.""" |
| 153 | + if any(dict_from_cookiejar(self.session.cookies).values()): |
| 154 | + return True |
| 155 | + |
| 156 | + login_params = { |
| 157 | + 'username': self.username, |
| 158 | + 'password': self.password, |
| 159 | + 'ssl': 'yes', |
| 160 | + } |
| 161 | + |
| 162 | + response = self.session.post(self.urls['login'], data=login_params) |
| 163 | + if not response or not response.text: |
| 164 | + log.debug('Unable to connect to provider') |
| 165 | + return False |
| 166 | + |
| 167 | + if re.search('Username or password incorrect!', response.text): |
| 168 | + log.debug('Invalid username or password. Check your settings') |
| 169 | + return False |
| 170 | + |
| 171 | + return True |
| 172 | + |
| 173 | + |
| 174 | +provider = GimmePeersProvider() |
0 commit comments