Skip to content

Commit 1bf74a9

Browse files
committed
Merge branch 'master' into tgcrypto
2 parents e14a364 + 1db1339 commit 1bf74a9

8 files changed

Lines changed: 7892 additions & 73 deletions

File tree

pyrogram/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
"e" if sys.getfilesystemencoding() == "ascii" else "\xe8"
2424
)
2525
__license__ = "GNU Lesser General Public License v3 or later (LGPLv3+)"
26-
__version__ = "0.5.0"
26+
__version__ = "0.6.0"
2727

2828
from .api.errors import Error
2929
from .client import ChatAction
3030
from .client import Client
3131
from .client import ParseMode
3232
from .client.input_media import InputMedia
33+
from .client import Emoji

pyrogram/client/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@
1919
from .chat_action import ChatAction
2020
from .client import Client
2121
from .parse_mode import ParseMode
22+
from .emoji import Emoji

pyrogram/client/client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ def resolve_username(self, username: str):
706706
def resolve_peer(self, peer_id: int or str):
707707
"""Use this method to get the *InputPeer* of a known *peer_id*.
708708
709-
It is intended to be used when working with Raw Functions (i.e: a Telegram API method you wish to use which is
709+
It is intended to be used when working with Raw Functions (i.e: a Telegram API method you wish to use which is
710710
not available yet in the Client class as an easy-to-use method).
711711
712712
Args:
@@ -2178,6 +2178,7 @@ def send_media_group(self,
21782178
mime_type=mimetypes.types_map[".mp4"],
21792179
attributes=[
21802180
types.DocumentAttributeVideo(
2181+
supports_streaming=i.supports_streaming,
21812182
duration=i.duration,
21822183
w=i.width,
21832184
h=i.height

pyrogram/client/emoji.py

Lines changed: 7810 additions & 0 deletions
Large diffs are not rendered by default.

pyrogram/client/input_media.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@
2020
class InputMedia:
2121
class Photo:
2222
"""This object represents a photo to be sent inside an album.
23+
It is intended to be used with :obj:`pyrogram.Client.send_media_group`.
2324
2425
Args:
2526
media (:obj:`str`):
26-
File to send.
27+
Photo file to send.
2728
Pass a file path as string to send a photo that exists on your local machine.
2829
2930
caption (:obj:`str`):
@@ -45,19 +46,32 @@ def __init__(self,
4546

4647
class Video:
4748
"""This object represents a video to be sent inside an album.
49+
It is intended to be used with :obj:`pyrogram.Client.send_media_group`.
4850
4951
Args:
5052
media (:obj:`str`):
51-
File to send.
53+
Video file to send.
5254
Pass a file path as string to send a video that exists on your local machine.
5355
54-
caption (:obj:`str`):
56+
caption (:obj:`str`, optional):
5557
Caption of the video to be sent, 0-200 characters
5658
57-
parse_mode (:obj:`str`):
59+
parse_mode (:obj:`str`, optional):
5860
Use :obj:`pyrogram.ParseMode.MARKDOWN` or :obj:`pyrogram.ParseMode.HTML` if you want Telegram apps
5961
to show bold, italic, fixed-width text or inline URLs in your caption.
6062
Defaults to Markdown.
63+
64+
width (:obj:`int`, optional):
65+
Video width.
66+
67+
height (:obj:`int`, optional):
68+
Video height
69+
70+
duration (:obj:`int`, optional):
71+
Video duration.
72+
73+
supports_streaming (:obj:`bool`, optional):
74+
Pass True, if the uploaded video is suitable for streaming.
6175
"""
6276

6377
def __init__(self,
@@ -66,10 +80,12 @@ def __init__(self,
6680
parse_mode: str = "",
6781
width: int = 0,
6882
height: int = 0,
69-
duration: int = 0):
83+
duration: int = 0,
84+
supports_streaming: bool = None):
7085
self.media = media
7186
self.caption = caption
7287
self.parse_mode = parse_mode
7388
self.width = width
7489
self.height = height
7590
self.duration = duration
91+
self.supports_streaming = supports_streaming

pyrogram/client/style/markdown.py

Lines changed: 54 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -24,95 +24,84 @@
2424
MessageEntityCode as Code,
2525
MessageEntityTextUrl as Url,
2626
MessageEntityPre as Pre,
27+
MessageEntityMentionName as MentionInvalid,
2728
InputMessageEntityMentionName as Mention
2829
)
2930
from . import utils
3031

3132

3233
class Markdown:
33-
INLINE_DELIMITERS = {
34-
"**": Bold,
35-
"__": Italic,
36-
"`": Code
37-
}
34+
BOLD_DELIMITER = "**"
35+
ITALIC_DELIMITER = "__"
36+
CODE_DELIMITER = "`"
37+
PRE_DELIMITER = "```"
3838

39-
# ``` python
40-
# for i in range(10):
41-
# print(i)
42-
# ```
43-
PRE_RE = r"(?P<pre>```(?P<lang>.*)\n(?P<code>(.|\n)*)\n```)"
44-
45-
# [url](github.com)
46-
URL_RE = r"(?P<url>(\[(?P<url_text>.+?)\]\((?P<url_path>.+?)\)))"
47-
48-
# [name](tg://user?id=123456789)
49-
MENTION_RE = r"(?P<mention>(\[(?P<mention_text>.+?)\]\(tg:\/\/user\?id=(?P<user_id>\d+?)\)))"
50-
51-
# **bold**
52-
# __italic__
53-
# `code`
54-
INLINE_RE = r"(?P<inline>(?P<start_delimiter>{d})(?P<body>.+?)(?P<end_delimiter>{d}))".format(
39+
MARKDOWN_RE = re.compile(r"```([\w ]*)\n([\w\W]*)(?:\n|)```|\[([^[(]+)\]\(([^])]+)\)|({d})(.+?)\5".format(
5540
d="|".join(
5641
["".join(i) for i in [
5742
["\{}".format(j) for j in i]
58-
for i in sorted( # Sort delimiters by length
59-
INLINE_DELIMITERS.keys(),
60-
key=lambda k: len(k), # Or: key=len
61-
reverse=True
62-
)
43+
for i in [
44+
PRE_DELIMITER,
45+
CODE_DELIMITER,
46+
ITALIC_DELIMITER,
47+
BOLD_DELIMITER
48+
]
6349
]]
6450
)
65-
)
66-
67-
MARKDOWN_RE = re.compile("|".join([PRE_RE, MENTION_RE, URL_RE, INLINE_RE]))
51+
))
52+
MENTION_RE = re.compile(r"tg://user\?id=(\d+)")
6853

69-
def __init__(self, peers_by_id):
54+
def __init__(self, peers_by_id: dict):
7055
self.peers_by_id = peers_by_id
7156

72-
def parse(self, text):
57+
def parse(self, message: str):
7358
entities = []
74-
text = utils.add_surrogates(text)
59+
message = utils.add_surrogates(message).strip()
7560
offset = 0
7661

77-
for match in self.MARKDOWN_RE.finditer(text):
62+
for match in self.MARKDOWN_RE.finditer(message):
7863
start = match.start() - offset
79-
80-
if match.group("pre"):
81-
pattern = match.group("pre")
82-
lang = match.group("lang")
83-
replace = match.group("code")
84-
entity = Pre(start, len(replace), lang.strip())
85-
offset += len(lang) + 8
86-
elif match.group("url"):
87-
pattern = match.group("url")
88-
replace = match.group("url_text")
89-
path = match.group("url_path")
90-
entity = Url(start, len(replace), path)
91-
offset += len(path) + 4
92-
elif match.group("mention"):
93-
pattern = match.group("mention")
94-
replace = match.group("mention_text")
95-
user_id = match.group("user_id")
96-
entity = Mention(start, len(replace), self.peers_by_id[int(user_id)])
97-
offset += len(user_id) + 17
98-
elif match.group("inline"):
99-
pattern = match.group("inline")
100-
replace = match.group("body")
101-
start_delimiter = match.group("start_delimiter")
102-
end_delimiter = match.group("end_delimiter")
103-
104-
if start_delimiter != end_delimiter:
64+
lang, pre, text, url, style, body = match.groups()
65+
66+
if pre:
67+
body = pre = pre.strip()
68+
entity = Pre(start, len(pre), lang.strip() or "")
69+
offset += len(lang) + len(self.PRE_DELIMITER) * 2
70+
elif url:
71+
mention = self.MENTION_RE.match(url)
72+
73+
if mention:
74+
user_id = int(mention.group(1))
75+
input_user = self.peers_by_id.get(user_id, None)
76+
77+
entity = (
78+
Mention(start, len(text), input_user)
79+
if input_user
80+
else MentionInvalid(start, len(text), user_id)
81+
)
82+
else:
83+
entity = Url(start, len(text), url)
84+
85+
body = text
86+
offset += len(url) + 4
87+
else:
88+
if style == self.BOLD_DELIMITER:
89+
entity = Bold(start, len(body))
90+
elif style == self.ITALIC_DELIMITER:
91+
entity = Italic(start, len(body))
92+
elif style == self.CODE_DELIMITER:
93+
entity = Code(start, len(body))
94+
elif style == self.PRE_DELIMITER:
95+
entity = Pre(start, len(body), "")
96+
else:
10597
continue
10698

107-
entity = self.INLINE_DELIMITERS[start_delimiter](start, len(replace))
108-
offset += len(start_delimiter) * 2
109-
else:
110-
continue
99+
offset += len(style) * 2
111100

112101
entities.append(entity)
113-
text = text.replace(pattern, replace)
102+
message = message.replace(match.group(), body)
114103

115104
return dict(
116-
message=utils.remove_surrogates(text),
105+
message=utils.remove_surrogates(message),
117106
entities=entities
118107
)

pyrogram/connection/transport/tcp/tcp.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
class TCP(socks.socksocket):
3131
def __init__(self, proxy: Proxy):
3232
super().__init__()
33+
self.settimeout(10)
3334
self.proxy_enabled = False
3435

3536
if proxy and proxy.enabled:

pyrogram/session/session.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ def ping(self):
296296
break
297297

298298
try:
299-
self._send(functions.Ping(0), False)
299+
self._send(functions.PingDelayDisconnect(0, self.PING_INTERVAL + 15), False)
300300
except (OSError, TimeoutError):
301301
pass
302302

0 commit comments

Comments
 (0)