gopallikehack commited on
Commit
6aec4e1
·
verified ·
1 Parent(s): 29631de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -79
app.py CHANGED
@@ -1,98 +1,61 @@
1
  import os
2
  import json
3
- import re
4
- from fastapi import FastAPI, Query
 
5
 
6
- app = FastAPI(title="Hitek Binary Search API", developer="Gopal Parmar")
7
 
8
  MOUNT_PATH = "/data/"
9
  TEST_FILE = "Hitek01.7z"
10
 
11
- def search_in_binary(filepath: str, query: str, limit: int = 10):
12
- """Search for query in binary file (no extract)"""
13
- results = []
14
- query_bytes = query.encode('utf-8')
15
-
16
- try:
17
- with open(filepath, 'rb') as f:
18
- # Read in chunks to handle large file
19
- chunk_size = 1024 * 1024 # 1MB
20
- chunk = b''
21
- while True:
22
- data = f.read(chunk_size)
23
- if not data:
24
- break
25
- # Combine with leftover from previous chunk
26
- text = (chunk + data).decode('utf-8', errors='ignore')
27
- chunk = data[-50:] # Keep last 50 bytes for boundary
28
-
29
- lines = text.split('\n')
30
- for line in lines:
31
- if query in line:
32
- # Try to parse as JSON
33
- try:
34
- results.append(json.loads(line))
35
- except:
36
- # Try to extract JSON-like data
37
- match = re.search(r'\{[^}]+\}', line)
38
- if match:
39
- try:
40
- results.append(json.loads(match.group()))
41
- except:
42
- results.append({"raw": line.strip()})
43
- else:
44
- results.append({"raw": line.strip()})
45
- if len(results) >= limit:
46
- return results
47
- except Exception as e:
48
- return [{"error": str(e)}]
49
-
50
- return results
51
-
52
  @app.get("/")
53
  async def root():
54
  return {
55
- "message": "🔍 Hitek Binary Search API",
56
  "developer": "Gopal Parmar",
57
  "endpoints": {
58
- "/search?q=9999999998": "Search by phone number",
59
- "/debug": "Check file"
60
  }
61
  }
62
 
63
- @app.get("/debug")
64
- async def debug():
65
- filepath = os.path.join(MOUNT_PATH, TEST_FILE)
66
- if not os.path.exists(filepath):
67
- return {"error": f"File {TEST_FILE} not found"}
68
-
69
- # Read first 1KB as sample
70
- with open(filepath, 'rb') as f:
71
- sample = f.read(1024)
72
-
73
- # Try to find any readable text
74
- text = sample.decode('utf-8', errors='ignore')
75
- readable = ''.join(c if 32 <= ord(c) < 127 else ' ' for c in text)
76
-
77
- return {
78
- "developer": "Gopal Parmar",
79
- "file": TEST_FILE,
80
- "size_mb": round(os.path.getsize(filepath) / (1024**2), 2),
81
- "sample_text": readable[:500],
82
- "contains_json": "{" in readable or "}" in readable
83
- }
84
-
85
- @app.get("/search")
86
- async def search(q: str = Query(..., min_length=4), limit: int = 10):
87
  filepath = os.path.join(MOUNT_PATH, TEST_FILE)
88
  if not os.path.exists(filepath):
89
  return {"error": f"File {TEST_FILE} not found"}
90
 
91
- results = search_in_binary(filepath, q, limit)
92
-
93
- return {
94
- "developer": "Gopal Parmar",
95
- "query": q,
96
- "count": len(results),
97
- "results": results
98
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
  import json
3
+ import tempfile
4
+ import subprocess
5
+ from fastapi import FastAPI
6
 
7
+ app = FastAPI(title="Hitek JSON Preview API", developer="Gopal Parmar")
8
 
9
  MOUNT_PATH = "/data/"
10
  TEST_FILE = "Hitek01.7z"
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  @app.get("/")
13
  async def root():
14
  return {
15
+ "message": "🔍 Hitek JSON Preview API",
16
  "developer": "Gopal Parmar",
17
  "endpoints": {
18
+ "/preview": "Show first 10-15 lines of users_data.json"
 
19
  }
20
  }
21
 
22
+ @app.get("/preview")
23
+ async def preview():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  filepath = os.path.join(MOUNT_PATH, TEST_FILE)
25
  if not os.path.exists(filepath):
26
  return {"error": f"File {TEST_FILE} not found"}
27
 
28
+ try:
29
+ with tempfile.TemporaryDirectory() as tmp_dir:
30
+ # Extract
31
+ subprocess.run(
32
+ ["7z", "x", filepath, f"-o{tmp_dir}", "-y"],
33
+ capture_output=True,
34
+ timeout=120
35
+ )
36
+
37
+ # Find and read JSON file
38
+ for root, dirs, files in os.walk(tmp_dir):
39
+ for file in files:
40
+ if file.endswith('.json'):
41
+ file_path = os.path.join(root, file)
42
+ lines = []
43
+ with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
44
+ for i, line in enumerate(f):
45
+ if i >= 15:
46
+ break
47
+ try:
48
+ lines.append(json.loads(line))
49
+ except:
50
+ lines.append({"raw": line.strip()})
51
+
52
+ return {
53
+ "developer": "Gopal Parmar",
54
+ "file_name": file,
55
+ "total_lines_previewed": len(lines),
56
+ "sample": lines
57
+ }
58
+
59
+ return {"error": "No JSON file found"}
60
+ except Exception as e:
61
+ return {"error": str(e)}