-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapi.py
3108 lines (2595 loc) · 107 KB
/
api.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Module with all API functions and classes."""
try:
from regex import search as re_search
except ImportError:
from re import search as re_search
from asyncio import iscoroutinefunction
from filetype import guess as filetype_guess
from telethon import TelegramClient
from telethon.sessions import StringSession
from telethon.errors import SessionPasswordNeededError
from telethon.tl.custom.file import File
from telethon.tl.functions.messages import EditChatAboutRequest
from telethon.tl.functions.channels import (
CreateChannelRequest, EditPhotoRequest,
GetFullChannelRequest, DeleteChannelRequest
)
from telethon.tl.types import (
Channel, Message, Photo,
PeerChannel, Document
)
from telethon import events
from telethon.tl.types.auth import SentCode
from .crypto import get_rnd_bytes
from .crypto import AESwState as AES
from .keys import (
make_filekey, make_requestkey,
make_sharekey, MainKey, RequestKey,
ShareKey, ImportKey, FileKey, BaseKey,
EncryptedMainkey, make_mainkey
)
from .constants import (
VERSION, VERBYTE, BOX_IMAGE_PATH, DEF_TGBOX_NAME,
DOWNLOAD_PATH, API_ID, API_HASH, FILESIZE_MAX,
FILE_NAME_MAX, FOLDERNAME_MAX, COMMENT_MAX,
PREVIEW_MAX, DURATION_MAX, DEF_NO_FOLDER, NAVBYTES_SIZE
)
from .fastelethon import upload_file, download_file
from .db import TgboxDB
from . import loop
from .errors import (
IncorrectKey, NotInitializedError,
InUseException, BrokenDatabase,
AlreadyImported, RemoteFileNotFound,
NotImported, AESError, PreviewImpossible,
DurationImpossible
)
from .tools import (
int_to_bytes, bytes_to_int, RemoteBoxFileMetadata,
make_media_preview, SearchFilter, OpenPretender,
make_folder_id, get_media_duration, float_to_bytes,
bytes_to_float, prbg, anext, pad_request_size
)
from typing import (
BinaryIO, Union, NoReturn, Callable,
AsyncGenerator, List, Optional
)
from sqlite3 import IntegrityError
from dataclasses import dataclass
from os.path import getsize
from pathlib import Path
from os import PathLike
from io import BytesIO
from time import time
from base64 import (
urlsafe_b64encode,
urlsafe_b64decode
)
__all__ = [
'make_remote_box',
'get_remote_box',
'make_local_box',
'get_local_box',
'TelegramAccount',
'EncryptedRemoteBox',
'DecryptedRemoteBox',
'EncryptedRemoteBoxFile',
'DecryptedRemoteBoxFile',
'EncryptedLocalBox',
'DecryptedLocalBox',
'LocalBoxFolder',
'EncryptedLocalBoxFile',
'DecryptedLocalBoxFile',
'FutureFile'
]
TelegramClient.__version__ = VERSION
async def _search_func(
sf: SearchFilter,
ta: Optional['TelegramAccount'] = None,
mainkey: Optional[MainKey] = None,
it_messages: Optional[AsyncGenerator] = None,
lb: Optional[Union['DecryptedLocalBox', 'EncryptedLocalBox']] = None) -> AsyncGenerator:
"""
Function used to search for files in dlb and rb. It's
only for internal use, and you shouldn't use it in your
own projects. ``ta`` must be specified with ``it_messages``.
If file is imported from other ``RemoteBox`` and was exported
to your LocalBox, then we can specify box as ``lb``. AsyncGenerator
will try to get FILEKEY and decrypt ``EncryptedRemoteBoxFile``.
Otherwise imported file will be ignored.
"""
in_func = re_search if sf.re else lambda p,s: p in s
iter_from = it_messages if it_messages else lb.files()
if not iter_from:
raise ValueError('At least it_messages or lb must be specified.')
async for file in iter_from:
if hasattr(file,'document') and file.document:
try:
file = await EncryptedRemoteBoxFile(file, ta).init()
if hasattr(lb, '_mainkey') and not mainkey:
if isinstance(lb._mainkey, EncryptedMainkey):
mainkey = None
else:
mainkey = lb._mainkey
file = file if not mainkey else await file.decrypt(mainkey)
except ValueError: # Incorrect padding. Imported file?
if lb and isinstance(lb, DecryptedLocalBox):
try:
dlbfi = await lb.get_file(file.id, cache_preview=False)
# Mostly, it's not a Tgbox file, so continue.
except BrokenDatabase:
continue
if not dlbfi:
continue
else:
file = await file.decrypt(dlbfi._filekey)
else:
continue
if isinstance(file, (EncryptedRemoteBoxFile, DecryptedRemoteBoxFile)):
file_size = file.file_size
elif isinstance(file, (EncryptedLocalBoxFile, DecryptedLocalBoxFile)):
file_size = file.size
else:
continue
if sf.exported is not None:
if file.exported != sf.exported:
continue
for size in sf.min_size:
if file_size >= size:
break
else:
if sf.min_size: continue
for size in sf.max_size:
if file_size <= size:
break
else:
if sf.max_size: continue
for time in sf.time:
if all((file.upload_time - time > 0, file.upload_time - time <= 86400)):
break
else:
if sf.time: continue
for comment in sf.comment:
if file.comment and in_func(comment, file.comment):
break
else:
if sf.comment: continue
for folder in sf.folder:
if in_func(folder, file.foldername):
break
else:
if sf.folder: continue
for file_name in sf.file_name:
if in_func(file_name, file.file_name):
break
else:
if sf.file_name: continue
for file_salt in sf.file_salt:
if in_func(file_salt, rbf.file_salt):
break
else:
if sf.file_salt: continue
for verbyte in sf.verbyte:
if verbyte == rbf.verbyte:
break
else:
if sf.verbyte: continue
yield file
async def make_remote_box(
ta: 'TelegramAccount',
tgbox_db_name: str=DEF_TGBOX_NAME,
box_image_path: Union[PathLike, str] = BOX_IMAGE_PATH,
box_salt: Optional[bytes] = None) -> 'EncryptedRemoteBox':
"""
Function used for making ``RemoteBox``.
Arguments:
ta (``TelegramAccount``):
Account to make private Telegram channel.
You must be signed in via ``sign_in()``.
tgbox_db_name (``TgboxDB``, optional):
Name of your Local and Remote boxes.
``constants.DEF_TGBOX_NAME`` by default.
box_image_path (``PathLike``, optional):
``PathLike`` to image that will be used as
``Channel`` photo of your ``RemoteBox``.
Can be setted to ``None`` if you don't
want to set ``Channel`` photo.
box_salt (``bytes``, optional):
Random 32 bytes. Will be used in ``MainKey``
creation. Default is ``crypto.get_rnd_bytes()``.
"""
if box_salt and len(box_salt) != 32:
raise ValueError('Box salt len != 32')
tgbox_db = await TgboxDB.create(tgbox_db_name)
if (await tgbox_db.BoxData.count_rows()):
raise InUseException(f'TgboxDB "{tgbox_db.name}" in use. Specify new.')
channel_name = 'tgbox: ' + tgbox_db.name
box_salt = urlsafe_b64encode(box_salt if box_salt else get_rnd_bytes())
channel = (await ta.TelegramClient(
CreateChannelRequest(channel_name,'',megagroup=False))).chats[0]
if box_image_path:
box_image = await ta.TelegramClient.upload_file(open(box_image_path,'rb'))
await ta.TelegramClient(EditPhotoRequest(channel, box_image))
await ta.TelegramClient(EditChatAboutRequest(channel, box_salt.decode()))
return EncryptedRemoteBox(channel, ta)
async def get_remote_box(
dlb: Optional['DecryptedLocalBox'] = None,
ta: Optional['TelegramAccount'] = None,
entity: Optional[Union[int, str, PeerChannel]] = None)\
-> Union['EncryptedRemoteBox', 'DecryptedRemoteBox']:
"""
Returns ``EncryptedRemoteBox`` or
``DecryptedRemoteBox`` if you specify ``dlb``.
.. note::
Must be specified at least ``dlb`` or ``ta`` with ``entity``.
Arguments:
dlb (``DecryptedLocalBox``, optional):
Should be specified if ``ta`` is ``None``.
ta (``TelegramAccount``, optional):
Should be specified if ``dlb`` is ``None``.
``entity`` should be specified with ``ta``.
Note that ``ta`` must be already connected
with Telegram via ``await ta.connect()``.
entity (``PeerChannel``, ``int``, ``str``, optional):
Can be ``Channel`` ID, Username or ``PeerChannel``.
Will be used if specified. Must be specified with ``ta``.
"""
if ta:
account = ta
elif ta and not entity:
raise ValueError('entity must be specified with ta')
else:
account = TelegramAccount(session=dlb._session)
await account.connect()
entity = entity if entity else PeerChannel(dlb._box_channel_id)
channel_entity = await account.TelegramClient.get_entity(entity)
if not dlb:
return EncryptedRemoteBox(channel_entity, account)
else:
return await EncryptedRemoteBox(
channel_entity, account).decrypt(dlb=dlb)
async def make_local_box(
erb: 'EncryptedRemoteBox',
ta: 'TelegramAccount',
basekey: BaseKey) -> 'DecryptedLocalBox':
"""
Makes LocalBox
Arguments:
erb (``RemoteBox``):
``EncryptedRemoteBox``. You will
recieve it after ``make_remote_box``.
ta (``TelegramAccount``):
``TelegramAccount`` connected to Telegram.
basekey (``BaseKey``):
``BaseKey`` that will be used
for ``MainKey`` creation.
"""
tgbox_db = await TgboxDB.create(await erb.get_box_name())
if (await tgbox_db.BoxData.count_rows()):
raise InUseException(f'TgboxDB "{tgbox_db.name}" in use. Specify new.')
box_salt = await erb.get_box_salt()
mainkey = make_mainkey(basekey, box_salt)
await tgbox_db.BoxData.insert(
AES(mainkey).encrypt(int_to_bytes(0)),
AES(mainkey).encrypt(int_to_bytes(erb._box_channel_id)),
AES(mainkey).encrypt(int_to_bytes(int(time()))),
box_salt,
None, # We aren't cloned box, so Mainkey is empty
AES(basekey).encrypt(ta.get_session().encode()),
)
return await EncryptedLocalBox(tgbox_db).decrypt(basekey)
async def get_local_box(
basekey: Optional[BaseKey] = None,
tgbox_db_path: Optional[Union[PathLike, str]] = DEF_TGBOX_NAME,
) -> Union['EncryptedLocalBox', 'DecryptedLocalBox']:
"""
Returns LocalBox.
Arguments:
basekey (``BaseKey``, optional):
Returns ``DecryptedLocalBox`` if specified,
``EncryptedLocalBox`` otherwise (default).
tgbox_db_path (``PathLike``, ``str``, optional):
``PathLike`` to your TgboxDB (LocalBox). Default
is ``constants.DEF_TGBOX_NAME``.
"""
if not isinstance(tgbox_db_path, PathLike):
tgbox_db_path = Path(tgbox_db_path)
if not tgbox_db_path.exists():
raise FileNotFoundError(f'Can\'t open {tgbox_db_path.absolute()}')
else:
tgbox_db = await TgboxDB(tgbox_db_path).init()
if basekey:
return await EncryptedLocalBox(tgbox_db).decrypt(basekey)
else:
return await EncryptedLocalBox(tgbox_db).init()
class TelegramAccount:
"""
Wrapper around ``telethon.TelegramClient``
Typical usage:
.. code-block:: python
from asyncio import run as asyncio_run
from tgbox.api import TelegramAccount, make_remote_box
from getpass import getpass # For hidden input
async def main():
ta = TelegramAccount(
phone_number = input('Phone: ')
)
await ta.connect()
await ta.send_code_request()
await ta.sign_in(
code = int(input('Code: ')),
password = getpass('Pass: ')
)
erb = await make_remote_box(ta)
asyncio_run(main())
"""
def __init__(
self, api_id: int=API_ID,
api_hash: str=API_HASH,
phone_number: Optional[str] = None,
session: Optional[Union[str, StringSession]] = None):
"""
Arguments:
api_id (``int``, optional):
API_ID from https://my.telegram.org.
``constants.API_ID`` by default.
api_hash (``int``, optional):
API_HASH from https://my.telegram.org.
``constants.API_HASH`` by default.
phone_number (``str``, optional):
Phone number linked to your Telegram
account. You may want to specify it
to recieve log-in code. You should
specify it if ``session`` is ``None``.
session (``str``, ``StringSession``, optional):
``StringSession`` that give access to
your Telegram account. You can get it
after connecting and signing in via
``TelegramAccount.get_session()`` method.
"""
self._api_id, self._api_hash = api_id, api_hash
self._phone_number = phone_number
self.TelegramClient = TelegramClient(
StringSession(session),
self._api_id, self._api_hash, loop=loop
)
async def signed_in(self) -> bool:
"""Returns ``True`` if you logged in account"""
return await self.TelegramClient.is_user_authorized()
async def connect(self) -> 'TelegramAccount':
"""
Connects to Telegram. Typically
you will use this method if you have
``StringSession`` (``session`` specified).
"""
await self.TelegramClient.connect()
return self
async def send_code_request(self, force_sms: bool=False) -> SentCode:
"""
Sends the Telegram code needed to login to the given phone number.
Arguments:
force_sms (``bool``, optional):
Whether to force sending as SMS.
"""
return await self.TelegramClient.send_code_request(self._phone_number)
async def sign_in(
self, password: Optional[str] = None,
code: Optional[int] = None) -> None:
"""
Logs in to Telegram to an existing user account.
You should only use this if you are not signed in yet.
Arguments:
password (``str``, optional):
Your 2FA password. You can ignore
this if you don't enabled it yet.
code (``int``, optional):
The code that Telegram sent you after calling
``TelegramAccount.send_code_request()`` method.
"""
if not await self.TelegramClient.is_user_authorized():
try:
await self.TelegramClient.sign_in(self._phone_number, code)
except SessionPasswordNeededError:
await self.TelegramClient.sign_in(password=password)
async def log_out(self) -> bool:
"""
Logs out from Telegram. Returns ``True``
if the operation was successful.
"""
return await self.TelegramClient.log_out()
async def resend_code(self, phone_code_hash: str) -> SentCode:
"""
Send log-in code again. This can be used to
force Telegram send you SMS or Call to dictate code.
Arguments:
phone_code_hash (``str``):
You can get this hash after calling
``TelegramAccount.send_code_request()``.
Example:
.. code-block:: python
sent_code = await tg_account.send_code_request()
sent_code = await tg_account.resend_code(sent_code.phone_code_hash)
"""
return await self.TelegramClient(
ResendCodeRequest(self._phone_number, phone_code_hash)
)
def get_session(self) -> str:
"""Returns ``StringSession`` as ``str``"""
return self.TelegramClient.session.save()
async def tgboxes(self, yield_with: str='tgbox: ') -> AsyncGenerator:
"""
Iterate over all Tgbox Channels in your account.
It will return any channel with Tgbox prefix,
``"tgbox: "`` by default, you can override this.
Arguments:
yield_with (``str``):
Any channel that have ``in`` title this
string will be returned as ``RemoteBox``.
"""
async for d in self.TelegramClient.iter_dialogs():
if yield_with in d.title and d.is_channel:
yield EncryptedRemoteBox(d, self)
class EncryptedRemoteBox:
"""
*RemoteBox* is a remote cloud storage. You can
upload files and download them later.
Locally we only keep info about files (in *LocalBox*).
You can fully restore your LocalBox from RemoteBox.
.. note::
In ``EncryptedRemoteBox`` you should specify ``MainKey``
or ``DecryptedLocalBox``. Usually you want to use
``DecryptedRemoteBox``, not this class.
Typical usage:
.. code-block:: python
from tgbox.api import (
TelegramAccount,
make_local_box,
make_remote_box
)
from getpass import getpass
from asyncio import run as asyncio_run
async def main():
# Connecting and logging to Telegram
ta = TelegramAccount(
phone_number = input('Phone: ')
)
await ta.connect()
await ta.send_code_request()
await ta.sign_in(
code = int(input('Code: ')),
password = getpass('Pass: ')
)
# Making base RemoteBox (EncryptedRemoteBox)
erb = await make_remote_box(ta)
asyncio_run(main())
"""
def __init__(self, box_channel: Channel, ta: TelegramAccount):
"""
Arguments:
box_channel (``Channel``):
Telegram channel that represents
``RemoteBox``. By default have "tgbox: "
prefix and always encoded by urlsafe
b64encode BoxSalt in description.
ta (``TelegramAccount``):
Telegram account that have ``box_channel``.
"""
self._ta = ta
self._enc_class = True
self._box_channel = box_channel
self._box_channel_id = box_channel.id
self._box_salt = None
# We can't use await in __init__, so
# you should call get_box_salt for first.
self._box_name = None
# Similar to box_salt, call get_box_name.
def __hash__(self) -> int:
# Without 22 hash of int wil be equal to object's
return hash((self._box_channel_id, 22))
def __eq__(self, other) -> bool:
return all((
isinstance(other, self.__class__),
self._box_channel_id == other.box_channel_id
))
@property
def event(self) -> events.NewMessage:
"""
Will return ``events.NewMessage`` for
``Channel`` of this *RemoteBox*.
You can use it in Telethon's decorator,
see *"Events Reference"* in Docs.
"""
return events.NewMessage(chats=self.box_channel_id)
@property
def ta(self) -> TelegramAccount:
"""Returns ``TelegramAccount``"""
return self._ta
@property
def is_enc_class(self) -> bool:
"""
Returns ``True`` if you call it on
``EncryptedRemoteBox``.
"""
return self._enc_class
@property
def box_channel(self) -> Channel:
"""Returns instance of ``Channel``"""
return self._box_channel
@property
def box_channel_id(self) -> int:
"""Returns box channel id"""
return self._box_channel_id
async def get_box_salt(self) -> bytes:
"""
Returns BoxSalt. Will be cached
after first method call.
"""
if not self._box_salt:
full_rq = await self._ta.TelegramClient(
GetFullChannelRequest(channel=self._box_channel)
)
self._box_salt = urlsafe_b64decode(full_rq.full_chat.about)
return self._box_salt
async def get_box_name(self):
"""
Returns name of ``RemoteBox``.
Will be cached after first method call.
"""
if not self._box_name:
entity = await self._ta.TelegramClient.get_entity(self._box_channel_id)
self._box_name = entity.title.split(': ')[1]
return self._box_name
async def file_exists(self, id: int) -> bool:
"""
Returns ``True`` if file with specified ``id``
exists in RemoteBox. ``False`` otherwise.
Arguments:
id (``int``):
File ID.
"""
if await self.get_file(id, decrypt=False):
return True
else:
return False
async def get_file(
self,
id: int,
key: Optional[Union[MainKey, FileKey, ImportKey]] = None,
dlb: Optional['DecryptedLocalBox'] = None,
decrypt: bool=True,
ignore_errors: bool=True,
return_imported_as_erbf: bool=False,
cache_preview: bool=True) -> Union[
'EncryptedRemoteBoxFile',
'DecryptedRemoteBoxFile', None
]:
"""
Returns file from the ``RemoteBox`` by the given ID.
.. note::
You may ignore ``key` and ``dlb`` if you call
this method on ``DecryptedRemoteBox``.
Arguments:
id (``int``):
File ID.
key (``MainKey``, ``FileKey``, optional):
Will be used to decrypt ``EncryptedRemoteBoxFile``.
dlb (``DecryptedLocalBox``, optional):
If file in your ``RemoteBox`` was imported from
other ``RemoteBox`` then you can't decrypt it with
specified mainkey, but if you already imported it
to your LocalBox, then you can specify ``dlb`` and we
will use ``FILE_KEY`` from the Database.
If ``decrypt`` specified but there is no ``key``,
then we try to use mainkey from this ``dlb``.
This kwarg works in tandem with ``ignore_errors``
and ``return_imported_as_erbf`` if dlb doesn't have
this file (tip: you need to import it with ``dlb.import_file``.
decrypt (``bool``, optional):
Returns ``DecryptedRemoteBoxFile`` if ``True`` (default),
``EncryptedRemoteBoxFile`` otherwise.
ignore_errors (``bool``, optional):
Ignore all errors related to decryption of the
files in your ``RemoteBox``. If ``True``, (by default)
only returns file that was successfully decrypted. Can
be useful if you have files that was imported from other
``RemoteBox`` and you don't want to specify ``dlb``.
return_imported_as_erbf (``bool``, optional):
If specified, returns file that method can't
decrypt (if imported) as ``EncryptedRemoteBoxFile``.
cache_preview (``bool``, optional):
Cache preview in returned by method
RemoteBoxFiles or not. ``True`` by default.
"""
if hasattr(self, '_mainkey'):
key = self._mainkey
if hasattr(self, '_dlb'):
dlb = self._dlb
file_iter = self.files(
key, dlb=dlb, decrypt=decrypt,
ids=id, cache_preview=cache_preview,
return_imported_as_erbf=return_imported_as_erbf,
ignore_errors=ignore_errors
)
try:
return await anext(file_iter)
# If there is no file by ``id``.
except StopAsyncIteration:
return None
async def files(
self, key: Optional[Union[MainKey, FileKey]] = None,
dlb: Optional['DecryptedLocalBox'] = None, *,
ignore_errors: bool=True,
return_imported_as_erbf: bool=False,
limit: Optional[int] = None,
offset_id: int=0, max_id: int=0,
min_id: int=0, add_offset: int=0,
search: Optional[str] = None,
from_user: Optional[Union[str, int]] = None,
wait_time: Optional[float] = None,
ids: Optional[Union[int, List[int]]] = None,
reverse: bool=False, decrypt: bool=True,
cache_preview: bool=True) -> AsyncGenerator[
Union['EncryptedRemoteBoxFile',
'DecryptedRemoteBoxFile'],
None
]:
"""
Yields every RemoteBoxFile from ``RemoteBox``.
.. note::
- The default order is from newest to oldest, but this\
behaviour can be changed with the ``reverse`` parameter.
- You may ignore ``key`` and ``dlb`` if you call\
this method on ``DecryptedRemoteBox``.
Arguments:
key (``MainKey``, ``FileKey``, optional):
Will be used to decrypt ``EncryptedRemoteBoxFile``.
dlb (``DecryptedLocalBox``, optional):
If file in your ``RemoteBox`` was imported from
other ``RemoteBox`` then you can't decrypt it with
specified mainkey, but if you already imported it
to your LocalBox, then you can specify dlb and we
will use ``FILE_KEY`` from the Database.
If ``decrypt`` specified but there is no ``key``,
then we try to use mainkey from this dlb.
This kwarg works in tandem with ``ignore_errors``
and ``return_imported_as_erbf`` if dlb doesn't have
this file (tip: you need to import it with ``dlb.import_file``.
ignore_errors (``bool``, optional):
Ignore all errors related to decryption of the
files in your ``RemoteBox``. If ``True``, (by default)
only yields files that was successfully decrypted. Can
be useful if you have files that was imported from other
``RemoteBox`` and you don't want to specify dlb.
return_imported_as_erbf (``bool``, optional):
If specified, yields files that generator can't
decrypt (imported) as ``EncryptedRemoteBoxFile``.
limit (``int`` | ``None``, optional):
Number of files to be retrieved. Due to limitations with
the API retrieving more than 3000 messages will take longer
than half a minute (or even more based on previous calls).
The limit may also be ``None``, which would eventually return
the whole history.
offset_id (``int``, optional):
Offset message ID (only remote files *previous* to the given
ID will be retrieved). Exclusive.
max_id (``int``, optional):
All the remote box files with a higher (newer) ID
or equal to this will be excluded.
min_id (``int``, optional):
All the remote box files with a lower (older) ID
or equal to this will be excluded.
add_offset (``int``, optional):
Additional message offset (all of the specified offsets +
this offset = older files).
search (``str``, optional):
The string to be used as a search query.
from_user (``str``, ``int``, optional):
Only messages from this entity will be returned.
wait_time (``int``, optional):
Wait time (in seconds) between different
``GetHistoryRequest`` (Telethon). Use this parameter to avoid hitting
the ````FloodWaitError```` as needed. If left to ``None``, it will
default to 1 second only if the limit is higher than 3000.
If the ````ids```` parameter is used, this time will default
to 10 seconds only if the amount of IDs is higher than 300.
ids (``int``, ``list``, optional):
A single integer ID (or several IDs) for the box files that
should be returned. This parameter takes precedence over
the rest (which will be ignored if this is set). This can
for instance be used to get the file with ID 123 from
a box channel. Note that if the file-message doesn't exist,
``None`` will appear in its place, so that zipping the list of IDs
with the files can match one-to-one.
reverse (``bool``, optional):
If set to ``True``, the remote files will be returned in reverse
order (from oldest to newest, instead of the default newest
to oldest). This also means that the meaning of ``offset_id``
parameter is reversed, although ``offset_id`` still be exclusive.
``min_id`` becomes equivalent to ``offset_id`` instead of being ``max_id``
as well since files are returned in ascending order.
decrypt (``bool``, optional):
Returns ``DecryptedRemoteBoxFile`` if ``True`` (default),
``EncryptedRemoteBoxFile`` otherwise.
cache_preview (``bool``, optional):
Cache preview in yielded by generator
RemoteBoxFiles or not. ``True`` by default.
"""
if hasattr(self, '_mainkey'):
key = self._mainkey
if hasattr(self, '_dlb'):
dlb = self._dlb
if decrypt and not any((key, dlb)):
raise ValueError(
'You need to specify key or dlb to be able to decrypt.'
)
key = key if (key or not dlb) else dlb._mainkey
it_messages = self._ta.TelegramClient.iter_messages(
self._box_channel, limit=limit, offset_id=offset_id,
max_id=max_id, min_id=min_id, add_offset=add_offset,
search=search, from_user=from_user, wait_time=wait_time,
ids=ids, reverse=reverse
)
async for m in it_messages:
if not m and ignore_errors:
continue
elif not m and not ignore_errors:
raise RemoteFileNotFound('One of requsted by you file doesn\'t exist')
if m.document:
if not decrypt:
rbf = await EncryptedRemoteBoxFile(
m, self._ta, cache_preview=cache_preview).init()
else:
try:
rbf = await EncryptedRemoteBoxFile(
m, self._ta, cache_preview=cache_preview).decrypt(key)
except Exception as e: # In case of imported file
if return_imported_as_erbf and not dlb:
rbf = await EncryptedRemoteBoxFile(
m, self._ta, cache_preview=cache_preview).init()
elif ignore_errors and not dlb:
continue
elif not ignore_errors and not dlb:
raise IncorrectKey(
'File is imported. Specify dlb?') from None
elif dlb:
# We try to fetch FileKey of imported file from DLB.
dlb_file = await dlb.get_file(m.id, cache_preview=False)
# If we haven't imported this file to DLB
if not dlb_file:
if return_imported_as_erbf:
rbf = await EncryptedRemoteBoxFile(
m, self._ta, cache_preview=cache_preview).init()
elif ignore_errors:
continue
else:
raise NotImported(
"""You don\'t have FileKey for this file. """
"""Set to True ``return_imported_as_erbf``?"""
) from None
else:
# We already imported file, so have FileKey
rbf = await EncryptedRemoteBoxFile(
m, self._ta, cache_preview=cache_preview
).decrypt(dlb_file._filekey)
else:
raise e # Unknown Exception
yield rbf
async def search_file(
self,
sf: SearchFilter,
mainkey: Optional[MainKey] = None,
dlb: Optional['DecryptedLocalBox'] = None) ->\
AsyncGenerator[Union['EncryptedRemoteBoxFile', 'DecryptedRemoteBoxFile'], None]:
"""
This method used to search for files in your ``RemoteBox``.
Arguments:
sf (``SearchFilter``):
``SearchFilter`` with kwargs you like.
mainkey (``MainKey``, optional):
``MainKey`` for this ``RemoteBox``.
dlb (``DecryptedLocalBox``, optional):
LocalBox associated with this ``RemoteBox``. We
will take ``MainKey`` from it.
.. note::
- If ``dlb`` and ``mainkey`` not specified, then method\
will search on ``EncryptedRemoteBoxFile``.
- You may ignore this kwargs if you call this\
method on ``DecryptedRemoteBox`` class.
"""
if hasattr(self, '_mainkey'):
mainkey = self._mainkey
if hasattr(self, '_dlb'):
dlb = self._dlb
it_messages = self._ta.TelegramClient.iter_messages(
self._box_channel, ids=sf.id if sf.id else None
)
sfunc = _search_func(
sf, mainkey=mainkey,
it_messages=it_messages,
lb=dlb, ta=self._ta
)
async for file in sfunc:
yield file
async def push_file(
self, ff: 'FutureFile',
progress_callback: Optional[Callable[[int, int], None]] = None,
) -> 'DecryptedRemoteBoxFile':
"""
Uploads ``FutureFile`` to the ``RemoteBox``.
Arguments:
ff (``FutureFile``):
File to upload. You should recieve
it via ``DecryptedLocalBox.make_file``.
progress_callback (``Callable[[int, int], None]``, optional):
A callback function accepting two parameters:
(downloaded_bytes, total).
"""
state = AES(ff.filekey, ff.file_iv)
oe = OpenPretender(ff.file, state, ff.size)
oe.concat_metadata(ff.metadata)
ifile = await upload_file(
self._ta.TelegramClient, oe,
file_name = urlsafe_b64encode(ff.file_salt).decode(),
part_size_kb = 512, file_size = ff.wm_size,
progress_callback = progress_callback
)
file_message = await self._ta.TelegramClient.send_file(
self._box_channel,
file=ifile, silent=True,