python-pixabay
query.py
1 ##
2 # Pixabay API (unofficial)
3 # @author Luk� Pleva� <lukas@plevac.eu>
4 # @date 3.2.2022
5 
6 import requests
7 from .image import image
8 from .video import video
9 import urllib.parse
10 
11 class query:
12 
13  ##
14  # Init query on API
15  # @params params params of query instance of class params
16  # @return query object
17  def __init__(self, params):
18  self.params = params
19  self.cache = []
20  self._getPage(0)
21 
22  ##
23  # Get lenght of fonded images
24  # @return len of fonded images
25  def __len__(self):
26  return self.info['totalHits']
27 
28  ##
29  # Get item on index from result
30  # @praram index index of item
31  # return image object of item
32  def __getitem__(self, index):
33  if (index < 0):
34  index += self.__len__()
35 
36  if not(self._inCache(index)):
37  self._addToChache(index)
38  if 'videos' in self.params.host:
39  return video(self.cache[index])
40  else:
41  return image(self.cache[index])
42 
43  ##
44  # Check if is image with index in chace
45  # @param index index of image
46  # @return bool
47  def _inCache(self, index):
48  return len(self.cache) > index and self.cache[index] != None
49 
50  ##
51  # Insert image in cache
52  # @param index index of image
53  # @param data data of image
54  def _cacheInsert(self, index, data):
55 
56  for i in range(len(self.cache) - 1, index - 1):
57  self.cache.insert(i, None)
58 
59  self.cache.insert(index, data)
60 
61  ##
62  # Download page from API and save to cache
63  # @param page index of page (from 0)
64  def _getPage(self, page):
65  uri = "{host}?key={api}&q={query}&lang={lang}&orientation={orient}&per_page={per}&page={page}&order={order}&safesearch={safe}&min_width={width}&min_height={height}&editors_choice={editors}&category={cat}&colors={colors}&image_type={image_type}".format(
66  host = self.params.host,
67  query = urllib.parse.quote(self.params.query, safe=''),
68  api = self.params.apiKey,
69  lang = self.params.lang,
70  orient = self.params.orientation,
71  per = self.params.perPage,
72  page = page + 1,
73  order = self.params.order,
74  safe = self.params.safeSearch,
75  width = self.params.minWidth,
76  height = self.params.minHeight,
77  editors = self.params.editorsChoice,
78  cat = self.params.category,
79  colors = self.params.colors,
80  image_type = self.params.image_type
81  )
82 
83  r = requests.get(uri)
84 
85  if (r.status_code != 200):
86  raise ValueError('Pixabay return status code != 200 for uri', uri, 'Invalid parameters?')
87 
88  data = r.json()
89 
90  # Update info
91  self.info = {}
92  self.info['total'] = data['total']
93  self.info['totalHits'] = data['totalHits']
94 
95  # copy data
96  for i in range(len(data['hits'])):
97  self._cacheInsert(page * self.params.perPage + i, data['hits'][i])
98 
99  ##
100  # Download image with index to cache
101  # @param index index of image
102  def _addToChache(self, index):
103  page = index // self.params.perPage
104  self._getPage(page)
def __init__(self, params)
Init query on API params params of query instance of class params.
Definition: query.py:17
def _cacheInsert(self, index, data)
Insert image in cache.
Definition: query.py:54
def __getitem__(self, index)
Get item on index from result index index of item return image object of item.
Definition: query.py:32
def _inCache(self, index)
Check if is image with index in chace.
Definition: query.py:47
def __len__(self)
Get lenght of fonded images.
Definition: query.py:25
def _addToChache(self, index)
Download image with index to cache.
Definition: query.py:102
def _getPage(self, page)
Download page from API and save to cache.
Definition: query.py:64