Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 332a63479e | |||
| b748a217a0 | |||
| afaef06f40 | |||
| 1948ff4a9c |
@@ -1,6 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
__version__ = "1.0.17"
|
||||
__version__ = "1.0.18"
|
||||
|
||||
import time
|
||||
from typing import Callable
|
||||
|
||||
@@ -7,7 +7,7 @@ import requests
|
||||
import tqdm
|
||||
|
||||
|
||||
def download_file(url: str, file: str | Path):
|
||||
def download_file(url: str, file: str | Path, progress: bool = True):
|
||||
"""
|
||||
Helper method handling downloading large files from `url` to `filename`.
|
||||
Returns a pointer to `filename`.
|
||||
@@ -32,11 +32,16 @@ def download_file(url: str, file: str | Path):
|
||||
tqdm_args['total'] = int(r.headers['content-length']) / 1024 / 1024
|
||||
|
||||
with open(file, 'wb') as f:
|
||||
pbar = tqdm.tqdm(unit=" MB", ncols=term_len,
|
||||
bar_format='{desc} {rate_noinv_fmt} {remaining} [{bar}] {percentage:.0f}%', ascii=' #',
|
||||
desc=file.name[:bar_len].ljust(bar_len), **tqdm_args)
|
||||
pbar = None
|
||||
if progress:
|
||||
pbar = tqdm.tqdm(unit=" MB", ncols=term_len,
|
||||
bar_format='{desc} {rate_noinv_fmt} {remaining} [{bar}] {percentage:.0f}%', ascii=' #',
|
||||
desc=file.name[:bar_len].ljust(bar_len), **tqdm_args)
|
||||
|
||||
for chunk in r.iter_content(chunk_size=chunk_size):
|
||||
if chunk:
|
||||
pbar.update(len(chunk) / 1024 / 1024)
|
||||
if pbar:
|
||||
pbar.update(len(chunk) / 1024 / 1024)
|
||||
f.write(chunk)
|
||||
|
||||
return file
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
import logging
|
||||
import os
|
||||
|
||||
|
||||
def setup_logger(debug: bool = os.environ.get("DEBUG", False)):
|
||||
# Try to use rich for pretty printing
|
||||
try:
|
||||
from rich.logging import RichHandler
|
||||
handler = RichHandler(rich_tracebacks=True)
|
||||
|
||||
from rich.traceback import install
|
||||
install(show_locals=True)
|
||||
except ImportError:
|
||||
handler = logging.StreamHandler()
|
||||
|
||||
# Initialize debug logger
|
||||
logging.basicConfig(
|
||||
level="NOTSET" if debug else "INFO",
|
||||
format="%(message)s",
|
||||
datefmt="[%X]",
|
||||
handlers=[handler]
|
||||
)
|
||||
|
||||
return logging.getLogger("a2")
|
||||
@@ -0,0 +1,27 @@
|
||||
import requests
|
||||
|
||||
|
||||
def setup_proxy(session: requests.Session, addr: str = 'socks5://localhost:9050', verbose: bool = True):
|
||||
url = 'https://ifconfig.me/ip'
|
||||
|
||||
# Setup proxy
|
||||
ip = session.get(url).text.strip()
|
||||
session.proxies = {
|
||||
'http': addr,
|
||||
'https': addr
|
||||
}
|
||||
proxy_ip = session.get(url).text.strip()
|
||||
|
||||
# Print ip
|
||||
if verbose:
|
||||
print(f'Raw ip: {ip}')
|
||||
print(f'Proxy ip: {proxy_ip}')
|
||||
|
||||
# ips shouldn't match
|
||||
assert ip != proxy_ip, 'Proxy did not start correctly.'
|
||||
|
||||
# Disable default requests behavior
|
||||
def warn(*args, **kwargs):
|
||||
raise ReferenceError('Use session.get instead of requests.get')
|
||||
requests.get = warn
|
||||
requests.post = warn
|
||||
Reference in New Issue
Block a user