forked from timelany/airdrop_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
159 lines (111 loc) · 4.13 KB
/
client.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import typing, os
from telethon import sync
from sqlite3 import OperationalError
from telethon.sessions import Session
from telethon.sync import TelegramClient
from telethon.errors.rpcerrorlist import AuthKeyDuplicatedError
from telethon.errors import SessionPasswordNeededError, rpcerrorlist
from telethon.network import MTProtoSender, ConnectionTcpFull, Connection
class Client(TelegramClient):
code: int
_apiId: int
_api: tuple
_apiHash: str
errors: list
connection: bool
_proxy: typing.Union[tuple, dict] = None
_use_connection: 'typing.Type[Connection]' = ConnectionTcpFull
def __init__(
self: 'Client',
session: 'typing.Union[str, Session]' = str
):
self.session = session
self._apiId = None
self._apiHash = None
self._api = ()
self.errors = []
self.code = None
self.connection = False
self._proxy = None
self._use_connection = ConnectionTcpFull
def set_session(self, value: 'typing.Union[str, Session]' = str):
self.session = value
return self
def set_api_full(self, value: tuple):
self._api = value
return self
def set_apiId(self, value: int):
self._apiId = value
return self
def set_apiHash(self, value: str):
self._apiHash = value
return self
def use_connection(self, connection):
self._use_connection = connection
return self
def set_proxy(self, proxy: tuple|dict):
self._proxy = proxy
return self
def get_params(self):
params = ()
if self._api:
params += self._api
elif self._apiId and self._apiHash:
params += (self._apiId, self._apiHash)
return params
def perform(self):
if (self._apiId and self._apiHash) or (self._api):
try:
api_id, api_hash = self.get_params()
self._client = TelegramClient(self.session, api_id, api_hash, connection = self._use_connection, proxy = self._proxy)
self._client.connect()
if self.is_authorized():
self.code = 200
else:
self.code = 201
except AuthKeyDuplicatedError as authkeyduplicate:
self.errors.append(authkeyduplicate)
self.code = 101
except OperationalError as operationalError:
self.errors.append(operationalError)
self.code = 301
except Exception as exception:
self.errors.append(exception)
self.code = 401
else:
self.errors.append(ValueError('App ID or Api ID has been not set!'))
self.code = 501
return self
def login(self):
self._client.send_code_request(self.session)
try:
self._client.sign_in( self.session, input(' Enter the code: ') )
self.code = 200
except SessionPasswordNeededError:
try:
self._client.sign_in( password = input(' Enter the password: ') )
self.code = 200
except rpcerrorlist.PasswordHashInvalidError as invalidPassword:
self.errors.append(invalidPassword)
self.code = 203
return self
def is_authorized(self)->bool:
if self._client:
if self._client.is_user_authorized():
self.connection = True
else:
self.connection = False
return self.connection
async def disconnect(self):
try:
await self._client.disconnect()
self.code = 200
# os.remove('{}.session'.format(self.session))
except:
self.code = 205
def logout(self):
if self.is_authorized():
if self._client.log_out():
self.code = 200
def get(self):
return self._client