pages.py 3.61 KB
Newer Older
1 2 3 4
# -*- coding: utf-8 -*-

# Copyright(C) 2018 Julien Veyssier
#
5
# This file is part of a weboob module.
6
#
7
# This weboob module is free software: you can redistribute it and/or modify
8 9 10 11
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
12
# This weboob module is distributed in the hope that it will be useful,
13 14 15 16 17
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
18
# along with this weboob module. If not, see <http://www.gnu.org/licenses/>.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
import re

from weboob.tools.misc import get_bytes_size
from weboob.capabilities.torrent import Torrent

from weboob.browser.elements import ItemElement, ListElement, method
from weboob.browser.pages import HTMLPage, pagination
from weboob.browser.filters.standard import Regexp, CleanText, CleanDecimal, Format
from weboob.browser.filters.html import AbsoluteLink


class SearchPage(HTMLPage):
    @pagination
    @method
    class iter_torrents(ListElement):
        next_page = AbsoluteLink('//a[@id="next"]')
        item_xpath = '//table[has-class("table2")]//tr[position()>1]'

        class item(ItemElement):
            klass = Torrent
            def obj_url(self):
40
                url = Regexp(AbsoluteLink('.//div[has-class("tt-name")]/a[1]'), '(^.*)\?.*', '\\1')(self)
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
                return url.replace('http://', 'https://')
            obj_id = Regexp(CleanText('.//div[has-class("tt-name")]/a[2]/@href'), '/.*-torrent-([0-9]+)\.html$', '\\1')
            obj_name = CleanText('.//div[has-class("tt-name")]/a[2]/text()')
            obj_seeders = CleanDecimal('.//td[has-class("tdseed")]', default=0)
            obj_leechers = CleanDecimal('.//td[has-class("tdleech")]', default=0)
            obj_filename = Format('%s.torrent', obj_name)

            def obj_size(self):
                rawsize = CleanText('(.//td[has-class("tdnormal")])[2]')(self)
                nsize = float(re.sub(r'[A-Za-z]', '', rawsize))
                usize = re.sub(r'[.0-9 ]', '', rawsize).upper()
                size = get_bytes_size(nsize, usize)
                return size


class TorrentPage(HTMLPage):
    @method
    class get_torrent(ItemElement):
        klass = Torrent
        obj_name = CleanText('.//div[@id="content"]/h1')
        obj_id = Regexp(CleanText('//div[@id="updatestatslink"]/a/@onclick'), 'torrent_id=([0-9]+)&', '\\1')
        def obj_url(self):
63
            url = Regexp(AbsoluteLink('//div[has-class("torrentinfo")]//div[has-class("dltorrent")]//a[text()="Download torrent"]'), '(^.*)\?.*', '\\1')(self)
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
            return url.replace('http://', 'https://')
        obj_filename = Format('%s.torrent', obj_name)
        def obj_size(self):
            s = CleanText('//td/b[text()="Size"]/../../td[2]')(self)
            nsize = float(re.sub(r'[A-Za-z]', '', s))
            usize = re.sub(r'[.0-9 ]', '', s).upper()
            size = get_bytes_size(nsize, usize)
            return size
        def obj_files(self):
            res = []
            for f in self.xpath('//div[has-class("fileline")]'):
                res.append(CleanText(f)(self))
            return res
        obj_seeders = CleanDecimal('//div[@id="content"]/span[has-class("greenish")]', default=0)
        obj_leechers = CleanDecimal('//div[@id="content"]/span[has-class("reddish")]', default=0)
        obj_magnet = AbsoluteLink('//div[has-class("torrentinfo")]//div[has-class("dltorrent")]//a[text()="Magnet Link"]')