-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy path_common.py
More file actions
67 lines (49 loc) · 1.77 KB
/
Copy path_common.py
File metadata and controls
67 lines (49 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from __future__ import annotations
import os
from pathlib import Path
from typing import Iterable
from aiograpi import Client
DEFAULT_SESSION_FILE = "ig_settings.json"
def env(name: str, default: str | None = None) -> str | None:
value = os.environ.get(name)
if value is None or value == "":
return default
return value
def require_env(name: str) -> str:
value = env(name)
if value is None:
raise SystemExit(f"Set {name} environment variable before running this example.")
return value
def env_int(name: str, default: int) -> int:
value = env(name)
if value is None:
return default
return int(value)
def parse_ids(values: Iterable[str] | str | None) -> list[int]:
if values is None:
return []
if isinstance(values, str):
values = values.split(",")
return [int(value.strip()) for value in values if value.strip()]
async def make_client(login: bool = True) -> Client:
kwargs = {}
public_transport = env("IG_PUBLIC_TRANSPORT")
if public_transport:
kwargs["public_transport"] = public_transport
public_transport_impersonate = env("IG_PUBLIC_TRANSPORT_IMPERSONATE")
if public_transport_impersonate:
kwargs["public_transport_impersonate"] = public_transport_impersonate
cl = Client(**kwargs)
proxy = env("IG_PROXY")
if proxy:
cl.set_proxy(proxy)
if not login:
return cl
username = require_env("IG_USERNAME")
password = require_env("IG_PASSWORD")
session_file = Path(env("IG_SESSION_FILE", DEFAULT_SESSION_FILE)).expanduser()
if session_file.exists():
cl.load_settings(session_file)
await cl.login(username, password, verification_code=env("IG_VERIFICATION_CODE", ""))
cl.dump_settings(session_file)
return cl