python-pixabay
video.py
1 ##
2 # Pixabay API (unofficial)
3 # @author qualityassurance
4 # @email qualityassurance21@163.com
5 # @date 2022.9.17
6 
7 import requests
8 
9 class video:
10  ##
11  # Init video object
12  # @param data data of video in JSON format
13  # @return video obj
14  def __init__(self, data):
15  self._raw_data = data
16 
17  ##
18  # Get ID of video
19  def getId(self):
20  return self._raw_data['id']
21 
22  ##
23  # Get page url of video
24  def getPageURL(self):
25  return self._raw_data['pageURL']
26 
27  ##
28  # Get type of video
29  def getType(self):
30  return self._raw_data['type']
31 
32  ##
33  # Get tags of video
34  def getTags(self):
35  return self._raw_data['tags']
36 
37  ##
38  # Get url of Preview
39  def getDuration(self):
40  return self._raw_data['duration']
41 
42  ##
43  # Get width of Preview
44  def getPictureId(self):
45  return self._raw_data['picture_id']
46 
47  ##
48  # Get Height of Preview
49  def getVideoLarge(self):
50  return self._raw_data['videos']['large']['url']
51 
52  ##
53  # Get Height of Preview
54  def getVideoSmall(self):
55  return self._raw_data['videos']['small']['url']
56 
57  ##
58  # Get Height of Preview
59  def getVideoMedium(self):
60  return self._raw_data['videos']['medium']['url']
61 
62  ##
63  # Get Height of Preview
64  def getVideoTiny(self):
65  return self._raw_data['videos']['tiny']['url']
66 
67  ##
68  # Get views of video
69  def getViews(self):
70  return self._raw_data['views']
71 
72  ##
73  # Get downloads of video
74  def getDownloads(self):
75  return self._raw_data['downloads']
76 
77  ##
78  # Get likes of video
79  def getLikes(self):
80  return self._raw_data['likes']
81 
82  ##
83  # Get comments of video
84  def getComments(self):
85  return self._raw_data['comments']
86 
87  ##
88  # Get author id of video
89  def getUserId(self):
90  return self._raw_data['user_id']
91 
92  ##
93  # Get name of author of video
94  def getUser(self):
95  return self._raw_data['user']
96 
97  ##
98  # Get url of video of author of video
99  def getUservideoURL(self):
100  return self._raw_data['uservideoURL']
101 
102  ##
103  # Download video to varaible
104  # @param imtype type if video (webformat, preview, largevideo) (Default: webformat)
105  # @return byte array of video
106  def downloadRaw(self, imtype = 'medium'):
107 
108  uri = None
109 
110  if (imtype == 'large'):
111  uri = self.getVideoLarge()
112  elif (imtype == 'medium'):
113  uri = self.getVideoMedium()
114  elif (imtype == 'small'):
115  uri = self.getVideoSmall()
116  elif (imtype == 'tiny'):
117  uri = self.getVideoTiny()
118  else:
119  raise ValueError('supported types is large, medium, small and tiny.', imtype, 'unsupported')
120 
121  r = requests.get(uri, allow_redirects=True)
122 
123  if (r.status_code != 200):
124  raise ValueError('Pixabay return status code != 200 for uri', uri, 'Invalid parameters?')
125 
126  return r.content
127 
128  ##
129  # Download video to file
130  # @param dst location of file to save
131  def download(self, dst, imtype = 'medium'):
132  with open(dst, 'wb') as handler:
133  handler.write(self.downloadRaw(imtype))
134 
135 
def getVideoMedium(self)
Get Height of Preview.
Definition: video.py:59
def getVideoLarge(self)
Get Height of Preview.
Definition: video.py:49
def getLikes(self)
Get likes of video.
Definition: video.py:79
def getUserId(self)
Get author id of video.
Definition: video.py:89
def getPageURL(self)
Get page url of video.
Definition: video.py:24
def getComments(self)
Get comments of video.
Definition: video.py:84
def __init__(self, data)
Init video object.
Definition: video.py:14
def getDownloads(self)
Get downloads of video.
Definition: video.py:74
def getUser(self)
Get name of author of video.
Definition: video.py:94
def getVideoTiny(self)
Get Height of Preview.
Definition: video.py:64
def getVideoSmall(self)
Get Height of Preview.
Definition: video.py:54
def downloadRaw(self, imtype='medium')
Download video to varaible.
Definition: video.py:106
def getPictureId(self)
Get width of Preview.
Definition: video.py:44
def getType(self)
Get type of video.
Definition: video.py:29
def getViews(self)
Get views of video.
Definition: video.py:69
def download(self, dst, imtype='medium')
Download video to file.
Definition: video.py:131
def getDuration(self)
Get url of Preview.
Definition: video.py:39
def getUservideoURL(self)
Get url of video of author of video.
Definition: video.py:99
def getId(self)
Get ID of video.
Definition: video.py:19
def getTags(self)
Get tags of video.
Definition: video.py:34