{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Download all the ETSI doucments from the website and extract the information from the documents." ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "csv_path = 'ETSICatalog.csv'\n", "# load the CSV file\n", "df = pd.read_csv(csv_path, delimiter=';', skiprows=1, on_bad_lines='skip')\n" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Index(['id', 'ETSI deliverable', 'title', 'Status', 'Details link', 'PDF link',\n", " 'Scope', 'Technical body', 'Keywords'],\n", " dtype='object')\n", " Number of rows: 26442\n" ] } ], "source": [ "print(df.columns)\n", "# get the number of rows and columns\n", "print(' Number of rows:', df.shape[0])" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "64372 Zero-touch network and Service Management\n", "63040 Zero-touch network and Service Management\n", "62010 Zero-touch network and Service Management\n", "61992 Zero-touch network and Service Management\n", "58436 Zero-touch network and Service Management\n", "Name: Scope, dtype: object\n" ] } ], "source": [ "# ge the 'PDF link' column from the dataframe and print first 5 rows\n", "pdf_links = df['Scope']\n", "print(pdf_links.head())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ " 8%|▊ | 2030/26442 [09:21<28:35:26, 4.22s/it]" ] } ], "source": [ "import os\n", "import requests\n", "from tqdm import tqdm\n", "import pandas as pd\n", "\n", "# Function to download files and save metadata, with pause and resume capability\n", "def download_etsi_documents(csv_path, download_dir, state_file='download_state.txt'):\n", " # Load the CSV file\n", " df = pd.read_csv(csv_path, delimiter=';', skiprows=1, on_bad_lines='skip', quotechar='\"')\n", " \n", " # Manually realign the columns\n", " corrected_rows = []\n", " for index, row in df.iterrows():\n", " corrected_row = {\n", " 'id': row['id'],\n", " 'ETSI deliverable': row['ETSI deliverable'],\n", " 'title': row['title'],\n", " 'Status': row['Status'],\n", " 'Details link': row['Details link'],\n", " 'PDF link': row['PDF link'],\n", " 'Scope': row['Scope'],\n", " 'Technical body': row['Technical body'],\n", " 'Keywords': row['Keywords']\n", " }\n", " \n", " # Correcting the misalignment\n", " corrected_row['PDF link'] = corrected_row['Details link']\n", " corrected_row['Details link'] = corrected_row['Status']\n", " corrected_row['Status'] = corrected_row['title']\n", " corrected_row['title'] = corrected_row['ETSI deliverable']\n", " corrected_row['ETSI deliverable'] = corrected_row['id']\n", " corrected_row['id'] = index # Reassigning ID to the index for unique identification\n", "\n", " corrected_rows.append(corrected_row)\n", "\n", " # Create a new DataFrame with the corrected rows\n", " corrected_df = pd.DataFrame(corrected_rows)\n", " \n", " # Create download directory if it doesn't exist\n", " os.makedirs(download_dir, exist_ok=True)\n", " \n", " # Load the state of downloaded documents\n", " if os.path.exists(state_file):\n", " with open(state_file, 'r') as file:\n", " completed_docs = set(file.read().splitlines())\n", " else:\n", " completed_docs = set()\n", " \n", " for index, row in tqdm(corrected_df.iterrows(), total=corrected_df.shape[0]):\n", " doc_id = str(row['id'])\n", " \n", " if doc_id in completed_docs:\n", " continue\n", " \n", " pdf_link = row['PDF link']\n", " title = row['ETSI deliverable']\n", " status = row['Status']\n", " details_link = row['Details link']\n", " scope = row['Scope']\n", " technical_body = row['Technical body']\n", " keywords = row['Keywords']\n", " \n", " try:\n", " response = requests.get(pdf_link, stream=True)\n", " if response.status_code == 200:\n", " file_path = os.path.join(download_dir, f\"{doc_id}.pdf\")\n", " with open(file_path, 'wb') as pdf_file:\n", " for chunk in response.iter_content(chunk_size=1024):\n", " if chunk:\n", " pdf_file.write(chunk)\n", " \n", " # Write metadata to a text file\n", " metadata_path = os.path.join(download_dir, f\"{doc_id}_metadata.txt\")\n", " with open(metadata_path, 'w') as metadata_file:\n", " metadata_file.write(f\"ID: {doc_id}\\n\")\n", " metadata_file.write(f\"Title: {title}\\n\")\n", " metadata_file.write(f\"Status: {status}\\n\")\n", " metadata_file.write(f\"Details Link: {details_link}\\n\")\n", " metadata_file.write(f\"Scope: {scope}\\n\")\n", " metadata_file.write(f\"Technical Body: {technical_body}\\n\")\n", " metadata_file.write(f\"Keywords: {keywords}\\n\")\n", " \n", " # Update the state file\n", " with open(state_file, 'a') as file:\n", " file.write(doc_id + '\\n')\n", " \n", " else:\n", " print(f\"Failed to download {pdf_link}\")\n", " except Exception as e:\n", " print(f\"Error downloading {pdf_link}: {e}\")\n", "\n", "# Example usage\n", "csv_path = 'ETSICatalog.csv'\n", "download_dir = './data/'\n", "download_etsi_documents(csv_path, download_dir) # Uncomment to execute\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.12" } }, "nbformat": 4, "nbformat_minor": 4 }