python-pixabay
image.py
1 ##
2 # Pixabay API (unofficial)
3 # @author Luká¹ Plevaè <lukas@plevac.eu>
4 # @date 17.9.2022
5 
6 from datetime import datetime, timezone
7 import requests
8 import re
9 
10 class image:
11  ##
12  # Init image object
13  # @param data data of image in JSON format
14  # @return image obj
15  def __init__(self, data):
16  self._raw_data = data
17 
18  ##
19  # Get ID of image
20  def getId(self):
21  return self._raw_data['id']
22 
23  ##
24  # Get page url of image
25  def getPageURL(self):
26  return self._raw_data['pageURL']
27 
28  ##
29  # Get type of image
30  def getType(self):
31  return self._raw_data['type']
32 
33  ##
34  # Get tags of image
35  def getTags(self):
36  return self._raw_data['tags']
37 
38  ##
39  # Get url of Preview
40  def getPreviewURL(self):
41  return self._raw_data['previewURL']
42 
43  ##
44  # Get width of Preview
45  def getPreviewWidth(self):
46  return self._raw_data['previewWidth']
47 
48  ##
49  # Get Height of Preview
50  def getPreviewHeight(self):
51  return self._raw_data['previewHeight']
52 
53  ##
54  # Get webformat URL
55  def getWebformatURL(self):
56  return self._raw_data['webformatURL']
57 
58  ##
59  # Get width of webformat
60  def getWebformatWidth(self):
61  return self._raw_data['webformatWidth']
62 
63  ##
64  # Get height of webformat
65  def getWebformatHeight(self):
66  return self._raw_data['webformatHeight']
67 
68  ##
69  # Get large Image URL
70  def getLargeImageURL(self):
71  return self._raw_data['largeImageURL']
72 
73  ##
74  # Get width of image
75  def getImageWidth(self):
76  return self._raw_data['imageWidth']
77 
78  ##
79  # Get height of image
80  def getImageHeight(self):
81  return self._raw_data['imageHeight']
82 
83  ##
84  # Get size of image
85  def getImageSize(self):
86  return self._raw_data['imageSize']
87 
88  ##
89  # Get views of image
90  def getViews(self):
91  return self._raw_data['views']
92 
93  ##
94  # Get downloads of image
95  def getDownloads(self):
96  return self._raw_data['downloads']
97 
98  ##
99  # Get collections of image
100  def getCollections(self):
101  return self._raw_data['collections']
102 
103  ##
104  # Get likes of image
105  def getLikes(self):
106  return self._raw_data['likes']
107 
108  ##
109  # Get comments of image
110  def getComments(self):
111  return self._raw_data['comments']
112 
113  ##
114  # Get author id of image
115  def getUserId(self):
116  return self._raw_data['user_id']
117 
118  ##
119  # Get name of author of image
120  def getUser(self):
121  return self._raw_data['user']
122 
123  ##
124  # Get url of image of author of image
125  def getUserImageURL(self):
126  return self._raw_data['userImageURL']
127 
128  ##
129  # Download image to varaible
130  # @param imtype type if image (webformat, preview, largeImage) (Default: webformat)
131  # @return byte array of image
132  def downloadRaw(self, imtype = 'webformat'):
133 
134  uri = None
135 
136  if (imtype == 'webformat'):
137  uri = self.getWebformatURL()
138  elif (imtype == 'largeImage'):
139  uri = self.getLargeImageURL()
140  elif (imtype == 'preview'):
141  uri = self.getPreviewURL()
142  else:
143  raise ValueError('supported types is webformat, largeImage and preview.', imtype, 'unsupported')
144 
145  r = requests.get(uri, allow_redirects=True)
146 
147  if (r.status_code != 200):
148  raise ValueError('Pixabay return status code != 200 for uri', uri, 'Invalid parameters?')
149 
150  return r.content
151 
152  ##
153  # Download image to file
154  # @param dst location of file to save
155  # @param imtype type if image (webformat, preview, largeImage) (Default: webformat)
156  def download(self, dst, imtype = 'webformat'):
157  with open(dst, 'wb') as handler:
158  handler.write(self.downloadRaw(imtype))
159  ##
160  # Get published date of image
161  # @return datetime UTC of image publication
162  def getPublishedDate(self):
163  preview_url = self.getPreviewURL()
164 
165  # year / month / day / hour / minute
166  match = re.search('\d{4}/\d{2}/\d{2}/\d{2}/\d{2}', preview_url)
167  if match:
168  parts = match.group().split('/')
169  return datetime(int(parts[0]), int(parts[1]), int(parts[2]), int(parts[3]), int(parts[4]), tzinfo=timezone.utc)
170 
171  # year / month / day
172  match = re.search('\d{4}/\d{2}/\d{2}', preview_url)
173  if match:
174  parts = match.group().split('/')
175  return datetime(int(parts[0]), int(parts[1]), int(parts[2]), tzinfo=timezone.utc)
def getPreviewHeight(self)
Get Height of Preview.
Definition: image.py:50
def getViews(self)
Get views of image.
Definition: image.py:90
def getPublishedDate(self)
Get published date of image.
Definition: image.py:162
def getWebformatHeight(self)
Get height of webformat.
Definition: image.py:65
def getTags(self)
Get tags of image.
Definition: image.py:35
def getImageHeight(self)
Get height of image.
Definition: image.py:80
def getPreviewURL(self)
Get url of Preview.
Definition: image.py:40
def getId(self)
Get ID of image.
Definition: image.py:20
def getImageSize(self)
Get size of image.
Definition: image.py:85
def getUser(self)
Get name of author of image.
Definition: image.py:120
def getWebformatURL(self)
Get webformat URL.
Definition: image.py:55
def getPageURL(self)
Get page url of image.
Definition: image.py:25
def __init__(self, data)
Init image object.
Definition: image.py:15
def getUserImageURL(self)
Get url of image of author of image.
Definition: image.py:125
def getPreviewWidth(self)
Get width of Preview.
Definition: image.py:45
def getComments(self)
Get comments of image.
Definition: image.py:110
def getCollections(self)
Get collections of image.
Definition: image.py:100
def getType(self)
Get type of image.
Definition: image.py:30
def download(self, dst, imtype='webformat')
Download image to file.
Definition: image.py:156
def downloadRaw(self, imtype='webformat')
Download image to varaible.
Definition: image.py:132
def getLargeImageURL(self)
Get large Image URL.
Definition: image.py:70
def getUserId(self)
Get author id of image.
Definition: image.py:115
def getWebformatWidth(self)
Get width of webformat.
Definition: image.py:60
def getDownloads(self)
Get downloads of image.
Definition: image.py:95
def getLikes(self)
Get likes of image.
Definition: image.py:105
def getImageWidth(self)
Get width of image.
Definition: image.py:75