python-pixabay
core.py
1 ##
2 # Pixabay API (unofficial)
3 # @author Luk� Pleva� <lukas@plevac.eu>
4 # @date 3.2.2022
5 
6 import requests
7 from .params import params
8 from .query import query as queryCore
9 from .image import image as imageCore
10 from .video import video as videoCore
11 
12 class core:
13  ##
14  # Init function
15  # @param apiKey api key for pixabay API
16  # @param host host of api (default: https://pixabay.com/api/)
17  # @return core obj
18  def __init__(self, apiKey, host='https://pixabay.com/api/'):
19  self.apiKey = apiKey
20  self.host = host
21 
22  ##
23  # Make search qvery on API
24  # @param query search words (ex: big tree)
25  # @param lang language of search words (default "en")
26  # supported: cs, da, de, en, es, fr, id, it, hu, nl, no, pl, pt, ro, sk, fi, sv, tr, vi, th, bg, ru, el, ja, ko, zh
27  # @param orientation orientation of image (default "all")
28  # supported: all, horizontal, vertical
29  # @param perPage number of images per one request (default: 25)
30  # @param order order of images (default: "popular")
31  # supported: popular, latest
32  # @param safeSearch a flag indicating that only images suitable for all ages should be returned (Default: False)
33  # @param minWidth min width of image (default: 0)
34  # @param minHeight min height of image (default: 0)
35  # @param editorsChoice images that have received an Editor's Choice award (Default: False)
36  # @param category filter images by category (default: "all")
37  # supported: all, backgrounds, fashion, nature, science, education, feelings, health, people, religion, places, animals, industry, computer, food, sports, transportation, travel, buildings, business, music
38  # @param colors filter images by color properties. A comma separated list of values may be used to select multiple properties. (Default: "all")
39  # supported: all, grayscale, transparent, red, orange, yellow, green, turquoise, blue, lilac, pink, white, gray, black, brown
40  #
41  # @return query object
42  def query(self, query='', lang='en', orientation='all', perPage=50, order="popular", safeSearch=False, minWidth=0, minHeight=0, editorsChoice=False, category='all', colors='all', image_type='all'):
43  param = params(
44  host = self.host,
45  apiKey = self.apiKey,
46  query = query,
47  lang = lang,
48  orientation = orientation,
49  perPage = perPage,
50  order = order,
51  safeSearch = safeSearch,
52  minWidth = minWidth,
53  minHeight = minHeight,
54  editorsChoice = editorsChoice,
55  category = category,
56  colors = colors,
57  image_type = image_type
58  )
59 
60  return queryCore(param)
61 
62  ##
63  # Get image by ID
64  # @param iid image id
65  # @return image object
66  def image(self, iid):
67  uri = "{host}?key={api}&id={id}".format(
68  host = self.host,
69  id = iid,
70  api = self.apiKey,
71  )
72 
73  r = requests.get(uri)
74 
75  if (r.status_code != 200):
76  raise ValueError('Pixabay return status code != 200 for uri', uri, 'Invalid parameters?')
77 
78  return imageCore(r.json()['hits'][0])
79 
80 ##
81  # Get video by ID
82  # @param iid video id
83  # @return video object
84  def video(self, iid):
85  uri = "{host}videos/?key={api}&id={id}".format(
86  host = self.host,
87  id = iid,
88  api = self.apiKey,
89  )
90 
91  r = requests.get(uri)
92 
93  if (r.status_code != 200):
94  raise ValueError('Pixabay return status code != 200 for uri', uri, 'Invalid parameters?')
95 
96  return videoCore(r.json()['hits'][0])
97 
98  # @return query object
99  def queryVideo(self, query='', lang='en', orientation='all', perPage=50, order="popular", safeSearch=False, minWidth=0, minHeight=0, editorsChoice=False, category='all', colors='all'):
100  param = params(
101  host = self.host+'videos/',
102  apiKey = self.apiKey,
103  query = query,
104  lang = lang,
105  orientation = orientation,
106  perPage = perPage,
107  order = order,
108  safeSearch = safeSearch,
109  minWidth = minWidth,
110  minHeight = minHeight,
111  editorsChoice = editorsChoice,
112  category = category,
113  colors = colors
114  )
115 
116  return queryCore(param)
def query(self, query='', lang='en', orientation='all', perPage=50, order="popular", safeSearch=False, minWidth=0, minHeight=0, editorsChoice=False, category='all', colors='all', image_type='all')
Make search qvery on API.
Definition: core.py:42
def image(self, iid)
Get image by ID.
Definition: core.py:66
Pixabay API (unofficial)
Definition: params.py:6
def __init__(self, apiKey, host='https://pixabay.com/api/')
Init function.
Definition: core.py:18