Skip to content

Commit a9b1783

Browse files
committed
Let api_key and proxy parameters override the config.ini file
1 parent bf0b8aa commit a9b1783

1 file changed

Lines changed: 32 additions & 20 deletions

File tree

pyrogram/client/client.py

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import tempfile
3030
import threading
3131
import time
32-
from collections import namedtuple
3332
from configparser import ConfigParser
3433
from datetime import datetime
3534
from hashlib import sha256, md5
@@ -59,8 +58,20 @@
5958

6059
log = logging.getLogger(__name__)
6160

62-
ApiKey = namedtuple("ApiKey", ["api_id", "api_hash"])
63-
Proxy = namedtuple("Proxy", ["enabled", "hostname", "port", "username", "password"])
61+
62+
class APIKey:
63+
def __init__(self, api_id: int, api_hash: str):
64+
self.api_id = api_id
65+
self.api_hash = api_hash
66+
67+
68+
class Proxy:
69+
def __init__(self, enabled: bool, hostname: str, port: int, username: str, password: str):
70+
self.enabled = enabled
71+
self.hostname = hostname
72+
self.port = port
73+
self.username = username
74+
self.password = password
6475

6576

6677
class Client:
@@ -127,7 +138,7 @@ class Client:
127138

128139
def __init__(self,
129140
session_name: str,
130-
api_key: tuple or ApiKey = None,
141+
api_key: tuple or APIKey = None,
131142
proxy: dict or Proxy = None,
132143
test_mode: bool = False,
133144
token: str = None,
@@ -790,34 +801,35 @@ def load_config(self):
790801
parser = ConfigParser()
791802
parser.read("config.ini")
792803

793-
if parser.has_section("pyrogram"):
794-
self.api_key = ApiKey(
804+
if self.api_key is not None:
805+
self.api_key = APIKey(
806+
api_id=int(self.api_key[0]),
807+
api_hash=self.api_key[1]
808+
)
809+
elif parser.has_section("pyrogram"):
810+
self.api_key = APIKey(
795811
api_id=parser.getint("pyrogram", "api_id"),
796812
api_hash=parser.get("pyrogram", "api_hash")
797813
)
798814
else:
799-
self.api_key = ApiKey(
800-
api_id=int(self.api_key[0]),
801-
api_hash=self.api_key[1]
802-
)
815+
raise AttributeError("No API Key found")
803816

804-
if parser.has_section("proxy"):
817+
if self.proxy is not None:
818+
self.proxy = Proxy(
819+
enabled=True,
820+
hostname=self.proxy["hostname"],
821+
port=int(self.proxy["port"]),
822+
username=self.proxy.get("username", None),
823+
password=self.proxy.get("password", None)
824+
)
825+
elif parser.has_section("proxy"):
805826
self.proxy = Proxy(
806827
enabled=parser.getboolean("proxy", "enabled"),
807828
hostname=parser.get("proxy", "hostname"),
808829
port=parser.getint("proxy", "port"),
809830
username=parser.get("proxy", "username", fallback=None) or None,
810831
password=parser.get("proxy", "password", fallback=None) or None
811832
)
812-
else:
813-
if self.proxy is not None:
814-
self.proxy = Proxy(
815-
enabled=True,
816-
hostname=self.proxy["hostname"],
817-
port=int(self.proxy["port"]),
818-
username=self.proxy.get("username", None),
819-
password=self.proxy.get("password", None)
820-
)
821833

822834
def load_session(self, session_name):
823835
try:

0 commit comments

Comments
 (0)