File size: 8,884 Bytes
9f149f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2cdd1ea
9f149f3
 
 
2cdd1ea
 
 
 
9f149f3
2cdd1ea
 
 
9f149f3
2cdd1ea
 
 
 
9f149f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2cdd1ea
9f149f3
 
 
 
 
 
2cdd1ea
9f149f3
 
 
2cdd1ea
 
9f149f3
2cdd1ea
 
 
 
 
 
9f149f3
 
2cdd1ea
 
 
 
 
 
 
 
 
 
 
 
 
9f149f3
2cdd1ea
 
 
 
 
 
 
 
 
 
 
 
9f149f3
2cdd1ea
 
 
 
 
 
 
 
 
 
 
 
 
9f149f3
2cdd1ea
 
 
 
 
9f149f3
2cdd1ea
 
 
 
 
 
 
 
 
9f149f3
 
 
 
 
2cdd1ea
 
9f149f3
2cdd1ea
9f149f3
 
2cdd1ea
9f149f3
 
 
 
 
 
 
2cdd1ea
9f149f3
 
 
2cdd1ea
 
 
9f149f3
2cdd1ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
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
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

warnings.filterwarnings("ignore")

app = FastAPI(title="3GPP Document Finder API", 
              description="API to find 3GPP documents based on TSG document IDs")

app.mount("/static", StaticFiles(directory="static"), name="static")

origins = [
    "*",
]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

class DocRequest(BaseModel):
    doc_id: str

class DocResponse(BaseModel):
    doc_id: str
    url: str
    release: Optional[str] = None
    scope: Optional[str] = None
    search_time: float

class MultiDocResponse(BaseModel):
    doc_id: str
    results: List[DocResponse]
    search_time: float

class BatchDocRequest(BaseModel):
    doc_ids: List[str]
    release: Optional[int] = None

class BatchDocResponse(BaseModel):
    results: Dict[str, str]
    missing: List[str]
    search_time: float

class DocFinder:
    def __init__(self):
        self.main_ftp_url = "https://docbox.etsi.org/SET"
        self.session = requests.Session()
        self.indexer_file = "indexed_docs.json"
        self.indexer, self.last_indexer_date = self.load_indexer()
        self.session.post("https://portal.etsi.org/ETSIPages/LoginEOL.ashx", verify=False, headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36"}, data=json.dumps({"username": os.environ.get("EOL_USER"), "password": os.environ.get("EOL_PASSWORD")}))
    
    def load_indexer(self):
        if os.path.exists(self.indexer_file):
            with open(self.indexer_file, "r", encoding="utf-8") as f:
                x = json.load(f)
                return x["docs"], x["last_indexed_date"]
        return {}, None

    def save_indexer(self):
        today = datetime.today()
        self.last_indexer_date = today.strftime("%d/%m/%Y-%H:%M:%S")
        with open(self.indexer_file, "w", encoding="utf-8") as f:
            output = {"docs": self.indexer, "last_indexed_date": self.last_indexer_date}
            json.dump(output, f, indent=4, ensure_ascii=False)
    
    def get_workgroup(self, doc: str):
        main_tsg = "SET-WG-R" if any(doc.startswith(kw) for kw in ["SETREQ", "SCPREQ"]) else "SET-WG-T" if any(doc.startswith(kw) for kw in ["SETTEC", "SCPTEC"]) else "SET" if any(doc.startswith(kw) for kw in ["SET", "SCP"]) else None
        if main_tsg is None:
            return None, None, None
        regex = re.search(r'\(([^)]+)\)', doc)
        workgroup = "20" + regex.group(1)
        return main_tsg, workgroup, doc

    def find_workgroup_url(self, main_tsg, workgroup):
        response = self.session.get(f"{self.main_ftp_url}/{main_tsg}/05-CONTRIBUTIONS", verify=False)
        soup = BeautifulSoup(response.text, 'html.parser')
        for item in soup.find_all("tr"):
            link = item.find("a")
            if link and workgroup in link.get_text():
                return f"{self.main_ftp_url}/{main_tsg}/05-CONTRIBUTIONS/{link.get_text()}"

        return f"{self.main_ftp_url}/{main_tsg}/05-CONTRIBUTIONS/{workgroup}"
    
    def get_docs_from_url(self, url):
        try:
            response = self.session.get(url, verify=False, timeout=15)
            soup = BeautifulSoup(response.text, "html.parser")
            return [item.get_text() for item in soup.select("tr td a")]
        except Exception as e:
            print(f"Error accessing {url}: {e}")
            return []
    
    def search_document(self, doc_id: str):
        original = doc_id

        if original in self.indexer:
            return self.indexer[original]
        for doc in self.indexer:
            if doc.startswith(original):
                return self.indexer[doc]
        
        main_tsg, workgroup, doc = self.get_workgroup(doc_id)
        urls = []
        if main_tsg:
            wg_url = self.find_workgroup_url(main_tsg, workgroup)
            if wg_url:
                files = self.get_docs_from_url(wg_url)
                for f in files:
                    if doc in f.lower() or original in f:
                        print(f)
                        doc_url = f"{wg_url}/{f}"
                        self.indexer[original] = doc_url
                        self.save_indexer()
                        urls.append(doc_url)
        return urls[0] if len(urls) == 1 else urls[-2] if len(urls) > 1 else f"Document {doc_id} not found"

class SpecFinder:
    def __init__(self):
        self.main_url = "https://www.etsi.org/deliver/etsi_ts"
        self.indexer_file = "indexed_specifications.json"
        self.indexer, self.last_indexer_date = self.load_indexer()
        self.headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36"}
        
    
    def load_indexer(self):
        if os.path.exists(self.indexer_file):
            with open(self.indexer_file, "r", encoding="utf-8") as f:
                x = json.load(f)
                return x["specs"], x["last_indexed_date"]
        return {}, None

    def save_indexer(self):
        today = datetime.today()
        self.last_indexer_date = today.strftime("%d/%m/%Y-%H:%M:%S")
        with open(self.indexer_file, "w", encoding="utf-8") as f:
            output = {"specs": self.indexer, "last_indexed_date": self.last_indexer_date}
            json.dump(output, f, indent=4, ensure_ascii=False)

    def get_spec_path(self, doc_id: str):
        if "-" in doc_id:
            position, part = doc_id.split("-")
        else:
            position, part = doc_id, None
        
        position = position.replace(" ", "")
        if part:
            if len(part) == 1:
                part = "0" + part
        spec_folder = position + part if part is not None else position
        return f"{int(position) - (int(position)%100)}_{int(position) - (int(position)%100) + 99}/{spec_folder}"

    def get_docs_from_url(self, url):
        try:
            response = requests.get(url, verify=False, timeout=15)
            soup = BeautifulSoup(response.text, "html.parser")
            docs = [item.get_text() for item in soup.find_all("a")][1:]
            return docs
        except Exception as e:
            print(f"Error accessing {url}: {e}")
            return []
    
    def search_document(self, doc_id: str):
        # Example : 103 666[-2 opt]
        original = doc_id

        if original in self.indexer:
            return self.indexer[original]
        for doc in self.indexer:
            if doc.startswith(original):
                return self.indexer[doc]

        url = f"{self.main_url}/{self.get_spec_path(original)}/"
        
        releases = self.get_docs_from_url(url)
        files = self.get_docs_from_url(url + releases[-1])
        for f in files:
            if f.endswith(".pdf"):
                return url + releases[-1] + "/" + f
                    
        return f"Specification {doc_id} not found"

@app.get("/")
async def main_menu():
    return FileResponse(os.path.join("templates", "index.html"))

finder_doc = DocFinder()
finder_spec = SpecFinder()

@app.post("/find")
def find_document(request: DocRequest):
    start_time = time.time()
    finder = finder_spec if request.doc_id[0].isnumeric() else finder_doc
    result = finder.search_document(request.doc_id)

    if "not found" not in result and "Could not" not in result and "Unable" not in result:
        return DocResponse(
            doc_id=request.doc_id,
            url=result,
            search_time=time.time() - start_time
        ) if not isinstance(result, list) else result
    else:
        raise HTTPException(status_code=404, detail=result)

@app.post("/batch", response_model=BatchDocResponse)
def find_documents_batch(request: BatchDocRequest):
    start_time = time.time()
    
    results = {}
    missing = []
    
    for doc_id in request.doc_ids:
        finder = finder_doc if doc_id[0].isalpha() else finder_spec
        result = finder.search_document(doc_id)
        if "not found" not in result and "Could not" not in result and "Unable" not in result:
            results[doc_id] = result
        else:
            missing.append(doc_id)
    
    return BatchDocResponse(
        results=results,
        missing=missing,
        search_time=time.time() - start_time
    )