azils3 commited on
Commit
e8d5828
·
verified ·
1 Parent(s): d63c4c5

Rename 1.py to app.py

Browse files
Files changed (2) hide show
  1. 1.py +0 -60
  2. app.py +480 -0
1.py DELETED
@@ -1,60 +0,0 @@
1
- import os
2
-
3
- def collect_files(directory, extensions):
4
- collected_files = []
5
- for root, dirs, files in os.walk(directory):
6
- for file in files:
7
- if any(file.endswith(ext) for ext in extensions):
8
- file_path = os.path.join(root, file)
9
- collected_files.append((file_path, file))
10
- return collected_files
11
-
12
- def write_combined_file(text_files, media_files, output_file):
13
- with open(output_file, 'w', encoding='utf-8') as aio_file:
14
- # Write text file contents
15
- if text_files:
16
- aio_file.write("# Text Files Contents\n")
17
- for file_path, file_name in text_files:
18
- aio_file.write(f"# File: {file_path}\n")
19
- try:
20
- with open(file_path, 'r', encoding='utf-8') as file:
21
- aio_file.write(file.read())
22
- except Exception as e:
23
- aio_file.write(f"Error reading file {file_path}: {str(e)}\n")
24
- aio_file.write("\n" + "="*80 + "\n\n")
25
-
26
- # Write media file paths with folder structure
27
- if media_files:
28
- aio_file.write("# Media File Paths\n")
29
- current_folder = None
30
- for file_path, file_name in sorted(media_files, key=lambda x: x[0]):
31
- folder = os.path.dirname(file_path)
32
- if folder != current_folder:
33
- if current_folder is not None:
34
- aio_file.write("\n" + "="*80 + "\n\n")
35
- aio_file.write(f"# Folder: {folder}\n")
36
- current_folder = folder
37
- aio_file.write(f"File: {file_path}\n")
38
- if current_folder is not None:
39
- aio_file.write("\n" + "="*80 + "\n\n")
40
-
41
- def main():
42
- directory = os.getcwd() # Current working directory
43
-
44
- # Define extensions for text files and media files
45
- text_extensions = ['.py', '.js', '.html', '.css', '.txt', '.md', '.json']
46
- media_extensions = ['.jpg', '.jpeg', '.png', '.webp', '.avi', '.mp3', '.mp4']
47
-
48
- # Collect text files
49
- text_files = collect_files(directory, text_extensions)
50
-
51
- # Collect media files
52
- media_files = collect_files(directory, media_extensions)
53
-
54
- # Combine both text file contents and media file paths into one file
55
- output_file = 'combined.txt'
56
- write_combined_file(text_files, media_files, output_file)
57
- print(f"All text file contents and media file paths have been written to {output_file}")
58
-
59
- if __name__ == "__main__":
60
- main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app.py ADDED
@@ -0,0 +1,480 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # File: C:\Users\Shakeel\Desktop\PROFESSOR-BOT\app.py
2
+ from flask import Flask, render_template, jsonify, request
3
+ from pyrogram import Client
4
+ import os
5
+ import psutil
6
+ import time
7
+ import shutil
8
+ import asyncio
9
+ from utils import get_settings, save_group_settings, get_size, humanbytes, get_time, admin_check
10
+ from database.users_chats_db import db
11
+ from database.ia_filterdb import Media, get_search_results, get_file_details, save_file
12
+ from database.filters_mdb import add_filter, get_filters, delete_filter, del_all
13
+ from database.gfilters_mdb import add_gfilter, get_gfilters, delete_gfilter, del_allg
14
+ from database.connections_mdb import add_connection, all_connections, if_active, delete_connection, make_active, make_inactive
15
+ from info import ADMINS, LOG_CHANNEL, CHANNELS, DATABASE_URL, DATABASE_NAME, CACHE_TIME, API_ID, API_HASH, BOT_TOKEN, UPTIME, WEB_SUPPORT, LOG_MSG
16
+ from pymongo import MongoClient
17
+
18
+ app = Flask(__name__)
19
+
20
+ # Initialize Pyrogram Client
21
+ bot = Client(
22
+ name="Professor-Bot",
23
+ api_id=API_ID,
24
+ api_hash=API_HASH,
25
+ bot_token=BOT_TOKEN,
26
+ plugins=dict(root="plugins")
27
+ )
28
+
29
+ # Initialize MongoDB Client
30
+ mongo_client = MongoClient(DATABASE_URL)
31
+ mongo_db = mongo_client[DATABASE_NAME]
32
+
33
+ @app.route('/')
34
+ def dashboard():
35
+ return render_template('dashboard.html')
36
+
37
+ @app.route('/api/system-info')
38
+ def system_info():
39
+ uptime = time.time() - UPTIME
40
+ uptime_str = get_time(uptime)
41
+ storage = shutil.disk_usage("/")
42
+ return jsonify({
43
+ "os": {
44
+ "name": os.name,
45
+ "version": os.uname().version
46
+ },
47
+ "uptime": uptime_str,
48
+ "storage": {
49
+ "total": storage.total,
50
+ "free": storage.free
51
+ }
52
+ })
53
+
54
+ @app.route('/api/bot-stats')
55
+ async def bot_stats():
56
+ total_files = await Media.count_documents()
57
+ total_users = await db.total_users_count()
58
+ total_chats = await db.total_chat_count()
59
+ monsize = await db.get_db_size()
60
+ free = 536870912 - monsize
61
+ monsize = get_size(monsize)
62
+ free = get_size(free)
63
+ return jsonify({
64
+ "total_files": total_files,
65
+ "total_users": total_users,
66
+ "total_chats": total_chats,
67
+ "storage_used": monsize,
68
+ "storage_free": free
69
+ })
70
+
71
+ @app.route('/api/filters')
72
+ async def get_filters_api():
73
+ chat_id = request.args.get('chat_id')
74
+ if not chat_id:
75
+ return jsonify({"error": "Chat ID is required"}), 400
76
+ filters = await get_filters(chat_id)
77
+ return jsonify({"filters": filters})
78
+
79
+ @app.route('/api/add-filter', methods=['POST'])
80
+ async def add_filter_api():
81
+ chat_id = request.form.get('chat_id')
82
+ text = request.form.get('text')
83
+ reply_text = request.form.get('reply_text')
84
+ btn = request.form.get('btn')
85
+ file = request.form.get('file')
86
+ alert = request.form.get('alert')
87
+ if not chat_id or not text or not reply_text:
88
+ return jsonify({"error": "Chat ID, text, and reply text are required"}), 400
89
+ await add_filter(chat_id, text, reply_text, btn, file, alert)
90
+ return jsonify({"message": "Filter added successfully"})
91
+
92
+ @app.route('/api/delete-filter', methods=['POST'])
93
+ async def delete_filter_api():
94
+ chat_id = request.form.get('chat_id')
95
+ text = request.form.get('text')
96
+ if not chat_id or not text:
97
+ return jsonify({"error": "Chat ID and text are required"}), 400
98
+ await delete_filter(request, text, chat_id)
99
+ return jsonify({"message": "Filter deleted successfully"})
100
+
101
+ @app.route('/api/gfilters')
102
+ async def get_gfilters_api():
103
+ gfilters = 'gfilters'
104
+ filters = await get_gfilters(gfilters)
105
+ return jsonify({"filters": filters})
106
+
107
+ @app.route('/api/add-gfilter', methods=['POST'])
108
+ async def add_gfilter_api():
109
+ gfilters = 'gfilters'
110
+ text = request.form.get('text')
111
+ reply_text = request.form.get('reply_text')
112
+ btn = request.form.get('btn')
113
+ file = request.form.get('file')
114
+ alert = request.form.get('alert')
115
+ if not text or not reply_text:
116
+ return jsonify({"error": "Text and reply text are required"}), 400
117
+ await add_gfilter(gfilters, text, reply_text, btn, file, alert)
118
+ return jsonify({"message": "Global filter added successfully"})
119
+
120
+ @app.route('/api/delete-gfilter', methods=['POST'])
121
+ async def delete_gfilter_api():
122
+ gfilters = 'gfilters'
123
+ text = request.form.get('text')
124
+ if not text:
125
+ return jsonify({"error": "Text is required"}), 400
126
+ await delete_gfilter(request, text, gfilters)
127
+ return jsonify({"message": "Global filter deleted successfully"})
128
+
129
+ @app.route('/api/users')
130
+ async def get_users_api():
131
+ users = await db.get_all_users()
132
+ user_list = []
133
+ async for user in users:
134
+ user_list.append({
135
+ "id": user['id'],
136
+ "name": user['name'],
137
+ "ban_status": user['ban_status']
138
+ })
139
+ return jsonify({"users": user_list})
140
+
141
+ @app.route('/api/chats')
142
+ async def get_chats_api():
143
+ chats = await db.get_all_chats()
144
+ chat_list = []
145
+ async for chat in chats:
146
+ chat_list.append({
147
+ "id": chat['id'],
148
+ "title": chat['title'],
149
+ "username": chat['username'],
150
+ "chat_status": chat['chat_status']
151
+ })
152
+ return jsonify({"chats": chat_list})
153
+
154
+ @app.route('/api/files')
155
+ async def get_files_api():
156
+ files = await Media.find().to_list(None)
157
+ file_list = []
158
+ for file in files:
159
+ file_list.append({
160
+ "file_id": file['file_id'],
161
+ "file_name": file['file_name'],
162
+ "file_size": file['file_size'],
163
+ "file_type": file['file_type'],
164
+ "mime_type": file['mime_type'],
165
+ "caption": file['caption']
166
+ })
167
+ return jsonify({"files": file_list})
168
+
169
+ @app.route('/api/add-file', methods=['POST'])
170
+ async def add_file_api():
171
+ file_id = request.form.get('file_id')
172
+ file_ref = request.form.get('file_ref')
173
+ file_name = request.form.get('file_name')
174
+ file_size = request.form.get('file_size')
175
+ file_type = request.form.get('file_type')
176
+ mime_type = request.form.get('mime_type')
177
+ caption = request.form.get('caption')
178
+ if not file_id or not file_name or not file_size:
179
+ return jsonify({"error": "File ID, file name, and file size are required"}), 400
180
+ file = Media(
181
+ file_id=file_id,
182
+ file_ref=file_ref,
183
+ file_name=file_name,
184
+ file_size=int(file_size),
185
+ file_type=file_type,
186
+ mime_type=mime_type,
187
+ caption=caption
188
+ )
189
+ try:
190
+ await file.commit()
191
+ except Exception as e:
192
+ return jsonify({"error": str(e)}), 500
193
+ return jsonify({"message": "File added successfully"})
194
+
195
+ @app.route('/api/delete-file', methods=['POST'])
196
+ async def delete_file_api():
197
+ file_id = request.form.get('file_id')
198
+ if not file_id:
199
+ return jsonify({"error": "File ID is required"}), 400
200
+ result = await Media.collection.delete_one({'_id': file_id})
201
+ if result.deleted_count:
202
+ return jsonify({"message": "File deleted successfully"})
203
+ else:
204
+ return jsonify({"error": "File not found"}), 404
205
+
206
+ @app.route('/api/settings/<chat_id>')
207
+ async def get_settings_api(chat_id):
208
+ settings = await get_settings(chat_id)
209
+ return jsonify({"settings": settings})
210
+
211
+ @app.route('/api/save-settings', methods=['POST'])
212
+ async def save_settings_api():
213
+ chat_id = request.form.get('chat_id')
214
+ setting_key = request.form.get('setting_key')
215
+ setting_value = request.form.get('setting_value')
216
+ if not chat_id or not setting_key or not setting_value:
217
+ return jsonify({"error": "Chat ID, setting key, and setting value are required"}), 400
218
+ await save_group_settings(chat_id, setting_key, setting_value)
219
+ return jsonify({"message": "Settings saved successfully"})
220
+
221
+ @app.route('/api/ban-user', methods=['POST'])
222
+ async def ban_user_api():
223
+ user_id = request.form.get('user_id')
224
+ ban_reason = request.form.get('ban_reason', "No Reason")
225
+ if not user_id:
226
+ return jsonify({"error": "User ID is required"}), 400
227
+ await db.ban_user(int(user_id), ban_reason)
228
+ temp.BANNED_USERS.append(int(user_id))
229
+ return jsonify({"message": "User banned successfully"})
230
+
231
+ @app.route('/api/unban-user', methods=['POST'])
232
+ async def unban_user_api():
233
+ user_id = request.form.get('user_id')
234
+ if not user_id:
235
+ return jsonify({"error": "User ID is required"}), 400
236
+ await db.remove_ban(int(user_id))
237
+ temp.BANNED_USERS.remove(int(user_id))
238
+ return jsonify({"message": "User unbanned successfully"})
239
+
240
+ @app.route('/api/disable-chat', methods=['POST'])
241
+ async def disable_chat_api():
242
+ chat_id = request.form.get('chat_id')
243
+ reason = request.form.get('reason', "No Reason")
244
+ if not chat_id:
245
+ return jsonify({"error": "Chat ID is required"}), 400
246
+ await db.disable_chat(int(chat_id), reason)
247
+ temp.BANNED_CHATS.append(int(chat_id))
248
+ return jsonify({"message": "Chat disabled successfully"})
249
+
250
+ @app.route('/api/enable-chat', methods=['POST'])
251
+ async def enable_chat_api():
252
+ chat_id = request.form.get('chat_id')
253
+ if not chat_id:
254
+ return jsonify({"error": "Chat ID is required"}), 400
255
+ await db.re_enable_chat(int(chat_id))
256
+ temp.BANNED_CHATS.remove(int(chat_id))
257
+ return jsonify({"message": "Chat enabled successfully"})
258
+
259
+ @app.route('/api/upload-file', methods=['POST'])
260
+ async def upload_file_api():
261
+ if 'fileUpload' not in request.files:
262
+ return jsonify({"error": "No file part"}), 400
263
+ file = request.files['fileUpload']
264
+ if file.filename == '':
265
+ return jsonify({"error": "No selected file"}), 400
266
+ if file:
267
+ file_path = os.path.join("/app/uploads", file.filename)
268
+ file.save(file_path)
269
+ file_id, file_ref = await save_file(file_path)
270
+ os.remove(file_path)
271
+ return jsonify({"file_id": file_id, "file_ref": file_ref, "message": "File uploaded successfully"})
272
+ return jsonify({"error": "Failed to upload file"}), 500
273
+
274
+ @app.route('/api/download-file/<file_id>')
275
+ async def download_file_api(file_id):
276
+ file_details = await get_file_details(file_id)
277
+ if not file_details:
278
+ return jsonify({"error": "File not found"}), 404
279
+ file = file_details[0]
280
+ return jsonify({
281
+ "file_name": file['file_name'],
282
+ "file_size": file['file_size'],
283
+ "file_type": file['file_type'],
284
+ "mime_type": file['mime_type'],
285
+ "caption": file['caption']
286
+ })
287
+
288
+ @app.route('/api/search-files', methods=['GET'])
289
+ async def search_files_api():
290
+ query = request.args.get('query')
291
+ file_type = request.args.get('file_type')
292
+ if not query:
293
+ return jsonify({"error": "Query is required"}), 400
294
+ files, next_offset, total = await get_search_results(query, file_type=file_type)
295
+ file_list = []
296
+ for file in files:
297
+ file_list.append({
298
+ "file_id": file['file_id'],
299
+ "file_name": file['file_name'],
300
+ "file_size": file['file_size'],
301
+ "file_type": file['file_type'],
302
+ "mime_type": file['mime_type'],
303
+ "caption": file['caption']
304
+ })
305
+ return jsonify({"files": file_list, "next_offset": next_offset, "total": total})
306
+
307
+ @app.route('/api/broadcast', methods=['POST'])
308
+ async def broadcast_api():
309
+ message_text = request.form.get('message_text')
310
+ if not message_text:
311
+ return jsonify({"error": "Message text is required"}), 400
312
+ users = await db.get_all_users()
313
+ total_users = await db.total_users_count()
314
+ done = 0
315
+ blocked = 0
316
+ deleted = 0
317
+ failed = 0
318
+ success = 0
319
+ async for user in users:
320
+ try:
321
+ await bot.send_message(user['id'], message_text)
322
+ success += 1
323
+ except UserIsBlocked:
324
+ blocked += 1
325
+ except InputUserDeactivated:
326
+ deleted += 1
327
+ except Exception as e:
328
+ failed += 1
329
+ done += 1
330
+ return jsonify({
331
+ "total_users": total_users,
332
+ "completed": done,
333
+ "success": success,
334
+ "blocked": blocked,
335
+ "deleted": deleted,
336
+ "failed": failed
337
+ })
338
+
339
+ @app.route('/api/ban-users')
340
+ async def ban_users_api():
341
+ users = await db.get_all_users()
342
+ banned_users = []
343
+ async for user in users:
344
+ if user['ban_status']['is_banned']:
345
+ banned_users.append({
346
+ "id": user['id'],
347
+ "name": user['name'],
348
+ "ban_reason": user['ban_status']['ban_reason']
349
+ })
350
+ return jsonify({"banned_users": banned_users})
351
+
352
+ @app.route('/api/banned-chats')
353
+ async def banned_chats_api():
354
+ chats = await db.get_all_chats()
355
+ banned_chats = []
356
+ async for chat in chats:
357
+ if chat['chat_status']['is_disabled']:
358
+ banned_chats.append({
359
+ "id": chat['id'],
360
+ "title": chat['title'],
361
+ "username": chat['username'],
362
+ "reason": chat['chat_status']['reason']
363
+ })
364
+ return jsonify({"banned_chats": banned_chats})
365
+
366
+ @app.route('/api/user-info/<user_id>')
367
+ async def user_info_api(user_id):
368
+ try:
369
+ user = await bot.get_users(int(user_id))
370
+ return jsonify({
371
+ "first_name": user.first_name,
372
+ "last_name": user.last_name,
373
+ "username": user.username,
374
+ "id": user.id,
375
+ "dc_id": user.dc_id
376
+ })
377
+ except Exception as e:
378
+ return jsonify({"error": str(e)}), 500
379
+
380
+ @app.route('/api/chat-info/<chat_id>')
381
+ async def chat_info_api(chat_id):
382
+ try:
383
+ chat = await bot.get_chat(int(chat_id))
384
+ return jsonify({
385
+ "title": chat.title,
386
+ "username": chat.username,
387
+ "id": chat.id,
388
+ "members_count": await bot.get_chat_members_count(int(chat_id))
389
+ })
390
+ except Exception as e:
391
+ return jsonify({"error": str(e)}), 500
392
+
393
+ @app.route('/api/restart-bot', methods=['POST'])
394
+ async def restart_bot_api():
395
+ admin_id = request.form.get('admin_id')
396
+ if not admin_id or int(admin_id) not in ADMINS:
397
+ return jsonify({"error": "Unauthorized access"}), 401
398
+ await bot.stop()
399
+ await bot.start()
400
+ return jsonify({"message": "Bot restarted successfully"})
401
+
402
+ @app.route('/api/add-connection', methods=['POST'])
403
+ async def add_connection_api():
404
+ group_id = request.form.get('group_id')
405
+ user_id = request.form.get('user_id')
406
+ if not group_id or not user_id:
407
+ return jsonify({"error": "Group ID and User ID are required"}), 400
408
+ result = await add_connection(group_id, user_id)
409
+ if result:
410
+ return jsonify({"message": "Connection added successfully"})
411
+ else:
412
+ return jsonify({"error": "Connection already exists"}), 400
413
+
414
+ @app.route('/api/delete-connection', methods=['POST'])
415
+ async def delete_connection_api():
416
+ group_id = request.form.get('group_id')
417
+ user_id = request.form.get('user_id')
418
+ if not group_id or not user_id:
419
+ return jsonify({"error": "Group ID and User ID are required"}), 400
420
+ result = await delete_connection(user_id, group_id)
421
+ if result:
422
+ return jsonify({"message": "Connection deleted successfully"})
423
+ else:
424
+ return jsonify({"error": "Connection not found"}), 404
425
+
426
+ @app.route('/api/group-stats')
427
+ async def group_stats_api():
428
+ chat_id = request.args.get('chat_id')
429
+ if not chat_id:
430
+ return jsonify({"error": "Chat ID is required"}), 400
431
+ chat = await db.get_chat(chat_id)
432
+ if not chat:
433
+ return jsonify({"error": "Chat not found"}), 404
434
+ return jsonify({
435
+ "chat_id": chat['id'],
436
+ "title": chat['title'],
437
+ "username": chat['username'],
438
+ "chat_status": chat['chat_status']
439
+ })
440
+
441
+ @app.route('/api/bot-status')
442
+ def bot_status():
443
+ cpu_usage = psutil.cpu_percent()
444
+ ram_usage = psutil.virtual_memory().percent
445
+ total_storage, used_storage, free_storage = shutil.disk_usage("/")
446
+ return jsonify({
447
+ "cpu_usage": cpu_usage,
448
+ "ram_usage": ram_usage,
449
+ "total_storage": humanbytes(total_storage),
450
+ "used_storage": humanbytes(used_storage),
451
+ "free_storage": humanbytes(free_storage),
452
+ "uptime": get_time(time.time() - UPTIME)
453
+ })
454
+
455
+ @app.route('/api/connections')
456
+ async def connections_api():
457
+ user_id = request.args.get('user_id')
458
+ if not user_id:
459
+ return jsonify({"error": "User ID is required"}), 400
460
+ groupids = await all_connections(str(user_id))
461
+ if groupids is None:
462
+ return jsonify({"connections": []})
463
+ connections = []
464
+ for groupid in groupids:
465
+ try:
466
+ ttl = await bot.get_chat(int(groupid))
467
+ title = ttl.title
468
+ active = await if_active(str(user_id), str(groupid))
469
+ connections.append({
470
+ "group_id": groupid,
471
+ "title": title,
472
+ "active": active
473
+ })
474
+ except Exception as e:
475
+ print(f"Error fetching chat info: {e}")
476
+ return jsonify({"connections": connections})
477
+
478
+ if __name__ == '__main__':
479
+ bot.run()
480
+ app.run(host='0.0.0.0', port=7860, debug=False)