Skip to content
module.py 2.8 KiB
Newer Older
hydrargyrum's avatar
hydrargyrum committed
# -*- coding: utf-8 -*-

# Copyright(C) 2016      Vincent A
#
Roger Philibert's avatar
Roger Philibert committed
# This file is part of a woob module.
hydrargyrum's avatar
hydrargyrum committed
#
Roger Philibert's avatar
Roger Philibert committed
# This woob module is free software: you can redistribute it and/or modify
hydrargyrum's avatar
hydrargyrum committed
# 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.
#
Roger Philibert's avatar
Roger Philibert committed
# This woob module is distributed in the hope that it will be useful,
hydrargyrum's avatar
hydrargyrum committed
# 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
Roger Philibert's avatar
Roger Philibert committed
# along with this woob module. If not, see <http://www.gnu.org/licenses/>.
hydrargyrum's avatar
hydrargyrum committed
from base64 import b64decode
from woob.capabilities.paste import CapPaste, BasePaste
from woob.tools.backend import Module, BackendConfig
from woob.tools.capabilities.paste import bin_to_b64
from woob.tools.value import Value
hydrargyrum's avatar
hydrargyrum committed

from .browser import JirafeauBrowser


__all__ = ['JirafeauModule']


class JirafeauPaste(BasePaste):
    @property
    def page_url(self):
        return self.url


class JirafeauModule(Module, CapPaste):
    NAME = 'jirafeau'
    DESCRIPTION = u'Jirafeau-based file upload website'
    MAINTAINER = u'Vincent A'
    EMAIL = 'dev@indigo.re'
    LICENSE = 'AGPLv3+'
Romain Bignon's avatar
Romain Bignon committed
    VERSION = '3.6'
hydrargyrum's avatar
hydrargyrum committed

    CONFIG = BackendConfig(Value('base_url', label='Base Jirafeau URL',
                                 description='URL of the Jirafeau-based site to use',
                                 regexp=r'https?://.*',
hydrargyrum's avatar
hydrargyrum committed
                                 default='https://jirafeau.net/'))
hydrargyrum's avatar
hydrargyrum committed

    BROWSER = JirafeauBrowser

    def create_default_browser(self):
        return self.create_browser(self.config['base_url'].get())

    def can_post(self, contents, title=None, public=None, max_age=None):
        if public or max_age not in self.browser.age_keyword:
            return 0

        max_size, _ = self.browser.get_max_sizes()
hydrargyrum's avatar
hydrargyrum committed
        if max_size and len(b64decode(contents)) > max_size:
hydrargyrum's avatar
hydrargyrum committed
            return 0
        return 1

    def get_paste(self, url):
        d = self.browser.recognize(url)
        if not d:
            return
        if self.browser.exists(d['id']):
            return

        ret = JirafeauPaste(d['id'])
        ret.url = d['url']
        return ret

    def new_paste(self, *args, **kwargs):
        return JirafeauPaste(*args, **kwargs)

    def post_paste(self, paste, max_age=None):
hydrargyrum's avatar
hydrargyrum committed
        d = self.browser.post(b64decode(paste.contents), paste.title, max_age)
hydrargyrum's avatar
hydrargyrum committed
        paste.id = d['id']
        paste.url = d['page_url']
        return paste

    def fill_paste(self, obj, fields):
        if 'contents' in fields:
            data = self.browser.download(obj.id)
hydrargyrum's avatar
hydrargyrum committed
            obj.contents = bin_to_b64(data)
hydrargyrum's avatar
hydrargyrum committed

    OBJECTS = {JirafeauPaste: fill_paste}