You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
5.4 KiB

# -*- coding: utf-8 -*-
import urllib
from lxml import html
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.image import AsyncImage
from kivy.uix.button import ButtonBehavior, Button
from import DictProperty, ObjectProperty, StringProperty
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivy.core.window import Window
from .editor import Slide
from kivy.logger import Logger
from kivy.uix.screenmanager import Screen
from kivy.metrics import dp
class SearchScreen(Screen):
def on_enter(self, *args): = True
class SearchBox(BoxLayout):
app = ObjectProperty(None)
current_page = ObjectProperty(1)
total_pages = ObjectProperty(None)
status = ObjectProperty('')
def search(self, term, provider):
Logger.debug('Search: requesting {term} from {provider}'.format(term=term,
providers = {'MetMuseum': 'met',
'Getty OCI': 'getty'}
if not term:
popup = Popup(title=_('Error'), size_hint=(0.3, 0.2))
popup.add_widget(Label(text=_('Please enter a search term.')))
elif provider == 'Flickr':
results =
for photo in results['photos']['photo']:
photo['thumb'] =, 't')
photo['artist'] = photo['owner']
photo['year'] = 'None'
Logger.debug('Search (Flickr): Loading {url}'.format(url=photo['thumb']))
image = ItemButton(photo=photo, source=photo['thumb'], keep_ratio=True, size_hint=(None, None))
elif provider == _('Select a search engine'):
popup = Popup(title=_('Error'), size_hint=(0.3, 0.2))
popup.add_widget(Label(text=_('Please select a search engine.')))
elif provider == 'MetMuseum':, providers[provider], self.current_page)
elif provider == 'Getty OCI':, providers[provider], self.current_page)
def search_next(self, text, provider):
if self.current_page < self.total_pages:
self.current_page += 1, provider)
def search_previous(self, text, provider):
if self.current_page > 1:
self.current_page -= 1, provider)
class ItemButton(ButtonBehavior, AsyncImage):
photo = DictProperty(None)
def on_press(self):
if '' in['thumb']:
src =['thumb'].replace('web-thumb', 'web-large')
popup = SearchItemInfo(, thumbnail=src, provider='MET')
elif '' in['thumb']:
popup = SearchItemInfo(,['thumb'], provider='Getty')
src =['thumb'].replace('t.jpg', 'm.jpg')
popup = SearchItemInfo(, thumbnail=src, provider='Flickr')
class SearchItemInfo(Popup):
photo = DictProperty(None)
thumbnail = StringProperty(None)
provider = StringProperty(None)
# TODO: Better async download of image and thumb creation.
def add_to_presentation(self):
"""Download high-res image and add slide to presentation.
app = ObjectProperty(None)
slide = Slide(['thumb'],['thumb'],['artist'],['title'],['year'])
if self.provider == 'MET':
# Check if high-res is available.
url =['thumb'].replace('web-thumb', 'original')
req = urllib.request.urlopen(url)
if req.getcode() == 404:
Logger.debug('Search: High-res image not available.')'Error'), _('High-res image not available, downloading inferior quality.')), slide)
else:, slide)
elif self.provider == 'Getty':
req = urllib.request.urlopen(['obj_link'])
tree = html.parse(req)
img_link = tree.xpath('//div[@class="cs-result-image"]//span[@class="nav"]/a/@href')
if img_link:
link = urllib.parse.urlparse(tree.xpath('//div[@class="cs-result-image"]//span[@class="nav"]/a/@href')[0])
url = urllib.parse.parse_qs(link.query)['dlimgurl'][0]
url =['thumb']
slide.img_src = url, slide)
elif self.provider == 'Flickr':
# thumb_filename = basename(['thumb'])
# thumb = splitext(thumb_filename)
# url = ''.join((thumb[0][:-1], 'o', thumb[1]))
url = app.get_flickr_url(, 'o'), slide)