|
7 | 7 | from plyer import notification
|
8 | 8 | import psutil
|
9 | 9 | from colorama import Fore, Style
|
10 |
| -from flask import Flask, render_template, redirect, url_for, request, jsonify |
11 |
| -from celery import Celery |
12 | 10 | import random
|
13 | 11 |
|
14 |
| -app = Flask(__name__) |
15 |
| -app.config['CELERY_BROKER_URL'] = 'redis://localhost:80/0' |
16 |
| -app.config['CELERY_RESULT_BACKEND'] = 'redis://localhost:80/0' |
17 |
| -celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL']) |
18 |
| -celery.conf.update(app.config) |
19 |
| - |
20 | 12 |
|
21 | 13 | def progress_bar(current, total, prefix='', suffix='', length=30, fill='=', print_end='\r'):
|
22 | 14 | percent = current / total
|
@@ -127,187 +119,11 @@ async def main():
|
127 | 119 | notification.notify(title=notification_title, message=notification_message)
|
128 | 120 |
|
129 | 121 |
|
130 |
| -async def run_event_loop(): |
131 |
| - loop = asyncio.get_event_loop() |
132 |
| - loop.create_task(app.run(host='127.0.0.1', port=80, debug=True)) |
133 |
| - |
134 |
| - await loop.run_forever() |
135 |
| - |
136 |
| - |
137 |
| -# Tasks------------------------- |
138 |
| - |
139 |
| -# error num 10 : task not found ! |
140 |
| -# status 0: task cancelled |
141 |
| -# status 1: task done |
142 |
| -# status 2: task pending |
143 |
| - |
144 |
| - |
145 |
| -# Dictionary to store tasks with their IDs |
146 |
| -task_dict = {} |
147 |
| - |
148 |
| -done_tasks = {} |
149 |
| - |
150 |
| -cancel_tasks = [] |
151 |
| - |
152 |
| -destroy_tasks = [] |
153 |
| - |
154 |
| -loop = asyncio.get_event_loop() |
155 |
| - |
156 |
| - |
157 |
| -# Function to add a new task |
158 |
| -async def create_search_task(task_id, filename): |
159 |
| - async def search_main_operation(filename): |
160 |
| - start_time = time.time() |
161 |
| - results = await search_all_drives(filename) |
162 |
| - end_time = time.time() |
163 |
| - |
164 |
| - print(results) |
165 |
| - |
166 |
| - final_result = "" |
167 |
| - |
168 |
| - if not results: |
169 |
| - final_result = f"Could not find any similar files or folders for: {filename}\n" |
170 |
| - return jsonify(result=final_result) |
171 |
| - else: |
172 |
| - drives = [] |
173 |
| - for result in results: |
174 |
| - if result[2] != [drive for drive in drives]: |
175 |
| - drives.append(result[2]) |
176 |
| - drive_sorted_result = {} |
177 |
| - for drive in drives: |
178 |
| - drive_sorted_result[drive] = [] |
179 |
| - for result in results: |
180 |
| - if result[2] == drive: |
181 |
| - file_size = os.path.getsize(result[0]) |
182 |
| - drive_sorted_result[drive].append((result[0], result[1], format_size(file_size))) |
183 |
| - print(drive_sorted_result) |
184 |
| - |
185 |
| - # Remove the task from the dictionary after completion |
186 |
| - done_tasks[task_id] = drive_sorted_result |
187 |
| - del task_dict[task_id] |
188 |
| - |
189 |
| - # Schedule the task and store it in the task dictionary |
190 |
| - task = asyncio.ensure_future(search_main_operation(filename=filename)) |
191 |
| - # task.add_done_callback(lambda t: print(f"Task {task_id} destroyed") and destroy_tasks.append(task_id)) |
192 |
| - print(done_tasks) |
193 |
| - print(task_dict) |
194 |
| - print(destroy_tasks) |
195 |
| - task_dict[task_id] = task |
196 |
| - |
197 |
| - |
198 |
| -# Fuction to get task status |
199 |
| -def get_task_status(task_id): |
200 |
| - if task_id in task_dict: |
201 |
| - task = task_dict[str(task_id)] |
202 |
| - if task.done(): |
203 |
| - return 1 |
204 |
| - elif task.cancelled(): |
205 |
| - return 0 |
206 |
| - else: |
207 |
| - return 2 |
208 |
| - else: |
209 |
| - task = task_dict[str(task_id)] |
210 |
| - print(task.done()) |
211 |
| - return 10 |
212 |
| - |
213 |
| - |
214 |
| -# Function to cancel a task |
215 |
| -def cancel_search_task(task_id): |
216 |
| - if task_id in task_dict: |
217 |
| - task = task_dict.pop(task_id) |
218 |
| - task.cancel() |
219 |
| - cancel_tasks.append(task_id) |
220 |
| - return 0 |
221 |
| - else: |
222 |
| - return 10 |
223 |
| - |
224 |
| - |
225 |
| -# Cancel any remaining tasks |
226 |
| -for task_id, task in task_dict.items(): |
227 |
| - cancel_search_task(task_id) |
228 |
| - # task.cancel() |
229 |
| - # print(f"Task {task_id} canceled") |
230 |
| - # cancel_tasks.append(task_id) |
231 |
| - |
232 |
| -# Close the event loop |
233 |
| -loop.close() |
234 |
| - |
235 |
| - |
236 |
| -# Routes------------------------------- |
237 |
| - |
238 |
| -@app.route('/') |
239 |
| -def home(): |
240 |
| - return render_template('index.html') |
241 |
| - |
242 |
| -@app.route('/search', methods=['POST']) |
243 |
| -def search(): |
244 |
| - filename = request.form.get('search') |
245 |
| - |
246 |
| - task_id = str(random.randint(0, 999)) |
247 |
| - # print(task_id) |
248 |
| - asyncio.run(create_search_task(task_id, filename)) |
249 |
| - print(task_id) |
250 |
| - |
251 |
| - return str(task_id), 202 |
252 |
| - |
253 |
| - |
254 |
| -@app.route('/task_status/<task_id>', methods=['GET', 'POST']) |
255 |
| -def task_status(task_id): |
256 |
| - task = get_task_status(task_id) |
257 |
| - |
258 |
| - if task == 0: |
259 |
| - return jsonify(status="cancelled") |
260 |
| - elif task == 1: |
261 |
| - result = task_dict[str(task_id)] |
262 |
| - print(result) |
263 |
| - return jsonify({ |
264 |
| - "status": "done", |
265 |
| - }) |
266 |
| - elif task == 2: |
267 |
| - return jsonify(status="pending") |
268 |
| - elif task == 10: |
269 |
| - return jsonify(status="not_found") |
270 |
| - |
271 |
| -@app.route("/cancel_task/<task_id>") |
272 |
| -def cancel_task(task_id): |
273 |
| - task = cancel_search_task(task_id) |
274 |
| - |
275 |
| - print(task) |
276 |
| - |
277 |
| - return jsonify(task) |
278 |
| - |
279 |
| - |
280 |
| -@app.route('/tasks', methods=['GET', 'POST']) |
281 |
| -def all_tasks(): |
282 |
| - tasks_count = len(task_dict) |
283 |
| - |
284 |
| - result = { |
285 |
| - "count": tasks_count, |
286 |
| - "tasks_dict": task_dict, |
287 |
| - "done_tasks": done_tasks, |
288 |
| - "destroy_tasks": destroy_tasks, |
289 |
| - "cancel_tasks": cancel_tasks |
290 |
| - } |
291 |
| - |
292 |
| - print(result) |
293 |
| - |
294 |
| - return jsonify(tasks_count) |
295 |
| - |
296 |
| -# @app.errorhandler(500) |
297 |
| -# def unknown(e): |
298 |
| -# return redirect(url_for('home')) |
299 |
| - |
300 |
| - |
301 |
| -# @app.errorhandler(404) |
302 |
| -# def not_found(e): |
303 |
| -# return redirect(url_for('home')) |
304 |
| - |
305 | 122 |
|
306 | 123 | if __name__ == "__main__":
|
307 | 124 | try:
|
308 | 125 | try:
|
309 | 126 | asyncio.run(main())
|
310 |
| - # app.run(host='127.0.0.1', port=80, debug=True) |
311 | 127 | except EOFError:
|
312 | 128 | print(Fore.MAGENTA + "\nGoody bye !")
|
313 | 129 | time.sleep(1)
|
|
0 commit comments