@@ -30,7 +30,7 @@ def user_agent():
3030 ])
3131 return s
3232
33- def doit (account_name , mp3_data ):
33+ def doit (account_name , audio_data ):
3434
3535 # Or place these in your cloudflare.cfg file
3636 os .environ ['CLOUDFLARE_API_EXTRAS' ] = '/accounts/:id/ai/run/@cf/openai/whisper'
@@ -54,9 +54,9 @@ def doit(account_name, mp3_data):
5454
5555 try :
5656 # This should be easy to call; however, the @ will not work in Python (or many languages)
57- # r = cf.accounts.ai.run.@cf.openai/whisper(account_id, data=mp3_data )
57+ # r = cf.accounts.ai.run.@cf.openai/whisper(account_id, data=audio_data )
5858 # We find the endpoint via a quick string search
59- r = find_call (cf , '/accounts/:id/ai/run/@cf/openai/whisper' ).post (account_id , data = mp3_data )
59+ r = find_call (cf , '/accounts/:id/ai/run/@cf/openai/whisper' ).post (account_id , data = audio_data )
6060 except CloudFlare .exceptions .CloudFlareAPIError as e :
6161 exit ('/ai.run %d %s - api call failed' % (e , e ))
6262
@@ -66,7 +66,7 @@ def doit(account_name, mp3_data):
6666
6767# based on ... thank you to the author
6868# https://stackoverflow.com/questions/16694907/download-large-file-in-python-with-requests
69- def download_file (url , referer , n_requested ):
69+ def download_audio_file (url , referer , n_requested ):
7070 headers = {}
7171 headers ['Referer' ] = referer
7272 headers ['User-Agent' ] = user_agent ()
@@ -88,6 +88,37 @@ def download_file(url, referer, n_requested):
8888 fp .seek (0 )
8989 return fp
9090
91+ def default_audio_clip ():
92+
93+ s = random .choice ([
94+ (
95+ 'https://www.americanrhetoric.com/mp3clipsXE/barackobama/barackobamapresidentialfarewellARXE.mp3' ,
96+ 'https://www.americanrhetoric.com/barackobamaspeeches.htm'
97+ ),
98+ (
99+ 'https://archive.org/download/DoNotGoGentleIntoThatGoodNight/gentle.ogg' ,
100+ 'https://archive.org/details/DoNotGoGentleIntoThatGoodNight'
101+ ),
102+ (
103+ 'https://www.nasa.gov/wp-content/uploads/2015/01/590333main_ringtone_eagleHasLanded_extended.mp3' ,
104+ 'https://www.nasa.gov/audio-and-ringtones/'
105+ ),
106+ (
107+ 'https://www.nasa.gov/wp-content/uploads/2015/01/590331main_ringtone_smallStep.mp3' ,
108+ 'https://www.nasa.gov/audio-and-ringtones/'
109+ ),
110+ (
111+ 'https://upload.wikimedia.org/wikipedia/en/7/7f/George_Bush_1988_No_New_Taxes.ogg' ,
112+ 'https://en.wikipedia.org/wiki/File:George_Bush_1988_No_New_Taxes.ogg'
113+ ),
114+ (
115+ 'https://archive.org/download/grand_meaulnes_2004_librivox/grandmeaulnes_01_alainfournier.mp3' ,
116+ 'https://archive.org/details/grand_meaulnes_2004_librivox/grandmeaulnes_01_alainfournier_128kb.mp3'
117+ ),
118+ ])
119+
120+ return s
121+
91122def main ():
92123 if len (sys .argv ) > 1 and sys .argv [1 ] == '-a' :
93124 del sys .argv [1 ]
@@ -100,15 +131,14 @@ def main():
100131 url = sys .argv [1 ]
101132 referer = url
102133 else :
103- url = 'https://www.americanrhetoric.com/mp3clipsXE/barackobama/barackobamapresidentialfarewellARXE.mp3'
104- referer = 'https://www.americanrhetoric.com/barackobamaspeeches.htm'
134+ url , referer = default_audio_clip ()
105135
106136 # we only grab the first 680KB of the file - that's enough to show working code
107- mp3_fp = download_file (url , referer , 680 * 1024 )
108- mp3_data = mp3_fp .read ()
109- print ('mp3 received : length=%d' % (len (mp3_data )))
137+ audio_fp = download_audio_file (url , referer , 680 * 1024 )
138+ audio_data = audio_fp .read ()
139+ print ('%s : length=%d' % (url . split ( '/' )[ - 1 :][ 0 ], len (audio_data )))
110140
111- doit (account_name , mp3_data )
141+ doit (account_name , audio_data )
112142
113143if __name__ == '__main__' :
114144 main ()
0 commit comments