Skip to content

Commit bf27bf1

Browse files
committed
Add core types
1 parent c9f90fa commit bf27bf1

File tree

7 files changed

+315
-0
lines changed

7 files changed

+315
-0
lines changed

pyrogram/api/core/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,11 @@
1515
#
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from .future_salt import FutureSalt
20+
from .future_salts import FutureSalts
21+
from .gzip_packed import GzipPacked
22+
from .message import Message
23+
from .msg_container import MsgContainer
24+
from .object import Object
25+
from .primitives import *

pyrogram/api/core/future_salt.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017 Dan Tès <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from datetime import datetime
20+
from io import BytesIO
21+
22+
from .object import Object
23+
from .primitives import Int, Long
24+
25+
26+
class FutureSalt(Object):
27+
ID = 0x0949d9dc
28+
29+
def __init__(self, valid_since: int or datetime, valid_until: int or datetime, salt: int):
30+
self.valid_since = valid_since
31+
self.valid_until = valid_until
32+
self.salt = salt
33+
34+
@staticmethod
35+
def read(b: BytesIO) -> "FutureSalt":
36+
valid_since = datetime.fromtimestamp(Int.read(b))
37+
valid_until = datetime.fromtimestamp(Int.read(b))
38+
salt = Long.read(b)
39+
40+
return FutureSalt(valid_since, valid_until, salt)

pyrogram/api/core/future_salts.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017 Dan Tès <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from datetime import datetime
20+
from io import BytesIO
21+
22+
from . import FutureSalt
23+
from .object import Object
24+
from .primitives import Int, Long
25+
26+
27+
class FutureSalts(Object):
28+
ID = 0xae500895
29+
30+
def __init__(self, req_msg_id: int, now: int or datetime, salts: list):
31+
self.req_msg_id = req_msg_id
32+
self.now = now
33+
self.salts = salts
34+
35+
@staticmethod
36+
def read(b: BytesIO) -> "FutureSalts":
37+
req_msg_id = Long.read(b)
38+
now = datetime.fromtimestamp(Int.read(b))
39+
40+
count = Int.read(b)
41+
salts = [FutureSalt.read(b) for _ in range(count)]
42+
43+
return FutureSalts(req_msg_id, now, salts)

pyrogram/api/core/gzip_packed.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017 Dan Tès <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from gzip import compress, decompress
20+
from io import BytesIO
21+
22+
from .object import Object
23+
from .primitives import Int, Bytes
24+
25+
26+
class GzipPacked(Object):
27+
ID = 0x3072cfa1
28+
29+
def __init__(self, packed_data: Object):
30+
self.packed_data = packed_data
31+
32+
@staticmethod
33+
def read(b: BytesIO) -> "GzipPacked":
34+
# Return the Object itself instead of a GzipPacked wrapping it
35+
return Object.read(
36+
BytesIO(
37+
decompress(
38+
Bytes.read(b)
39+
)
40+
)
41+
)
42+
43+
def write(self) -> bytes:
44+
b = BytesIO()
45+
46+
b.write(Int(self.ID, False))
47+
48+
b.write(
49+
Bytes(
50+
compress(
51+
self.packed_data.write()
52+
)
53+
)
54+
)
55+
56+
return b.getvalue()

pyrogram/api/core/message.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017 Dan Tès <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from io import BytesIO
20+
21+
from .object import Object
22+
from .primitives import Int, Long
23+
24+
25+
class Message(Object):
26+
ID = 0x5bb8e511 # hex(crc32(b"message msg_id:long seqno:int bytes:int body:Object = Message"))
27+
28+
def __init__(self, body: Object, msg_id: int, seq_no: int, length: int):
29+
self.msg_id = msg_id
30+
self.seq_no = seq_no
31+
self.length = length
32+
self.body = body
33+
34+
@staticmethod
35+
def read(b: BytesIO) -> "Message":
36+
msg_id = Long.read(b)
37+
seq_no = Int.read(b)
38+
length = Int.read(b)
39+
body = b.read(length)
40+
41+
return Message(Object.read(BytesIO(body)), msg_id, seq_no, length)
42+
43+
def write(self) -> bytes:
44+
b = BytesIO()
45+
46+
b.write(Long(self.msg_id))
47+
b.write(Int(self.seq_no))
48+
b.write(Int(self.length))
49+
b.write(self.body.write())
50+
51+
return b.getvalue()

pyrogram/api/core/msg_container.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017 Dan Tès <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from io import BytesIO
20+
21+
from .message import Message
22+
from .object import Object
23+
from .primitives import Int
24+
25+
26+
class MsgContainer(Object):
27+
ID = 0x73f1f8dc
28+
29+
def __init__(self, messages: list):
30+
self.messages = messages
31+
32+
@staticmethod
33+
def read(b: BytesIO) -> "MsgContainer":
34+
count = Int.read(b)
35+
return MsgContainer([Message.read(b) for _ in range(count)])
36+
37+
def write(self) -> bytes:
38+
b = BytesIO()
39+
40+
b.write(Int(self.ID, False))
41+
42+
count = len(self.messages)
43+
b.write(Int(count))
44+
45+
for message in self.messages:
46+
b.write(message.write())
47+
48+
return b.getvalue()

pyrogram/api/core/object.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017 Dan Tès <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from collections import OrderedDict
20+
from datetime import datetime
21+
from importlib import import_module
22+
from io import BytesIO
23+
from json import JSONEncoder, dumps
24+
25+
from ..all import objects
26+
27+
28+
class Object:
29+
@staticmethod
30+
def read(b: BytesIO, *args):
31+
id = int.from_bytes(b.read(4), "little")
32+
name = objects.get(id)
33+
path, name = name.rsplit(".", 1)
34+
35+
return getattr(
36+
import_module("pyrogram.api." + path),
37+
name
38+
).read(b, *args)
39+
40+
def write(self, *args) -> bytes:
41+
pass
42+
43+
def __str__(self) -> str:
44+
return dumps(self, cls=Encoder, indent=4)
45+
46+
def __eq__(self, other) -> bool:
47+
return self.__dict__ == other.__dict__
48+
49+
def __len__(self) -> int:
50+
return len(self.write())
51+
52+
def __call__(self):
53+
pass
54+
55+
56+
class Encoder(JSONEncoder):
57+
def default(self, o: Object):
58+
try:
59+
content = o.__dict__
60+
except AttributeError:
61+
if isinstance(o, datetime):
62+
return o.strftime("%d-%b-%Y %H:%M:%S")
63+
else:
64+
return repr(o)
65+
66+
return OrderedDict(
67+
[("_", objects.get(getattr(o, "ID", None), None))]
68+
+ [i for i in content.items()]
69+
)

0 commit comments

Comments
 (0)