|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | +import random |
| 6 | +import requests |
| 7 | + |
| 8 | +sys.path.insert(0, os.path.abspath('.')) |
| 9 | +import CloudFlare |
| 10 | + |
| 11 | +def find_call(cf, verbs): |
| 12 | + # So we walk over the @ via a getattr() call. |
| 13 | + # We also have to deal with a . in a verb - that does not work in Python. So sad. |
| 14 | + # Also, the - is actually an _ in this Python library. |
| 15 | + # This is not normally needed for other calls |
| 16 | + m = cf |
| 17 | + for verb in verbs.split('/'): |
| 18 | + if verb == '' or verb[0] == ':': |
| 19 | + continue |
| 20 | + m = getattr(m, verb) |
| 21 | + return m |
| 22 | + |
| 23 | +def doit(account_name, mp3_data): |
| 24 | + |
| 25 | + # Or place these in your cloudflare.cfg file |
| 26 | + os.environ['CLOUDFLARE_API_EXTRAS'] = '/accounts/:id/ai/run/@cf/openai/whisper' |
| 27 | + |
| 28 | + # We set the timeout because these AI calls take longer than normal API calls |
| 29 | + cf = CloudFlare.CloudFlare(global_request_timeout=120) |
| 30 | + |
| 31 | + try: |
| 32 | + if account_name == None or account_name == '': |
| 33 | + params = {'per_page': 1} |
| 34 | + else: |
| 35 | + params = {'name': account_name, 'per_page': 1} |
| 36 | + accounts = cf.accounts.get(params=params) |
| 37 | + except CloudFlare.exceptions.CloudFlareAPIError as e: |
| 38 | + exit('/accounts %d %s - api call failed' % (e, e)) |
| 39 | + |
| 40 | + try: |
| 41 | + account_id = accounts[0]['id'] |
| 42 | + except IndexError: |
| 43 | + exit('%s: account name not found' % (account_name)) |
| 44 | + |
| 45 | + try: |
| 46 | + # This should be easy to call; however, the @ will not work in Python (or many languages) |
| 47 | + # r = cf.accounts.ai.run.@cf.openai/whisper(account_id, data=mp3_data) |
| 48 | + # We find the endpoint via a quick string search |
| 49 | + r = find_call(cf, '/accounts/:id/ai/run/@cf/openai/whisper').post(account_id, data=mp3_data) |
| 50 | + except CloudFlare.exceptions.CloudFlareAPIError as e: |
| 51 | + exit('/ai.run %d %s - api call failed' % (e, e)) |
| 52 | + |
| 53 | + print('%s' % (r['text'])) |
| 54 | + # words = [word['word'] for word in r['words']] |
| 55 | + # print('%s' % ('|'.join(words))) |
| 56 | + |
| 57 | +# based on ... thank you to the author |
| 58 | +# https://stackoverflow.com/questions/16694907/download-large-file-in-python-with-requests |
| 59 | +def download_file(url, referer, n_requested): |
| 60 | + headers={} |
| 61 | + headers['Referer'] = referer |
| 62 | + headers['User-Agent'] = random.choice([ |
| 63 | + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Safari/605.1.15', |
| 64 | + 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.101 Safari/537.36 Edg/91.0.864.48', |
| 65 | + 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0', |
| 66 | + 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36', |
| 67 | + ]) |
| 68 | + |
| 69 | + local_filename = '/tmp/' + url.split('/')[-1] |
| 70 | + n_received = 0 |
| 71 | + # NOTE the stream=True parameter below |
| 72 | + with requests.get(url, headers=headers, stream=True) as r: |
| 73 | + r.raise_for_status() |
| 74 | + with open(local_filename, 'wb') as f: |
| 75 | + for chunk in r.iter_content(chunk_size=16*1024): |
| 76 | + # If you have chunk encoded response uncomment if |
| 77 | + # and set chunk_size parameter to None. |
| 78 | + #if chunk: |
| 79 | + f.write(chunk) |
| 80 | + n_received += len(chunk) |
| 81 | + if n_received > n_requested: |
| 82 | + break |
| 83 | + return local_filename |
| 84 | + |
| 85 | +def main(): |
| 86 | + if len(sys.argv) > 1 and sys.argv[1] == '-a': |
| 87 | + del sys.argv[1] |
| 88 | + account_name = sys.argv[1] |
| 89 | + del sys.argv[1] |
| 90 | + else: |
| 91 | + account_name = None |
| 92 | + |
| 93 | + if len(sys.argv) > 1: |
| 94 | + url = sys.argv[1] |
| 95 | + referer = url |
| 96 | + else: |
| 97 | + url = 'https://www.americanrhetoric.com/mp3clipsXE/barackobama/barackobamapresidentialfarewellARXE.mp3' |
| 98 | + referer = 'https://www.americanrhetoric.com/barackobamaspeeches.htm' |
| 99 | + |
| 100 | + # we only grab the first 680KB of the file - that's enough to show working code |
| 101 | + mp3_file = download_file(url, referer, 680 * 1024) |
| 102 | + mp3_data = open(mp3_file, "rb").read() |
| 103 | + print('mp3 received: length=%d' % (len(mp3_data))) |
| 104 | + |
| 105 | + doit(account_name, mp3_data) |
| 106 | + |
| 107 | +if __name__ == '__main__': |
| 108 | + main() |
0 commit comments