From 63c72853f4d85bcce783292068d52901d11949d3 Mon Sep 17 00:00:00 2001 From: georgewhewell Date: Tue, 21 Jan 2014 12:07:38 +0000 Subject: [PATCH] Change HDBits provider to use API instead of scraping site --- .../core/providers/torrent/hdbits/__init__.py | 5 -- .../core/providers/torrent/hdbits/main.py | 77 +++++++++++-------- 2 files changed, 46 insertions(+), 36 deletions(-) diff --git a/couchpotato/core/providers/torrent/hdbits/__init__.py b/couchpotato/core/providers/torrent/hdbits/__init__.py index f0873798..1e9aa3ce 100644 --- a/couchpotato/core/providers/torrent/hdbits/__init__.py +++ b/couchpotato/core/providers/torrent/hdbits/__init__.py @@ -22,11 +22,6 @@ config = [{ 'name': 'username', 'default': '', }, - { - 'name': 'password', - 'default': '', - 'type': 'password', - }, { 'name': 'passkey', 'default': '', diff --git a/couchpotato/core/providers/torrent/hdbits/main.py b/couchpotato/core/providers/torrent/hdbits/main.py index 1ecc97e4..2a1b1828 100644 --- a/couchpotato/core/providers/torrent/hdbits/main.py +++ b/couchpotato/core/providers/torrent/hdbits/main.py @@ -1,7 +1,10 @@ -from bs4 import BeautifulSoup from couchpotato.core.helpers.variable import tryInt from couchpotato.core.logger import CPLog from couchpotato.core.providers.torrent.base import TorrentProvider + +import re +import json +import requests import traceback log = CPLog(__name__) @@ -10,50 +13,62 @@ log = CPLog(__name__) class HDBits(TorrentProvider): urls = { - 'test': 'https://hdbits.org/', - 'login': 'https://hdbits.org/login/doLogin/', - 'detail': 'https://hdbits.org/details.php?id=%s&source=browse', - 'search': 'https://hdbits.org/json_search.php?imdb=%s', - 'download': 'https://hdbits.org/download.php/%s.torrent?id=%s&passkey=%s&source=details.browse', - 'login_check': 'http://hdbits.org/inbox.php', + 'test' : 'https://hdbits.org/', + 'detail' : 'https://hdbits.org/details.php?id=%s', + 'download' : 'https://hdbits.org/download.php?id=%s&passkey=%s', + 'api': 'https://hdbits.org/api/torrents' + } + + status_codes = { + 1: 'Failure (something bad happened)', + 2: 'SSL Required - You should be on https://', + 3: 'JSON Malformed', + 4: 'Auth data missing', + 5: 'Auth failed' } http_time_between_calls = 1 #seconds + def _post_query(self, **params): + + post_data = { + 'username': self.conf('username'), + 'passkey': self.conf('passkey') + } + post_data.update(params) + + req = requests.post(self.urls['api'], + data=json.dumps(post_data)) + + response = json.loads(req.text) + + if response['status'] != 0: + log.error('Error searching hdbits: %s' + % self.status_codes[response['status']]) + else: + return response['data'] + def _search(self, movie, quality, results): - data = self.getJsonData(self.urls['search'] % movie['library']['identifier']) + match = re.match(r'tt(\d{7})', + movie['library']['identifier']) + + data = self._post_query(imdb={'id': match.group(1)}) if data: try: for result in data: results.append({ 'id': result['id'], - 'name': result['title'], - 'url': self.urls['download'] % (result['id'], result['id'], self.conf('passkey')), + 'name': result['name'], + 'url': self.urls['download'] + % (result['id'], self.conf('passkey')), 'detail_url': self.urls['detail'] % result['id'], 'size': self.parseSize(result['size']), - 'seeders': tryInt(result['seeder']), - 'leechers': tryInt(result['leecher']) + 'seeders': tryInt(result['seeders']), + 'leechers': tryInt(result['leechers']) }) - except: - log.error('Failed getting results from %s: %s', (self.getName(), traceback.format_exc())) + log.error('Failed getting results from %s: %s', + (self.getName(), traceback.format_exc())) - def getLoginParams(self): - data = self.getHTMLData('https://hdbits.org/login', cache_timeout = 0) - - bs = BeautifulSoup(data) - secret = bs.find('input', attrs = {'name': 'lol'})['value'] - - return { - 'uname': self.conf('username'), - 'password': self.conf('password'), - 'returnto': '/', - 'lol': secret - } - - def loginSuccess(self, output): - return '/logout.php' in output.lower() - - loginCheckSuccess = loginSuccess