Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

Commit 01ebc72

Browse files
committed
moved to use tempfile.TemporaryFile so more portable over many systems
1 parent 6b1cbe4 commit 01ebc72

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

examples/example_ai_speechrecognition.py

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import sys
55
import random
6+
import tempfile
67
import requests
78

89
sys.path.insert(0, os.path.abspath('.'))
@@ -20,6 +21,15 @@ def find_call(cf, verbs):
2021
m = getattr(m, verb)
2122
return m
2223

24+
def user_agent():
25+
s = random.choice([
26+
'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',
27+
'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',
28+
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0',
29+
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36',
30+
])
31+
return s
32+
2333
def doit(account_name, mp3_data):
2434

2535
# Or place these in your cloudflare.cfg file
@@ -59,28 +69,24 @@ def doit(account_name, mp3_data):
5969
def download_file(url, referer, n_requested):
6070
headers={}
6171
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]
72+
headers['User-Agent'] = user_agent()
73+
74+
# will be deleted once program exits
75+
fp = tempfile.TemporaryFile(mode='w+b')
76+
7077
n_received = 0
7178
# NOTE the stream=True parameter below
7279
with requests.get(url, headers=headers, stream=True) as r:
7380
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
81+
for chunk in r.iter_content(chunk_size=16*1024):
82+
fp.write(chunk)
83+
n_received += len(chunk)
84+
if n_received > n_requested:
85+
break
86+
87+
# rewind the file so it's ready to read
88+
fp.seek(0)
89+
return fp
8490

8591
def main():
8692
if len(sys.argv) > 1 and sys.argv[1] == '-a':
@@ -98,8 +104,8 @@ def main():
98104
referer = 'https://www.americanrhetoric.com/barackobamaspeeches.htm'
99105

100106
# 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()
107+
mp3_fp = download_file(url, referer, 680 * 1024)
108+
mp3_data = mp3_fp.read()
103109
print('mp3 received: length=%d' % (len(mp3_data)))
104110

105111
doit(account_name, mp3_data)

0 commit comments

Comments
 (0)