Skip to content

Commit

Permalink
[MODULE] open: refactor
Browse files Browse the repository at this point in the history
- reduce cyclomatic complexity in check_extension
- better parsing of syntax
  • Loading branch information
AbhiTheModder committed Nov 24, 2024
1 parent 743916b commit 1fc0237
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 61 deletions.
2 changes: 1 addition & 1 deletion modules/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ async def filters_handler(_, message: Message):
try:
text = ""
for index, a in enumerate(get_filters_chat(message.chat.id).items(), start=1):
key, item = a
key, _ = a
key = key.replace("<", "").replace(">", "")
text += f"{index}. <code>{key}</code>\n"
text = f"<b>Your filters in current chat</b>:\n\n" f"{text}"
Expand Down
96 changes: 36 additions & 60 deletions modules/open.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import time

import aiofiles
from pyrogram import Client, enums, filters
from pyrogram import Client, filters
from pyrogram.errors import MessageTooLong
from pyrogram.types import Message

Expand All @@ -35,58 +35,37 @@ async def read_file(file_path):


def check_extension(file_path):
if file_path.lower().endswith(".txt"):
code_start = "```txt"
elif file_path.lower().endswith(".py"):
code_start = "```py"
elif file_path.lower().endswith(".js"):
code_start = "```js"
elif file_path.lower().endswith(".json"):
code_start = "```json"
elif file_path.lower().endswith(".smali"):
code_start = "```smali"
elif file_path.lower().endswith(".sh"):
code_start = "```bash"
elif file_path.lower().endswith(".c"):
code_start = "```C"
elif file_path.lower().endswith(".java"):
code_start = "```java"
elif file_path.lower().endswith(".php"):
code_start = "```php"
elif file_path.lower().endswith(".doc"):
code_start = "```doc"
elif file_path.lower().endswith(".docx"):
code_start = "```docx"
elif file_path.lower().endswith(".rtf"):
code_start = "```rtf"
elif file_path.lower().endswith(".s"):
code_start = "```asm"
elif file_path.lower().endswith(".dart"):
code_start = "```dart"
elif file_path.lower().endswith(".cfg"):
code_start = "```cfg"
elif file_path.lower().endswith(".swift"):
code_start = "```swift"
elif file_path.lower().endswith(".cs"):
code_start = "```C#"
elif file_path.lower().endswith(".vb"):
code_start = "```vb"
elif file_path.lower().endswith(".css"):
code_start = "```css"
elif file_path.lower().endswith(".htm") or file_path.lower().endswith(".html"):
code_start = "```html"
elif file_path.lower().endswith(".rss"):
code_start = "```rss"
elif file_path.lower().endswith(".swift"):
code_start = "```swift"
elif file_path.lower().endswith(".xhtml"):
code_start = "```xhtml"
elif file_path.lower().endswith(".cpp"):
code_start = "```cpp"
else:
code_start = "```"

return code_start
extensions = {
".txt": "<pre lang='plaintext'>",
".py": "<pre lang='python'>",
".js": "<pre lang='javascript'>",
".json": "<pre lang='json'>",
".smali": "<pre lang='smali'>",
".sh": "<pre lang='shell'>",
".c": "<pre lang='c'>",
".java": "<pre lang='java'>",
".php": "<pre lang='php'>",
".doc": "<pre lang='doc'>",
".docx": "<pre lang='docx'>",
".rtf": "<pre lang='rtf'>",
".s": "<pre lang='asm'>",
".dart": "<pre lang='dart'>",
".cfg": "<pre lang='cfg'>",
".swift": "<pre lang='swift'>",
".cs": "<pre lang='csharp'>",
".vb": "<pre lang='vb'>",
".css": "<pre lang='css'>",
".htm": "<pre lang='html'>",
".html": "<pre lang='html'>",
".rss": "<pre lang='xml'>",
".xhtml": "<pre lang='xtml'>",
".cpp": "<pre lang='cpp'>",
}

ext = os.path.splitext(file_path)[1].lower()

return extensions.get(ext, "<pre>")



@Client.on_message(filters.command("open", prefix) & filters.me)
Expand All @@ -95,7 +74,7 @@ async def openfile(client: Client, message: Message):
return await message.edit_text("Kindly Reply to a File")

try:
ms = await edit_or_reply(message, "`Downloading...")
ms = await edit_or_reply(message, "<b>Downloading...</b>")
ct = time.time()
file_path = await message.reply_to_message.download(
progress=progress, progress_args=(ms, ct, "Downloading...")
Expand All @@ -108,18 +87,16 @@ async def openfile(client: Client, message: Message):
"%Y-%m-%d %H:%M:%S"
)
code_start = check_extension(file_path=file_path)
code_end = "```"
content = await read_file(file_path=file_path)
await ms.edit_text(
f"**File Name:** `{file_name[0]}`\n**Size:** `{file_size} bytes`\n**Last Modified:** `{last_modified}`\n**Content:** {code_start}\n{content}{code_end}",
parse_mode=enums.ParseMode.MARKDOWN,
f"<b>File Name:</b> <code>{file_name[0]}</code>\n<b>Size:</b> <code>{file_size} bytes</code>\n<b>Last Modified:</b> <code>{last_modified}</code>\n<b>Content:</b> {code_start}{content}</pre>",
)

except MessageTooLong:
await ms.edit_text(
"<code>File Content is too long... Pasting to rentry...</code>"
)
content_new = f"{code_start}\n{content}{code_end}"
content_new = f"```{code_start[11:-2]}\n{content}```"
paste = subprocess.run(
["rentry", "new", content_new], capture_output=True, text=True, check=True
)
Expand All @@ -130,8 +107,7 @@ async def openfile(client: Client, message: Message):
if parts[0].strip() == "Url:":
url = "".join(parts[1:]).split()[0]
await ms.edit_text(
f"**File Name:** `{file_name[0]}`\n**Size:** `{file_size} bytes`\n**Last Modified:** `{last_modified}`\n**Content:** {url}\n**Note:** `Edit Code has been sent to your saved messages`",
parse_mode=enums.ParseMode.MARKDOWN,
f"<b>File Name:</b> <code>{file_name[0]}</code>\n<b>Size:</b> <code>{file_size} bytes</code>\n<b>Last Modified:</b> <code>{last_modified}</code>\n<b>Content:</b> {url}\n<b>Note:</b> <code>Edit Code has been sent to your saved messages</code>",
disable_web_page_preview=True,
)
break
Expand Down

0 comments on commit 1fc0237

Please sign in to comment.