Last active
August 30, 2025 16:57
-
-
Save chankanzu/0eff0eb5b11fae9f3ea37ffc62559fcb to your computer and use it in GitHub Desktop.
UrltoGdrive-Ver3.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "nbformat": 4, | |
| "nbformat_minor": 0, | |
| "metadata": { | |
| "colab": { | |
| "name": "UrltoGdrive-Ver3.ipynb", | |
| "provenance": [], | |
| "include_colab_link": true | |
| }, | |
| "kernelspec": { | |
| "name": "python3", | |
| "display_name": "Python 3" | |
| } | |
| }, | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "view-in-github", | |
| "colab_type": "text" | |
| }, | |
| "source": [ | |
| "<a href=\"https://colab.research.google.com/gist/chankanzu/0eff0eb5b11fae9f3ea37ffc62559fcb/urltogdrive-ver2.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "id": "ck7pMIEAlgxz", | |
| "outputId": "e044306f-4f29-4449-b12a-ee03d9bb6a78", | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| } | |
| }, | |
| "source": [ | |
| "from google.colab import drive\n", | |
| "drive.mount( '/content/gdrive' )" | |
| ], | |
| "execution_count": 1, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "Mounted at /content/gdrive\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "import os\n", | |
| "import requests\n", | |
| "from tqdm import tqdm\n", | |
| "from urllib.parse import urlparse, unquote\n", | |
| "from google.colab import files\n", | |
| "\n", | |
| "# 📌 Bước 1: Upload file chứa danh sách URL\n", | |
| "print(\"⏳ Vui lòng upload file .txt chứa danh sách URL...\")\n", | |
| "uploaded = files.upload()\n", | |
| "\n", | |
| "# 📌 Bước 2: Đọc danh sách URL từ file\n", | |
| "file_name = list(uploaded.keys())[0]\n", | |
| "with open(file_name, \"r\") as f:\n", | |
| " # Lọc bỏ các dòng trống và các dòng chỉ chứa khoảng trắng\n", | |
| " url_list = [line.strip() for line in f if line.strip()]\n", | |
| "\n", | |
| "# 📌 Bước 3: Cấu hình thư mục lưu file (Chỉnh sửa nếu cần)\n", | |
| "#@markdown **Nhập đường dẫn thư mục trong Google Drive để lưu file:**\n", | |
| "Local = \"/content/gdrive/Shareddrives/thuanthanh3/ABKG5/\" #@param {type:\"string\"}\n", | |
| "os.makedirs(Local, exist_ok=True) # Tự động tạo thư mục nếu chưa có\n", | |
| "\n", | |
| "# ---------------------------------------------------------------------------\n", | |
| "\n", | |
| "# Hàm lấy tên file chuẩn từ URL\n", | |
| "def get_filename_from_url(url):\n", | |
| " \"\"\"Trích xuất và giải mã tên file từ một URL.\"\"\"\n", | |
| " parsed_url = urlparse(url)\n", | |
| " # unquote giúp giải mã các ký tự đặc biệt như %20 thành dấu cách\n", | |
| " return unquote(os.path.basename(parsed_url.path))\n", | |
| "\n", | |
| "# Hàm tải file với chức năng kiểm tra file đã tồn tại\n", | |
| "def download_file(url, destination_folder):\n", | |
| " \"\"\"Tải file từ URL vào thư mục đích, bỏ qua nếu file đã tồn tại.\"\"\"\n", | |
| " filename = get_filename_from_url(url)\n", | |
| " if not filename:\n", | |
| " print(f\"⚠️ Không thể lấy tên file từ URL: {url}\")\n", | |
| " return\n", | |
| "\n", | |
| " file_path = os.path.join(destination_folder, filename)\n", | |
| "\n", | |
| " # === KIỂM TRA FILE TỒN TẠI ===\n", | |
| " if os.path.exists(file_path):\n", | |
| " print(f\"⏩ Đã tồn tại, bỏ qua: {filename}\")\n", | |
| " return # Thoát khỏi hàm, không cần tải lại\n", | |
| "\n", | |
| " # === TIẾN HÀNH TẢI FILE NẾU CHƯA CÓ ===\n", | |
| " try:\n", | |
| " with requests.Session() as session:\n", | |
| " response = session.get(url, stream=True, timeout=30)\n", | |
| " response.raise_for_status() # Báo lỗi nếu có vấn đề (vd: 404 Not Found)\n", | |
| "\n", | |
| " total_size = int(response.headers.get(\"content-length\", 0))\n", | |
| " chunk_size = 1024 * 1024 # Tải mỗi lần 1MB để hiệu quả hơn\n", | |
| "\n", | |
| " print(f\"⏬ Đang tải: {filename}\")\n", | |
| " with open(file_path, \"wb\") as file, tqdm(\n", | |
| " total=total_size,\n", | |
| " unit=\"B\",\n", | |
| " unit_scale=True,\n", | |
| " unit_divisor=1024,\n", | |
| " ) as bar:\n", | |
| " for chunk in response.iter_content(chunk_size=chunk_size):\n", | |
| " if chunk:\n", | |
| " file.write(chunk)\n", | |
| " bar.update(len(chunk))\n", | |
| "\n", | |
| " print(f\"✅ Tải xong: {filename}\")\n", | |
| "\n", | |
| " except requests.exceptions.RequestException as e:\n", | |
| " print(f\"❌ Lỗi khi tải {filename}: {e}\")\n", | |
| " # Nếu tải lỗi, xóa file rác (nếu có)\n", | |
| " if os.path.exists(file_path):\n", | |
| " os.remove(file_path)\n", | |
| "\n", | |
| "# ---------------------------------------------------------------------------\n", | |
| "\n", | |
| "# 📌 Bước 4: Tải toàn bộ danh sách\n", | |
| "print(\"\\n\" + \"=\"*50)\n", | |
| "print(f\"📂 Bắt đầu quá trình tải {len(url_list)} file vào thư mục: {Local}\")\n", | |
| "print(\"=\"*50)\n", | |
| "\n", | |
| "for url in url_list:\n", | |
| " download_file(url, Local)\n", | |
| "\n", | |
| "print(\"\\n🎉 Hoàn thành! Tất cả các file đã được xử lý.\")" | |
| ], | |
| "metadata": { | |
| "id": "6XgUbfi-1QQo", | |
| "cellView": "form", | |
| "outputId": "432b54cd-0fdf-4ae7-f8ca-eb9ff23a4da9", | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| } | |
| }, | |
| "execution_count": null, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stderr", | |
| "text": [ | |
| "100%|██████████| 92.1M/92.1M [00:02<00:00, 33.2MB/s]\n" | |
| ] | |
| }, | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "✅ Tải xong: 05WR121G_N2000.mp4\n", | |
| "⏬ Đang tải: 05AR122G_N2000.mp4\n" | |
| ] | |
| }, | |
| { | |
| "output_type": "stream", | |
| "name": "stderr", | |
| "text": [ | |
| "100%|██████████| 52.7M/52.7M [00:01<00:00, 31.8MB/s]\n" | |
| ] | |
| }, | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "✅ Tải xong: 05AR122G_N2000.mp4\n", | |
| "⏬ Đang tải: 05AT122G_N2000.mp4\n" | |
| ] | |
| }, | |
| { | |
| "output_type": "stream", | |
| "name": "stderr", | |
| "text": [ | |
| " 36%|███▌ | 213M/595M [00:05<00:09, 43.8MB/s]" | |
| ] | |
| } | |
| ] | |
| } | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment