File size: 7,344 Bytes
7adc29d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
from io import StringIO
import numpy as np
import pandas as pd
import requests
from bs4 import BeautifulSoup
import json
import os
import traceback
import uuid
import zipfile
import io
import subprocess
import os
import re
import time
from datetime import datetime
from dotenv import load_dotenv
import warnings
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel
from typing import Any, Dict, List, Literal, Optional
load_dotenv()
warnings.filterwarnings("ignore")
app = FastAPI(title="3GPP Specification Splitter API",
description="API to split and display specifications by their chapters & sub-chapters")
app.mount("/static", StaticFiles(directory="static"), name="static")
origins = [
"*",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
def get_text(specification: str, version: str):
"""Récupère les bytes du PDF à partir d'une spécification et d'une version."""
doc_id = specification
series = doc_id.split(".")[0]
response = requests.get(
f"https://www.3gpp.org/ftp/Specs/archive/{series}_series/{doc_id}/{doc_id.replace('.', '')}-{version}.zip",
verify=False,
headers={"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'}
)
if response.status_code != 200:
raise Exception(f"Téléchargement du ZIP échoué pour {specification}-{version}")
zip_bytes = io.BytesIO(response.content)
with zipfile.ZipFile(zip_bytes) as zf:
for file_name in zf.namelist():
if file_name.endswith("zip"):
print("Another ZIP !")
zip_bytes = io.BytesIO(zf.read(file_name))
zf = zipfile.ZipFile(zip_bytes)
for file_name2 in zf.namelist():
if file_name2.endswith("doc") or file_name2.endswith("docx"):
if "cover" in file_name2.lower():
print("COVER !")
continue
ext = file_name2.split(".")[-1]
doc_bytes = zf.read(file_name2)
temp_id = str(uuid.uuid4())
input_path = f"/tmp/{temp_id}.{ext}"
output_path = f"/tmp/{temp_id}.txt"
with open(input_path, "wb") as f:
f.write(doc_bytes)
subprocess.run([
"libreoffice",
"--headless",
"--convert-to", "txt",
"--outdir", "/tmp",
input_path
], check=True)
with open(output_path, "r") as f:
txt_data = [line.strip() for line in f if line.strip()]
os.remove(input_path)
os.remove(output_path)
return txt_data
elif file_name.endswith("doc") or file_name.endswith("docx"):
if "cover" in file_name.lower():
print("COVER !")
continue
ext = file_name.split(".")[-1]
doc_bytes = zf.read(file_name)
temp_id = str(uuid.uuid4())
input_path = f"/tmp/{temp_id}.{ext}"
output_path = f"/tmp/{temp_id}.txt"
print("Ecriture")
with open(input_path, "wb") as f:
f.write(doc_bytes)
print("Convertissement")
subprocess.run([
"libreoffice",
"--headless",
"--convert-to", "txt",
"--outdir", "/tmp",
input_path
], check=True)
print("Ecriture TXT")
with open(output_path, "r", encoding="utf-8") as f:
txt_data = [line.strip() for line in f if line.strip()]
os.remove(input_path)
os.remove(output_path)
return txt_data
raise Exception(f"Aucun fichier .doc/.docx trouvé dans le ZIP pour {specification}-{version}")
def get_latest_version(spec: str) -> str:
try:
req = requests.post("https://organizedprogrammers-3gppdocfinder/find", headers={"Accept": "application/json"}, json=json.dumps({"doc_id": spec}))
except Exception as e:
raise HTTPException(500, f"An error has occured while getting latest version: {e}")
if req.status_code == 200:
reqJS = req.json
return reqJS['version']
else:
raise HTTPException(req.status_code, "An error has occured while getting latest version")
class SpecRequest(BaseModel):
specification: str
version: Optional[str] = None
@app.get("/")
def main_page():
return FileResponse(os.path.join("templates", "index.html"))
@app.post("/from-search")
def get_file_from_spec_id_version(req: SpecRequest) -> Dict[str, str]:
spec = req.specification
version = req.version
if not version:
version = get_latest_version(spec)
text = get_text(spec, version)
forewords = []
for x in range(len(text)):
line = text[x]
if "Foreword" in line:
forewords.append(x)
if len(forewords) >= 2:
break
toc_brut = text[forewords[0]:forewords[1]]
chapters = []
for line in toc_brut:
x = line.split("\t")
if re.fullmatch(r"^\d\t[A-Z][a-zA-Z0-9\s,;!?'.-]*$", line):
chapters.append(x[0] if len(x) == 1 else "\t".join(x[:2]))
if re.fullmatch(r"^\d\.\d\t[A-Z][a-zA-Z0-9\s,;!?'.-]*$", line):
chapters.append(x[0] if len(x) == 1 else "\t".join(x[:2]))
if re.fullmatch(r"^\d\.\d\.\d\t[A-Z][a-zA-Z0-9\s,;!?'.-]*$", line):
chapters.append(x[0] if len(x) == 1 else "\t".join(x[:2]))
if re.fullmatch(r"^\d\.\d\.\d.\d\t[A-Z][a-zA-Z0-9\s,;!?'.-]*$", line):
chapters.append(x[0] if len(x) == 1 else "\t".join(x[:2]))
if re.fullmatch(r"^\d\.\d\.\d.\d.\d\t[A-Z][a-zA-Z0-9\s,;!?'.-]*$", line):
chapters.append(x[0] if len(x) == 1 else "\t".join(x[:2]))
real_toc_indexes = {}
for chapter in chapters:
try:
x = text.index(chapter)
real_toc_indexes[chapter] = x
except ValueError as e:
real_toc_indexes[chapter] = -float("inf")
document = {}
toc = list(real_toc_indexes.keys())
index_toc = list(real_toc_indexes.values())
curr_index = 0
for x in range(1, len(toc)):
document[toc[curr_index].replace("\t", " ")] = re.sub(r"[\ \t]+", " ", "\n".join(text[index_toc[curr_index]+1:index_toc[x]]))
curr_index = x
document[toc[curr_index].replace("\t"," ")] = re.sub(r"\s+", " ", " ".join(text[index_toc[curr_index]+1:]))
return document
|