|
7 | 7 | from werkzeug.utils import secure_filename |
8 | 8 | import datetime |
9 | 9 | import re |
10 | | - |
| 10 | +import csv |
11 | 11 | # ========== SETTINGS ========== |
12 | 12 | UPLOAD_FOLDER = 'uploads' |
13 | 13 | DB_NAME = 'patterns-matter.db' |
@@ -413,68 +413,26 @@ def extract_drive_id(link): |
413 | 413 | return match.group(1) |
414 | 414 | raise ValueError("Invalid Drive link") |
415 | 415 |
|
416 | | -@app.route('/clips', methods=['GET', 'POST']) |
| 416 | +@app.route('/clips') |
417 | 417 | def public_clips(): |
418 | 418 | admin = session.get('admin', False) |
419 | | - message = "" |
420 | | - db_filename = None |
421 | | - |
422 | | - if admin and request.method == 'POST': |
423 | | - action = request.form.get('action', '') |
424 | | - title = request.form.get('title', '').strip() |
425 | | - description = request.form.get('description', '').strip() |
426 | | - |
427 | | - if not title: |
428 | | - message = "Title is required." |
429 | | - elif action == 'upload_file': |
430 | | - file = request.files.get('file') |
431 | | - if not file or not allowed_music_file(file.filename): |
432 | | - message = "Please upload a valid music or video file." |
433 | | - else: |
434 | | - filename = secure_filename(file.filename) |
435 | | - upload_folder = os.path.join(app.config['UPLOAD_FOLDER'], 'clips') |
436 | | - os.makedirs(upload_folder, exist_ok=True) |
437 | | - filepath = os.path.join(upload_folder, filename) |
438 | | - file.save(filepath) |
439 | | - db_filename = 'clips/' + filename |
440 | | - |
441 | | - elif action == 'drive_link': |
442 | | - link = request.form.get('link', '').strip() |
443 | | - try: |
444 | | - file_id = extract_drive_id(link) |
445 | | - preview_url = f"https://drive.google.com/file/d/{file_id}/preview" |
446 | | - download_url = f"https://drive.google.com/uc?export=download&id={file_id}" |
447 | | - db_filename = f"{preview_url}||{download_url}" |
448 | | - except Exception as e: |
449 | | - message = f"Invalid Google Drive link. ({e})" |
450 | | - |
451 | | - if db_filename: |
452 | | - try: |
453 | | - with sqlite3.connect(DB_NAME) as conn: |
454 | | - c = conn.cursor() |
455 | | - c.execute( |
456 | | - "INSERT INTO music_clips (filename, title, description) VALUES (?, ?, ?)", |
457 | | - (db_filename, title, description) |
458 | | - ) |
459 | | - conn.commit() |
460 | | - message = "Clip added!" |
461 | | - except Exception as e: |
462 | | - message = "Error saving to database: " + str(e) |
| 419 | + clips = [] |
463 | 420 |
|
464 | | - # Fetch all clips |
465 | 421 | try: |
466 | | - with sqlite3.connect(DB_NAME) as conn: |
467 | | - c = conn.cursor() |
468 | | - c.execute("SELECT id, filename, title, description FROM music_clips") |
469 | | - clips = [ |
470 | | - (id, filename.replace('\\', '/'), title, description) |
471 | | - for (id, filename, title, description) in c.fetchall() |
472 | | - ] |
473 | | - except sqlite3.OperationalError: |
| 422 | + with open('drive_music.csv', encoding='utf-8') as f: |
| 423 | + reader = csv.DictReader(f) |
| 424 | + for row in reader: |
| 425 | + title = row.get('title', '').strip() |
| 426 | + description = row.get('description', '').strip() |
| 427 | + preview = row.get('preview_url', '').strip() |
| 428 | + download = row.get('download_url', '').strip() |
| 429 | + if preview and download: |
| 430 | + clips.append((preview, download, title, description)) |
| 431 | + except Exception as e: |
| 432 | + print("Error reading drive_music.csv:", e) |
474 | 433 | clips = [] |
475 | 434 |
|
476 | | - return render_template('clips.html', clips=clips, admin=admin, message=message) |
477 | | - |
| 435 | + return render_template('clips.html', clips=clips, admin=admin) |
478 | 436 |
|
479 | 437 | @app.route('/dataset/<table>') |
480 | 438 | def public_view(table): |
@@ -569,6 +527,42 @@ def delete_dataset_file(property_name, tab, filename): |
569 | 527 | conn.commit() |
570 | 528 | return redirect(url_for('property_detail', property_name=property_name, tab=tab)) |
571 | 529 |
|
| 530 | +@app.route('/add_drive_clip', methods=['GET', 'POST']) |
| 531 | +def add_drive_clip(): |
| 532 | + if not session.get('admin'): |
| 533 | + return redirect(url_for('login')) |
| 534 | + |
| 535 | + message = "" |
| 536 | + if request.method == 'POST': |
| 537 | + link = request.form.get('link', '').strip() |
| 538 | + title = request.form.get('title', '').strip() |
| 539 | + description = request.form.get('description', '').strip() |
| 540 | + |
| 541 | + def extract_drive_id(link): |
| 542 | + match = re.search(r'/d/([a-zA-Z0-9_-]+)', link) |
| 543 | + if match: |
| 544 | + return match.group(1) |
| 545 | + match = re.search(r'id=([a-zA-Z0-9_-]+)', link) |
| 546 | + if match: |
| 547 | + return match.group(1) |
| 548 | + return None |
| 549 | + |
| 550 | + file_id = extract_drive_id(link) |
| 551 | + if file_id and title: |
| 552 | + preview_url = f"https://drive.google.com/file/d/{file_id}/preview" |
| 553 | + download_url = f"https://drive.google.com/uc?export=download&id={file_id}" |
| 554 | + try: |
| 555 | + with open('drive_music.csv', 'a', newline='', encoding='utf-8') as f: |
| 556 | + writer = csv.writer(f) |
| 557 | + writer.writerow([title, description, preview_url, download_url]) |
| 558 | + message = "✅ Clip added successfully!" |
| 559 | + except Exception as e: |
| 560 | + message = f"❌ Error writing to CSV: {e}" |
| 561 | + else: |
| 562 | + message = "❌ Invalid link or missing title." |
| 563 | + |
| 564 | + return render_template('add_drive_clip.html', message=message) |
| 565 | + |
572 | 566 | # --- Print routes for debugging (optional, can comment out) --- |
573 | 567 | for rule in app.url_map.iter_rules(): |
574 | 568 | print(rule.endpoint, rule) |
|
0 commit comments