Selaa lähdekoodia

Removed fernet encryption & Added image to PDF converter

TheHamkerCat 5 vuotta sitten
vanhempi
sitoutus
378225f58d
6 muutettua tiedostoa jossa 109 lisäystä ja 88 poistoa
  1. 0 5
      app.json
  2. 1 1
      requirements.txt
  3. 0 2
      sample_config.env
  4. 0 2
      sample_config.py
  5. 104 0
      wbb/modules/img_pdf.py
  6. 4 78
      wbb/modules/misc.py

+ 0 - 5
app.json

@@ -42,11 +42,6 @@
             "description": "gban logs. ex: '-123456'",
             "required": true
         },
-        "FERNET_ENCRYPTION_KEY": {
-            "description": "Don't change this value",
-            "value": "iKMq0WZMnJKjMQxZWKtv-cplMuF_LoyshXj0XbTGGWM=",
-            "required": true
-        },
         "WELCOME_DELAY_KICK_SEC": {
             "value": "300",
             "required": true

+ 1 - 1
requirements.txt

@@ -2,7 +2,6 @@ aiohttp
 aiofiles
 ffmpeg-python
 gTTS
-cryptography
 dnspython
 future
 googletrans==4.0.0-rc1
@@ -23,3 +22,4 @@ python-dotenv
 feedparser
 pyromod
 fuzzysearch
+img2pdf

+ 0 - 2
sample_config.env

@@ -18,8 +18,6 @@ GBAN_LOG_GROUP_ID=-173738999
 
 MESSAGE_DUMP_CHAT=-173738999
 
-FERNET_ENCRYPTION_KEY=iKMq0WZMnJKjMQxZWKtv-cplMuF_LoyshXj0XbTGGWM= # Leave it
-
 WELCOME_DELAY_KICK_SEC=300 # Edit if u want
 
 MONGO_DB_URI=your_mongodb_uri

+ 0 - 2
sample_config.py

@@ -20,7 +20,6 @@ if HEROKU:
     LOG_GROUP_ID = int(environ.get("LOG_GROUP_ID", None))
     GBAN_LOG_GROUP_ID = int(environ.get("GBAN_LOG_GROUP_ID", None))
     MESSAGE_DUMP_CHAT = int(environ.get("MESSAGE_DUMP_CHAT", None))
-    FERNET_ENCRYPTION_KEY = environ.get("FERNET_ENCRYPTION_KEY", None)
     WELCOME_DELAY_KICK_SEC = int(
         environ.get("WELCOME_DELAY_KICK_SEC", None)
     )
@@ -43,7 +42,6 @@ else:
     LOG_GROUP_ID = -100125431255
     GBAN_LOG_GROUP_ID = -100125431255
     MESSAGE_DUMP_CHAT = -1001181696437
-    FERNET_ENCRYPTION_KEY = "iKMq0WZMnJKjMQxZWKtv-cplMuF_LoyshXj0XbTGGWM="  # Leave this as it is
     WELCOME_DELAY_KICK_SEC = 300
     MONGO_DB_URI = "mongodb+srv://username:password@cluster0.ksiis.mongodb.net/YourDataBaseName?retryWrites=true&w=majority"
     ARQ_API_KEY = "Get this from @ARQRobot"

+ 104 - 0
wbb/modules/img_pdf.py

@@ -0,0 +1,104 @@
+"""
+MIT License
+
+Copyright (c) 2021 TheHamkerCat
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including witout limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+"""
+from io import BytesIO
+from os import path, remove
+from time import time
+
+import img2pdf
+from PIL import Image
+from pyrogram import filters
+from pyrogram.types import Message
+
+from wbb import app
+from wbb.core.decorators.errors import capture_err
+from wbb.core.sections import section
+
+
+async def convert(
+    main_message: Message,
+    reply_messages,
+    status_message: Message,
+    start_time: float,
+):
+    m = status_message
+
+    documents = []
+
+    for message in reply_messages:
+        if not message.document:
+            return await m.edit("Not document, ABORTED!")
+
+        if message.document.mime_type.split("/")[0] != "image":
+            return await m.edit("Invalid mime type!")
+
+        if message.document.file_size > 5000000:
+            return await m.edit("Size too large, ABORTED!")
+        documents.append(await message.download())
+
+    for img_path in documents:
+        img = Image.open(img_path).convert("RGB")
+        img.save(img_path, "JPEG", quality=100)
+
+    pdf = BytesIO(img2pdf.convert(documents))
+    pdf.name = "wbb.pdf"
+
+    elapsed = round(time() - start_time, 2)
+
+    await main_message.reply_document(
+        document=pdf,
+        caption=section(
+            "IMG2PDF",
+            body={
+                "Title": pdf.name,
+                "Size": f"{pdf.__sizeof__() / (10**6)}MB",
+                "Pages": len(documents),
+                "Took": f"{elapsed}s",
+            },
+        ),
+    )
+
+    await m.delete()
+    pdf.close()
+    [remove(file) for file in documents if path.exists(file)]
+
+
+@app.on_message(filters.command("pdf"))
+async def img_to_pdf(_, message: Message):
+    reply = message.reply_to_message
+    if not reply:
+        return await message.reply(
+            "Reply to an image (as document) or group of images."
+        )
+
+    m = await message.reply_text("Converting..")
+    start_time = time()
+
+    if reply.media_group_id:
+        messages = await app.get_media_group(
+            message.chat.id,
+            reply.message_id,
+        )
+        return await convert(message, messages, m, start_time)
+
+    return await convert(message, [reply], m, start_time)

+ 4 - 78
wbb/modules/misc.py

@@ -26,10 +26,9 @@ import string
 from asyncio import Lock
 
 import aiohttp
-from cryptography.fernet import Fernet
 from pyrogram import filters
 
-from wbb import FERNET_ENCRYPTION_KEY, app, arq
+from wbb import app, arq
 from wbb.core.decorators.errors import capture_err
 from wbb.utils import random_line
 from wbb.utils.http import get
@@ -53,12 +52,6 @@ __HELP__ = """
 /random [Length]
     Generate Random Complex Passwords
 
-/encrypt
-    Encrypt Text [Can Only Be Decrypted By This Bot]
-
-/decrypt
-    Decrypt Text
-
 /cheat [Language] [Query]
     Get Programming Related Help
 
@@ -87,6 +80,9 @@ __HELP__ = """
 /autocorrect [Reply to a message]
     Autocorrects the text in replied message.
 
+/pdf [Reply to an image (as document) or a group of images.]
+    Convert images to PDF, helpful for online classes.
+
 #RTFM - Tell noobs to read the manual
 """
 
@@ -186,76 +182,6 @@ async def random(_, message):
         )
 
 
-# Encrypt
-@app.on_message(filters.command("encrypt") & ~filters.edited)
-@capture_err
-async def encrypt(_, message):
-    if not message.reply_to_message:
-        return await message.reply_text(
-            "Reply To A Message To Encrypt It."
-        )
-    text = message.reply_to_message.text
-    text_in_bytes = bytes(text, "utf-8")
-    cipher_suite = Fernet(FERNET_ENCRYPTION_KEY)
-    encrypted_text = cipher_suite.encrypt(text_in_bytes)
-    bytes_in_text = encrypted_text.decode("utf-8")
-    await message.reply_text(bytes_in_text)
-
-
-# Decrypt
-@app.on_message(filters.command("decrypt") & ~filters.edited)
-@capture_err
-async def decrypt(_, message):
-    if not message.reply_to_message:
-        return await message.reply_text(
-            "Reply To A Message To Decrypt It."
-        )
-    text = message.reply_to_message.text
-    text_in_bytes = bytes(text, "utf-8")
-    cipher_suite = Fernet(FERNET_ENCRYPTION_KEY)
-    try:
-        decoded_text = cipher_suite.decrypt(text_in_bytes)
-    except Exception:
-        return await message.reply_text("Incorrect token")
-    bytes_in_text = decoded_text.decode("utf-8")
-    await message.reply_text(bytes_in_text)
-
-
-async def fetch_text(url):
-    async with aiohttp.ClientSession(
-        headers={"user-agent": "curl"}
-    ) as session:
-        async with session.get(url) as resp:
-            data = await resp.text()
-    return data
-
-
-# Cheat.sh
-@app.on_message(filters.command("cheat") & ~filters.edited)
-@capture_err
-async def cheat(_, message):
-    if len(message.command) < 3:
-        return await message.reply_text("/cheat [language] [query]")
-    text = message.text.split(None, 2)
-    m = await message.reply_text("Searching")
-    try:
-        language = text[1]
-        query = text[2]
-        data = await fetch_text(
-            f"http://cht.sh/{language}/{query}?QT"
-        )
-        if not data:
-            return await m.edit("Found Literally Nothing!")
-        if len(data) > 4090:
-            with open("cheat.txt", "w") as f:
-                f.write(data)
-            return await message.reply_document(data)
-        await m.edit(f"`{data}`")
-    except Exception as e:
-        await m.edit(str(e))
-        print(str(e))
-
-
 # Translate
 @app.on_message(filters.command("tr") & ~filters.edited)
 @capture_err