From a40cf0ef445fe240364270a70ad111ae1b7fbce3 Mon Sep 17 00:00:00 2001 From: Julien Veyssier Date: Sat, 27 Nov 2010 22:32:34 +0100 Subject: [PATCH] isohunt 50% --- weboob/backends/isohunt/__init__.py | 3 + weboob/backends/isohunt/backend.py | 54 ++++++++++ weboob/backends/isohunt/browser.py | 53 ++++++++++ weboob/backends/isohunt/pages/__init__.py | 0 weboob/backends/isohunt/pages/torrents.py | 117 ++++++++++++++++++++++ weboob/backends/isohunt/test.py | 26 +++++ 6 files changed, 253 insertions(+) create mode 100644 weboob/backends/isohunt/__init__.py create mode 100644 weboob/backends/isohunt/backend.py create mode 100644 weboob/backends/isohunt/browser.py create mode 100644 weboob/backends/isohunt/pages/__init__.py create mode 100644 weboob/backends/isohunt/pages/torrents.py create mode 100644 weboob/backends/isohunt/test.py diff --git a/weboob/backends/isohunt/__init__.py b/weboob/backends/isohunt/__init__.py new file mode 100644 index 00000000..e9db6014 --- /dev/null +++ b/weboob/backends/isohunt/__init__.py @@ -0,0 +1,3 @@ +from .backend import IsohuntBackend + +__all__ = ['IsohuntBackend'] diff --git a/weboob/backends/isohunt/backend.py b/weboob/backends/isohunt/backend.py new file mode 100644 index 00000000..4437338f --- /dev/null +++ b/weboob/backends/isohunt/backend.py @@ -0,0 +1,54 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2010 Julien Veyssier +# +# This program 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, version 3 of the License. +# +# This program 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 +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +from weboob.capabilities.torrent import ICapTorrent +from weboob.tools.backend import BaseBackend + +from .browser import IsohuntBrowser + + +__all__ = ['IsohuntBackend'] + + +class IsohuntBackend(BaseBackend, ICapTorrent): + NAME = 'isohunt' + MAINTAINER = 'Julien Veyssier' + EMAIL = 'julien.veyssier@aiur.fr' + VERSION = '0.4' + DESCRIPTION = 'isohunt.com bittorrent tracker' + LICENSE = 'GPLv3' + #CONFIG = ValuesDict(Value('domain', label='Domain (example "ssl.what.cd")'), + # Value('protocol', label='Protocol to use', choices=('http', 'https')), + # Value('username', label='Username'), + # Value('password', label='Password', masked=True)) + BROWSER = IsohuntBrowser + + def create_default_browser(self): + return self.create_browser() + + def get_torrent(self, id): + return self.browser.get_torrent(id) + + def get_torrent_file(self, id): + torrent = self.browser.get_torrent(id) + if not torrent: + return None + + return self.browser.openurl(torrent.url.encode('utf-8')).read() + + def iter_torrents(self, pattern): + return self.browser.iter_torrents(pattern.replace(' ','+')) diff --git a/weboob/backends/isohunt/browser.py b/weboob/backends/isohunt/browser.py new file mode 100644 index 00000000..29778e44 --- /dev/null +++ b/weboob/backends/isohunt/browser.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2010 Julien Veyssier +# +# This program 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, version 3 of the License. +# +# This program 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 +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + +from weboob.tools.browser import BaseBrowser + +from .pages.torrents import TorrentsPage, TorrentPage + + +__all__ = ['IsohuntBrowser'] + + +class IsohuntBrowser(BaseBrowser): + DOMAIN = 'isohunt.com' + PROTOCOL = 'https' + ENCODING = 'utf-8' + USER_AGENT = BaseBrowser.USER_AGENTS['wget'] + PAGES = { + 'https://isohunt.com/torrents/.*iht=-1&ihp=1&ihs1=2&iho1=d' : TorrentsPage, + 'http://isohunt.com/torrent_details.*tab=summary' : TorrentPage + } + + def __init__(self, *args, **kwargs): + BaseBrowser.__init__(self, *args, **kwargs) + + def home(self): + return self.location('https://isohunt.com') + + def iter_torrents(self, pattern): + self.location('https://isohunt.com/torrents/%s?iht=-1&ihp=1&ihs1=2&iho1=d' % pattern) + + assert self.is_on_page(TorrentsPage) + return self.page.iter_torrents() + + def get_torrent(self, id): + self.location('https://isohunt.com/torrent_details/%s/?tab=summary' % id) + + assert self.is_on_page(TorrentPage) + return self.page.get_torrent(id) diff --git a/weboob/backends/isohunt/pages/__init__.py b/weboob/backends/isohunt/pages/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/weboob/backends/isohunt/pages/torrents.py b/weboob/backends/isohunt/pages/torrents.py new file mode 100644 index 00000000..a4e420c0 --- /dev/null +++ b/weboob/backends/isohunt/pages/torrents.py @@ -0,0 +1,117 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2010 Julien Veyssier +# +# This program 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, version 3 of the License. +# +# This program 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 +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + + +from weboob.tools.browser import BasePage +from weboob.capabilities.torrent import Torrent + + +__all__ = ['TorrentsPage'] + + +class TorrentsPage(BasePage): + def unit(self, n, u): + m = {'KB': 1024, + 'MB': 1024*1024, + 'GB': 1024*1024*1024, + 'TB': 1024*1024*1024*1024, + } + return float(n*m[u]) + + def iter_torrents(self): + + + + for tr in self.document.getiterator('tr'): + if tr.attrib.get('class','') == 'hlRow': + atitle = tr.getchildren()[2].getchildren()[1] + title = atitle.text + if not title: + title = '' + for bold in atitle.getchildren(): + if bold.text: + title += bold.text + if bold.tail: + title += bold.tail + idt = tr.getchildren()[2].getchildren()[0].attrib.get('href','') + idt = idt.split('/')[2] + size = tr.getchildren()[3].text + u = size[-2:] + size = float(size[:-3]) + seed = tr.getchildren()[4].text + leech = tr.getchildren()[5].text + url = 'https://isohunt.com/download/%s/mon_joli_torrent.torrent' % idt + + torrent = Torrent(idt, + title, + url=url, + size=self.unit(size,u), + seeders=int(seed), + leechers=int(leech)) + yield torrent + +class TorrentPage(BasePage): + def unit(self, n, u): + m = {'KB': 1024, + 'MB': 1024*1024, + 'GB': 1024*1024*1024, + 'TB': 1024*1024*1024*1024, + } + return float(n*m[u]) + + def get_torrent(self, id): + url = 'https://isohunt.com/download/%s/mon_joli_torrent.torrent' % id + for a in self.document.getiterator('a'): + if 'Search more torrents of' in a.attrib.get('title',''): + title = a.tail + #--------------------------TODO + + description = "No description" + for div in self.document.getiterator('div'): + if div.attrib.get('id','') == 'desc': + description = div.text.strip() + for td in self.document.getiterator('td'): + if td.attrib.get('class','') == 'hreview-aggregate': + seed = int(td.getchildren()[2].getchildren()[0].getchildren()[0].text) + leech = int(td.getchildren()[2].getchildren()[1].getchildren()[0].text) + url = td.getchildren()[3].getchildren()[0].attrib.get('href') + title = td.getchildren()[1].getchildren()[0].getchildren()[0].text + + size = 0 + for span in self.document.getiterator('span'): + if span.attrib.get('class','') == "folder" or span.attrib.get('class','') == "folderopen": + size = span.getchildren()[1].tail + u = size.split(' ')[-1].split(')')[0] + size = float(size.split(': ')[1].split(' ')[0].replace(',','.')) + + files = [] + for td in self.document.getiterator('td'): + if td.attrib.get('class','') == 'torFileName': + files.append(td.text) + + + torrent = Torrent(id, title) + torrent = Torrent(id, title) + torrent.url = url + torrent.size = self.unit(size,u) + torrent.seeders = int(seed) + torrent.leechers = int(leech) + torrent.description = description + torrent.files = files + + return torrent diff --git a/weboob/backends/isohunt/test.py b/weboob/backends/isohunt/test.py new file mode 100644 index 00000000..6c461297 --- /dev/null +++ b/weboob/backends/isohunt/test.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2010 Julien Veyssier +# +# This program 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, version 3 of the License. +# +# This program 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 +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +from weboob.tools.test import BackendTest + +class IsohuntTest(BackendTest): + BACKEND = 'isohunt' + + def test_torrent(self): + l = list(self.backend.iter_torrents('debian')) + if len(l) > 0: + self.backend.get_torrent_file(l[0].id)