File size: 5,155 Bytes
78b07ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import os
import uuid

from pyrogram import Client
from pyrogram.enums import MessageMediaType
from pyrogram.types import Message

from Hellbot.functions.images import convert_to_png
from Hellbot.functions.utility import TGraph

from . import Config, HelpMenu, Symbols, hellbot, on_message


@on_message(["tgm", "tm"], allow_stan=True)
async def telegraph_media(_, message: Message):
    if not message.reply_to_message or not message.reply_to_message.media:
        return await hellbot.edit(message, "__Reply to a media message!__")

    hell = await hellbot.edit(message, "__Uploading to telegraph...__")

    if message.reply_to_message.media in [
        MessageMediaType.ANIMATION,
        MessageMediaType.VIDEO,
    ]:
        file_size = (
            message.reply_to_message.animation.file_size
            if message.reply_to_message.animation
            else message.reply_to_message.video.file_size
        )

        if file_size >= 5242880:
            return await hellbot.delete(
                hell,
                "__This media is too big to upload to telegraph! You need to choose media below 5mb.__",
            )

        path = await message.reply_to_message.download(Config.TEMP_DIR)

    elif message.reply_to_message.media in [
        MessageMediaType.PHOTO,
        MessageMediaType.STICKER,
        MessageMediaType.DOCUMENT,
    ]:
        file_size = (
            message.reply_to_message.photo.file_size
            if message.reply_to_message.photo
            else message.reply_to_message.sticker.file_size
            if message.reply_to_message.sticker
            else message.reply_to_message.document.file_size
        )

        if file_size >= 5242880:
            return await hellbot.delete(
                hell,
                "__This media is too big to upload to telegraph! You need to choose media below 5mb.__",
            )

        if message.reply_to_message.document:
            if message.reply_to_message.document.mime_type.lower().split("/")[0] in [
                "image",
                "video",
            ]:
                path = await message.reply_to_message.download(Config.TEMP_DIR)
            else:
                return await hellbot.delete(hell, "This media is not supported!")
        else:
            path = await message.reply_to_message.download(Config.TEMP_DIR)
    else:
        return await hellbot.delete(hell, "This media is not supported!")

    if path.lower().endswith(".webp"):
        path = convert_to_png(path)

    await hell.edit(
        f"**Media downloaded to local server.** __Now uploading to telegraph...__"
    )

    try:
        media_url = TGraph.telegraph.upload_file(path)
        url = f"https://te.legra.ph{media_url[0]['src']}"
    except Exception as e:
        await hellbot.error(hell, str(e))
    else:
        await hell.edit(
            f"**💫 Uploaded to [telegraph]({url})!**\n\n**{Symbols.anchor} URL:** `{url}`",
            disable_web_page_preview=True,
        )

    os.remove(path)


@on_message(["tgt", "tt"], allow_stan=True)
async def telegraph_text(client: Client, message: Message):
    if len(message.command) < 2:
        page_name = client.me.first_name
    else:
        page_name = await hellbot.input(message)

    if not message.reply_to_message:
        return await hellbot.edit(
            message, "__Reply to a message to upload it on telegraph page!__"
        )

    hell = await hellbot.edit(message, "__Uploading to telegraph...__")

    page_content = (
        message.reply_to_message.text or message.reply_to_message.caption or ""
    )

    media_list = None
    if message.reply_to_message.media:
        page_media = await message.reply_to_message.download(Config.TEMP_DIR)

        with open(page_media, "rb") as f:
            media_list = f.readlines()

        for media in media_list:
            page_content += media.decode("utf-8") + "\n"

        os.remove(page_media)

    page_content = page_content.replace("\n", "<br>")

    try:
        response = TGraph.telegraph.create_page(page_name, html_content=page_content)
    except Exception:
        rnd_key = uuid.uuid4().hex[:8]
        page_name = f"{page_name}_{rnd_key}"
        response = TGraph.telegraph.create_page(page_name, html_content=page_content)

    try:
        url = f"https://te.legra.ph/{response['path']}"
        await hell.edit(
            f"**💫 Uploaded to [telegraph]({url})!**\n\n**{Symbols.anchor} URL:** `{url}`",
            disable_web_page_preview=True,
        )
    except Exception as e:
        await hellbot.error(hell, str(e))


HelpMenu("telegraph").add(
    "tgm",
    "<reply to media>",
    "Upload the replied media message to telegra.ph and returns a direct url.",
    "tgm",
    "An alias of 'tm' is also available.",
).add(
    "tgt",
    "<reply to message> <page title>",
    "Upload the replied message content to telegra.ph!",
    "tgt",
    "An alias of 'tt' is also available.",
).info(
    "Telegraph Uploader"
).done()