Skip to content
browser.py 1.97 KiB
Newer Older
# -*- coding: utf-8 -*-

hydrargyrum's avatar
hydrargyrum committed
# Copyright(C) 2015      Vincent A
Roger Philibert's avatar
Roger Philibert committed
# This file is part of a woob module.
Roger Philibert's avatar
Roger Philibert committed
# This woob module is free software: you can redistribute it and/or modify
# 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,
# 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, b64encode
hydrargyrum's avatar
hydrargyrum committed
import math
from urllib.parse import urljoin
from woob.browser import PagesBrowser, URL
hydrargyrum's avatar
hydrargyrum committed
from .pages import ImagePage, UploadPage
hydrargyrum's avatar
hydrargyrum committed
class LutimBrowser(PagesBrowser):
    BASEURL = 'https://lut.im'
    image_page = URL('(?P<id>.+)', ImagePage)
    upload_page = URL('', UploadPage)

    def __init__(self, base_url, *args, **kw):
hydrargyrum's avatar
hydrargyrum committed
        PagesBrowser.__init__(self, *args, **kw)
        self.base_url = self.BASEURL = base_url

    def fetch(self, paste):
        self.location(paste.id)
        assert self.image_page.is_here()
hydrargyrum's avatar
hydrargyrum committed
        paste.contents = b64encode(self.page.contents).decode('ascii')
hydrargyrum's avatar
hydrargyrum committed
        paste.title = self.page.filename

    def post(self, paste, max_age=0):
hydrargyrum's avatar
hydrargyrum committed
        bin = b64decode(paste.contents)
        name = paste.title or 'file'  # filename is mandatory
        filefield = {'file': (name, BytesIO(bin))}
hydrargyrum's avatar
hydrargyrum committed
        params = {'format': 'json'}
        if max_age:
            params['delete-day'] = int(math.ceil(max_age / 86400.))
        self.location(self.BASEURL, data=params, files=filefield)
hydrargyrum's avatar
hydrargyrum committed
        assert self.upload_page.is_here()
        info = self.page.fetch_info()
        paste.id = urljoin(self.base_url, info['short'])