-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
163 lines (142 loc) · 5.79 KB
/
app.py
File metadata and controls
163 lines (142 loc) · 5.79 KB
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
from __future__ import annotations
import json
import os
import sqlite3
import importlib.util
from concurrent.futures import ThreadPoolExecutor, as_completed
from datetime import datetime
from pathlib import Path
from typing import Any, Dict, Optional, Tuple
try:
from flask import Flask, render_template, request, url_for
except ImportError:
Flask = None
render_template = None
request = None
url_for = None
BASE_DIR = Path(__file__).resolve().parent
TEMPLATE_DIR = BASE_DIR / "templates"
STATIC_DIR = BASE_DIR / "static"
PCASE_SCRIPT = BASE_DIR / "2nd scrap.py"
DATA_EXTRACT_SCRIPT = BASE_DIR / "data extract.py"
TESSERACT_BUNDLE = BASE_DIR / "external" / "tesseract" / "tesseract.exe"
DB_FILE = BASE_DIR / "queries.db"
def init_db() -> None:
with sqlite3.connect(DB_FILE) as conn:
conn.execute(
"""
CREATE TABLE IF NOT EXISTS queries (
id INTEGER PRIMARY KEY AUTOINCREMENT,
case_type TEXT NOT NULL,
case_number TEXT NOT NULL,
year TEXT NOT NULL,
timestamp TEXT NOT NULL,
party1 TEXT,
party2 TEXT,
filing_date TEXT,
next_hearing_date TEXT,
pdf_link TEXT
)
"""
)
def log_query(result: Dict[str, Any]) -> None:
timestamp = datetime.utcnow().isoformat()
with sqlite3.connect(DB_FILE) as conn:
conn.execute(
"""
INSERT INTO queries (
case_type, case_number, year, timestamp,
party1, party2, filing_date, next_hearing_date, pdf_link
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(
result.get("case_type"),
result.get("case_number"),
result.get("year"),
timestamp,
result.get("party1"),
result.get("party2"),
result.get("filing_date"),
result.get("next_hearing_date"),
result.get("pdf_link"),
),
)
def load_external_function(file_path: Path, func_name: str):
spec = importlib.util.spec_from_file_location(func_name, file_path)
if spec is None or spec.loader is None:
raise ImportError(f"Cannot load spec from {file_path}")
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
if not hasattr(module, func_name):
raise ImportError(f"Function {func_name} not found in {file_path}")
return getattr(module, func_name)
def run_scrapers(case_type: str, case_number: str, year: str) -> Tuple[Optional[Dict[str, Any]], Optional[Dict[str, Any]]]:
get_date_of_filing = load_external_function(PCASE_SCRIPT, "get_date_of_filing")
fetch_case_details_external = load_external_function(DATA_EXTRACT_SCRIPT, "fetch_case_details")
print(fetch_case_details_external)
with ThreadPoolExecutor(max_workers=2) as executor:
future_pcase = executor.submit(get_date_of_filing, case_type, case_number, year)
future_data = executor.submit(
fetch_case_details_external,
case_type=case_type,
case_number=case_number,
year_text=year
)
pcase_result = future_pcase.result()
data_result = future_data.result()
return pcase_result, data_result
def create_app() -> Flask:
if Flask is None:
raise ImportError(
"Flask is not installed. Install it or use the standalone server to run the app."
)
app = Flask(__name__, static_folder=str(STATIC_DIR), template_folder=str(TEMPLATE_DIR))
init_db()
DEFAULT_CASE_TYPES = [
"FAO", "LPA", "WPC", "CRL", "RCREV", "CM", "MACA", "RFA"
]
@app.route("/", methods=["GET", "POST"])
def index():
error_message: Optional[str] = None
result_data: Optional[Dict[str, Any]] = None
available_case_types = DEFAULT_CASE_TYPES
if request.method == "POST":
case_type = request.form.get("case_type", "").strip().upper()
case_number = request.form.get("case_number", "").strip()
year = request.form.get("year", "").strip()
if not (case_type and case_number and year):
error_message = "Please enter all fields."
else:
try:
pcase_result, data_result = run_scrapers(case_type, case_number, year)
except Exception as e:
error_message = f"Error invoking scrapers: {e}"
pcase_result = None
data_result = None
if data_result is None:
error_message = "No case found for the given details. Please verify the case number and year."
elif isinstance(pcase_result, str):
error_message = pcase_result
else:
result_data = {
"case_type": case_type,
"case_number": case_number,
"year": year,
"party1": pcase_result.get("petitioner"),
"party2": pcase_result.get("respondent"),
"filing_date": pcase_result.get("date_of_filing"),
"next_hearing_date": data_result.get("next_hearing_date"),
"pdf_link": data_result.get("first_order_pdf"),
}
log_query(result_data)
return render_template(
"index.html",
available_case_types=available_case_types,
error_message=error_message,
result=result_data,
)
return app
if __name__ == "__main__":
app = create_app()
port = int(os.environ.get("PORT", 5000))
app.run(host="0.0.0.0", port=port, debug=True)