-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
698a7e1
commit 6640c9a
Showing
2 changed files
with
76 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
""" | ||
:project: telegram-onedrive | ||
:author: L-ING | ||
:copyright: (C) 2024 L-ING <[email protected]> | ||
:license: MIT, see LICENSE for more details. | ||
""" | ||
|
||
from modules.client import tg_client | ||
from modules.utils import delete_message | ||
|
||
KEYS = ["link", "url"] | ||
|
||
|
||
async def parse_t2o_to_dict(content): | ||
result = {} | ||
for key in KEYS: | ||
result[key] = [] | ||
current_key = "" | ||
|
||
for line in content.splitlines(): | ||
line.strip() | ||
|
||
if (key := line.lstrip("[").rstrip("]")) in KEYS: | ||
current_key = key | ||
elif line and current_key == "link": | ||
line = line.split() | ||
if len(line) == 1: | ||
result[current_key].append({"content": line[0], "range": 1}) | ||
elif len(line) == 2: | ||
result[current_key].append({"content": line[0], "range": int(line[1])}) | ||
elif line and current_key == "url": | ||
result[current_key].append(line) | ||
|
||
return result | ||
|
||
|
||
async def parse_t2o(event, message): | ||
content = (await message.download_media(file=bytes)).decode() | ||
t2o_dict = await parse_t2o_to_dict(content) | ||
print(t2o_dict) | ||
|
||
for key in KEYS: | ||
if key == "link": | ||
for link in t2o_dict[key]: | ||
if link["range"] == 1: | ||
await tg_client.send_message( | ||
event.chat_id, | ||
message=link["content"], | ||
) | ||
elif link["range"] > 1: | ||
await tg_client.send_message( | ||
event.chat_id, | ||
message="/links %s %d" % (link["content"], link["range"]), | ||
) | ||
elif key == "url": | ||
for url in t2o_dict[key]: | ||
await tg_client.send_message( | ||
event.chat_id, | ||
message=f"/url {url}", | ||
) | ||
|
||
await delete_message(event) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters