3232# released between then require at-least one paramater send via files= in order to not trigger a backend API bug
3333#
3434
35+ def rfc3339_iso8601_time (hour_delta = 0 , with_hms = False ):
36+ """ rfc3339_iso8601_time """
37+ # format time (with an hour offset in RFC3339 ISO8601 format (and do it UTC time)
38+ dt = (datetime .datetime .now (datetime .UTC ).replace (microsecond = 0 ) + datetime .timedelta (hours = hour_delta ))
39+ if with_hms :
40+ return dt .isoformat ().replace ('+00:00' , 'Z' )
41+ return dt .strftime ('%Y-%m-%d' )
42+
3543def method_from_library_version ():
44+ """ method_from_library_version """
3645 if CloudFlare .__version__ <= '2.14.2' :
3746 print ('Using %s version of Cloudflare python library - hence do not need data= or files=; but use files= if passing anything' % (CloudFlare .__version__ ))
3847 return ''
@@ -44,6 +53,7 @@ def method_from_library_version():
4453 return 'USE-DATA'
4554
4655def doit (account_name , image_filename ):
56+ """ doit """
4757
4858 # https://developers.cloudflare.com/stream/uploading-videos/direct-creator-uploads/
4959 # https://developers.cloudflare.com/api/operations/cloudflare-images-create-authenticated-direct-upload-url-v-2
@@ -53,16 +63,16 @@ def doit(account_name, image_filename):
5363 params = {'name' : account_name , 'per_page' : 1 }
5464 accounts = cf .accounts .get (params = params )
5565 except CloudFlare .exceptions .CloudFlareAPIError as e :
56- exit ('%s: %d %s - api call failed' % ('/accounts' , e , e ))
66+ sys . exit ('%s: %d %s - api call failed' % ('/accounts' , int ( e ), str ( e ) ))
5767 try :
5868 account_id = accounts [0 ]['id' ]
5969 except IndexError :
60- exit ('%s: account name not found' % (account_name ))
70+ sys . exit ('%s: account name not found' % (account_name ))
6171
6272 try :
6373 image_fp = open (image_filename , 'rb' )
6474 except Exception as e :
65- exit ('%s: %s - file read failed' % (image_filename , e ))
75+ sys . exit ('%s: %s - file read failed' % (image_filename , e ))
6676
6777 image_filesize = os .fstat (image_fp .fileno ()).st_size
6878 if image_filesize > 1024 * 1024 * 1024 :
@@ -75,7 +85,7 @@ def doit(account_name, image_filename):
7585 print ('%s: filesize = %d Bytes' % (image_filename , image_filesize ))
7686
7787 # format future time in RFC3339 format (and do it UTC time)
78- time_plus_one_hour_in_iso = ( datetime . datetime . utcnow (). replace ( microsecond = 0 ) + datetime . timedelta ( hours = 1 )). isoformat () + 'Z'
88+ time_plus_one_hour_in_iso = rfc3339_iso8601_time ( 1 , True )
7989
8090 # direct_upload uses multipart/form-data and hence this info is passed as files (but None for filename)
8191 # these are the four form values
@@ -115,7 +125,7 @@ def doit(account_name, image_filename):
115125 try :
116126 r = cf .accounts .images .v2 .direct_upload .post (account_id , data = data , files = files )
117127 except CloudFlare .exceptions .CloudFlareAPIError as e :
118- exit ('%s: %d %s - api call failed' % ('/accounts/images/v2/direct_upload' , e , e ))
128+ sys . exit ('%s: %d %s - api call failed' % ('/accounts/images/v2/direct_upload' , int ( e ), str ( e ) ))
119129 print ('v2 new image post results' )
120130 print (json .dumps (r , indent = 4 ))
121131
@@ -128,9 +138,9 @@ def doit(account_name, image_filename):
128138 # https://upload.videodelivery.net/f65014bc6ff5419ea86e7972a047ba22
129139
130140 try :
131- r = requests .post (image_url , files = {('file' , image_fp ) } )
141+ r = requests .post (image_url , files = {('file' , image_fp )}, timeout = 5 )
132142 except Exception as e :
133- exit ('%s: %s - api call failed' % (image_url , e ))
143+ sys . exit ('%s: %s - api call failed' % (image_url , e ))
134144
135145 image_fp .close ()
136146
@@ -139,20 +149,20 @@ def doit(account_name, image_filename):
139149 print ('403 means you need to enable images in your account' )
140150 if r .status_code == 403 :
141151 print ('415 means the file is a bad image format' )
142- exit ('%s: HTTP Error %s' % (image_url , r .status_code ))
152+ sys . exit ('%s: HTTP Error %s' % (image_url , r .status_code ))
143153
144154 j = r .json ()
145- if j ['success' ] == True :
155+ if j ['success' ] is True :
146156 print ('Image upload results' )
147157 print (json .dumps (j ['result' ], indent = 4 ))
148158 else :
149- exit ('Error:\n errors: %s\n messages: %s' % (image_url , j ['errors' ], j ['messages' ]))
159+ sys . exit ('Error:\n errors: %s\n messages: %s' % (j ['errors' ], j ['messages' ]))
150160
151161 # list all images
152162 try :
153163 r = cf .accounts .images .v2 (account_id )
154164 except CloudFlare .exceptions .CloudFlareAPIError as e :
155- exit ('%s: %d %s - api call failed' % ('/accounts/images/v1' , e , e ))
165+ sys . exit ('%s: %d %s - api call failed' % ('/accounts/images/v1' , int ( e ), str ( e ) ))
156166
157167 print ('All account images:' )
158168 for img in r ['images' ]:
@@ -167,21 +177,22 @@ def doit(account_name, image_filename):
167177 try :
168178 r = cf .accounts .images .v1 .delete (account_id , image_id )
169179 except CloudFlare .exceptions .CloudFlareAPIError as e :
170- exit ('%s: %d %s - api call failed' % ('/accounts/images/v1' , e , e ))
180+ sys . exit ('%s: %d %s - api call failed' % ('/accounts/images/v1' , int ( e ), str ( e ) ))
171181 print ('Image delete' )
172182 print (json .dumps (r , indent = 4 ))
173183
174184def main ():
185+ """ main """
175186 try :
176187 account_name = sys .argv [1 ]
177188 except IndexError :
178- exit ('usage: example_images_v2_direct_upload.py account_name image_filename' )
189+ sys . exit ('usage: example_images_v2_direct_upload.py account_name image_filename' )
179190 try :
180191 image_filename = sys .argv [2 ]
181192 except IndexError :
182- exit ('usage: example_images_v2_direct_upload.py account_name image_filename' )
193+ sys . exit ('usage: example_images_v2_direct_upload.py account_name image_filename' )
183194 doit (account_name , image_filename )
184- exit (0 )
195+ sys . exit (0 )
185196
186197if __name__ == '__main__' :
187198 main ()
0 commit comments