Commit 4cb855f5 authored by Vincent A's avatar Vincent A

[unsplash] new CC0 image search module

parent 6e485731
Pipeline #3366 passed with stages
in 14 minutes and 52 seconds
# -*- coding: utf-8 -*-
# Copyright(C) 2017 Vincent A
#
# This file is part of a weboob module.
#
# This weboob module is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This weboob 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this weboob module. If not, see <http://www.gnu.org/licenses/>.
from __future__ import unicode_literals
from .module import UnsplashModule
__all__ = ['UnsplashModule']
# -*- coding: utf-8 -*-
# Copyright(C) 2017 Vincent A
#
# This file is part of a weboob module.
#
# This weboob module is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This weboob 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this weboob module. If not, see <http://www.gnu.org/licenses/>.
from weboob.browser import PagesBrowser, URL
from .pages import ImageSearch
class UnsplashBrowser(PagesBrowser):
BASEURL = 'https://unsplash.com'
collection_search = URL(r'/napi/search/collections\?query=(?P<term>[^&]+)&page=(?P<page>\d+)&per_page=20')
image_search = URL(r'/napi/search/photos\?query=(?P<term>[^&]+)&page=(?P<page>\d+)&per_page=20', ImageSearch)
def __init__(self, *args, **kwargs):
super(UnsplashBrowser, self).__init__(*args, **kwargs)
self.session.headers['Authorization'] = 'Client-ID d69927c7ea5c770fa2ce9a2f1e3589bd896454f7068f689d8e41a25b54fa6042'
def search_image(self, term):
n = 1
nb_pages = 1
while n <= nb_pages:
self.image_search.go(term=term, page=n)
for img in self.page.iter_images():
yield img
nb_pages = self.page.nb_pages()
n += 1
# -*- coding: utf-8 -*-
# Copyright(C) 2017 Vincent A
#
# This file is part of a weboob module.
#
# This weboob module is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This weboob 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this weboob module. If not, see <http://www.gnu.org/licenses/>.
from weboob.tools.backend import Module
from weboob.capabilities.file import CapFile
from weboob.capabilities.image import CapImage, BaseImage
from .browser import UnsplashBrowser
__all__ = ['UnsplashModule']
class UnsplashModule(Module, CapImage):
NAME = 'unsplash'
DESCRIPTION = u'unsplash website'
MAINTAINER = u'Vincent A'
EMAIL = 'dev@indigo.re'
LICENSE = 'AGPLv3+'
VERSION = '2.1'
BROWSER = UnsplashBrowser
def search_image(self, pattern, sortby=CapFile.SEARCH_RELEVANCE, nsfw=False):
return self.browser.search_image(pattern)
def fill_image(self, img, fields):
if 'data' in fields:
img.data = self.browser.open(img.url).content
if 'thumbnail' in fields:
img.thumbnail.data = self.browser.open(img.thumbnail.url).content
OBJECTS = {BaseImage: fill_image}
# -*- coding: utf-8 -*-
# Copyright(C) 2017 Vincent A
#
# This file is part of a weboob module.
#
# This weboob module is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This weboob 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this weboob module. If not, see <http://www.gnu.org/licenses/>.
from weboob.browser.pages import JsonPage
from weboob.browser.elements import DictElement, ItemElement, method
from weboob.browser.filters.json import Dict
from weboob.browser.filters.standard import DateTime, Field, Format
from weboob.capabilities.image import BaseImage, Thumbnail
from weboob.capabilities.file import LICENSES
class CollectionSearch(JsonPage):
def do_stuff(self, _id):
raise NotImplementedError()
class ImageSearch(JsonPage):
def nb_pages(self):
return self.doc['total_pages']
@method
class iter_images(DictElement):
item_xpath = 'results'
class item(ItemElement):
klass = BaseImage
obj_id = Dict('id')
obj_nsfw = False
obj_license = LICENSES.PD
obj_author = Dict('user/name')
obj_url = Dict('urls/full')
obj_date = DateTime(Dict('created_at'))
obj_title = Format('%s (%s)', Field('id'), Field('author'))
obj_ext = 'jpg'
def obj_thumbnail(self):
return Thumbnail(Dict('urls/thumb')(self))
# -*- coding: utf-8 -*-
# Copyright(C) 2017 Vincent A
#
# This file is part of a weboob module.
#
# This weboob module is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This weboob 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this weboob module. If not, see <http://www.gnu.org/licenses/>.
from weboob.tools.test import BackendTest
class UnsplashTest(BackendTest):
MODULE = 'unsplash'
def test_search_img(self):
it = self.backend.search_image('tree')
images = [img for _, img in zip(range(20), it)]
self.assertEqual(len(images), 20)
for img in images:
assert img.id
assert img.title
assert img.ext
assert img.author
assert img.date
assert img.url
self.backend.fillobj(img, 'data')
assert img.data
self.backend.fillobj(img, 'thumbnail')
assert img.thumbnail.data
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment