88import logging
99import random
1010import sys
11+ from contextlib import nullcontext
12+ from pathlib import Path
1113from typing import IO , Any , Literal
1214from warnings import warn
1315
@@ -42,6 +44,7 @@ async def run(
4244 retry_errors : bool = True ,
4345 store_errors : bool | None = None ,
4446 eth_key : str | None = None ,
47+ trust_env : bool = False ,
4548) -> None :
4649 if stop_on_errors is not _UNSET :
4750 warn (
@@ -65,9 +68,15 @@ def write_output(content: Any) -> None:
6568 elif eth_key :
6669 auth_kwargs ["eth_key" ] = eth_key
6770 client = AsyncZyteAPI (
68- n_conn = n_conn , api_url = api_url , retrying = retrying , ** auth_kwargs
71+ n_conn = n_conn ,
72+ api_url = api_url ,
73+ retrying = retrying ,
74+ trust_env = trust_env ,
75+ ** auth_kwargs ,
6976 )
70- async with create_session (connection_pool_size = n_conn ) as session :
77+ async with create_session (
78+ connection_pool_size = n_conn , trust_env = trust_env
79+ ) as session :
7180 result_iter = client .iter (
7281 queries = queries ,
7382 session = session ,
@@ -128,7 +137,6 @@ def _get_argument_parser(program_name: str = "zyte-api") -> argparse.ArgumentPar
128137 )
129138 p .add_argument (
130139 "INPUT" ,
131- type = argparse .FileType ("r" , encoding = "utf8" ),
132140 help = (
133141 "Path to an input file (see 'Command-line client > Input file' in "
134142 "the docs for details)."
@@ -151,8 +159,7 @@ def _get_argument_parser(program_name: str = "zyte-api") -> argparse.ArgumentPar
151159 p .add_argument (
152160 "--output" ,
153161 "-o" ,
154- default = sys .stdout ,
155- type = argparse .FileType ("w" , encoding = "utf8" ),
162+ default = None ,
156163 help = (
157164 "Path for the output file. Results are written into the output "
158165 "file in JSON Lines format.\n "
@@ -225,6 +232,14 @@ def _get_argument_parser(program_name: str = "zyte-api") -> argparse.ArgumentPar
225232 ),
226233 action = "store_true" ,
227234 )
235+ p .add_argument (
236+ "--trust-env" ,
237+ help = (
238+ "Enable environment-based network settings such as HTTP_PROXY and "
239+ "HTTPS_PROXY for Zyte API requests."
240+ ),
241+ action = "store_true" ,
242+ )
228243 return p
229244
230245
@@ -234,7 +249,15 @@ def _main(program_name: str = "zyte-api") -> None:
234249 args = p .parse_args ()
235250 logging .basicConfig (stream = sys .stderr , level = getattr (logging , args .loglevel ))
236251
237- queries = read_input (args .INPUT , args .intype )
252+ if args .INPUT == "-" :
253+ with nullcontext (sys .stdin ) as input_fp :
254+ queries = read_input (input_fp , args .intype )
255+ else :
256+ try :
257+ with Path (args .INPUT ).open (encoding = "utf8" ) as input_fp :
258+ queries = read_input (input_fp , args .intype )
259+ except OSError as e :
260+ p .error (f"Cannot open input file { args .INPUT !r} : { e } " )
238261 if not queries :
239262 print ("No input queries found. Is the input file empty?" , file = sys .stderr )
240263 sys .exit (- 1 )
@@ -245,23 +268,28 @@ def _main(program_name: str = "zyte-api") -> None:
245268 queries = queries [: args .limit ]
246269
247270 logger .info (
248- f"Loaded { len (queries )} urls from { args .INPUT . name } ; shuffled: { args .shuffle } "
271+ f"Loaded { len (queries )} urls from { args .INPUT } ; shuffled: { args .shuffle } "
249272 )
250273 logger .info (f"Running Zyte API (connections: { args .n_conn } )" )
251274
252- loop = asyncio .get_event_loop ()
253- coro = run (
254- queries ,
255- out = args .output ,
256- n_conn = args .n_conn ,
257- api_url = args .api_url ,
258- api_key = args .api_key ,
259- eth_key = args .eth_key ,
260- retry_errors = not args .dont_retry_errors ,
261- store_errors = args .store_errors ,
262- )
263- loop .run_until_complete (coro )
264- loop .close ()
275+ run_kwargs = {
276+ "n_conn" : args .n_conn ,
277+ "api_url" : args .api_url ,
278+ "api_key" : args .api_key ,
279+ "eth_key" : args .eth_key ,
280+ "retry_errors" : not args .dont_retry_errors ,
281+ "store_errors" : args .store_errors ,
282+ "trust_env" : args .trust_env ,
283+ }
284+ if args .output is None or args .output == "-" :
285+ with nullcontext (sys .stdout ) as out :
286+ asyncio .run (run (queries , out = out , ** run_kwargs ))
287+ else :
288+ try :
289+ with Path (args .output ).open ("w" , encoding = "utf8" ) as out :
290+ asyncio .run (run (queries , out = out , ** run_kwargs ))
291+ except OSError as e :
292+ p .error (f"Cannot open output file { args .output !r} : { e } " )
265293
266294
267295if __name__ == "__main__" :
0 commit comments