gopallikehack commited on
Commit
0bd4c41
·
verified ·
1 Parent(s): 3df280f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -86
app.py CHANGED
@@ -1,98 +1,77 @@
1
  import os
2
- import subprocess
3
  import json
4
- import zipfile
5
- from fastapi import FastAPI, Query
6
 
7
- app = FastAPI(title="Indian Database Search API", developer="Gopal Parmar")
8
 
9
- FILE_PATH = "/data/HitekAll.zip"
10
- EXTRACTED_JSON = "/data/users_data.json"
 
11
 
12
- @app.on_event("startup")
13
- async def startup():
14
- if os.path.exists(FILE_PATH):
15
- print(f"✅ Found: {FILE_PATH}")
16
- if not os.path.exists(EXTRACTED_JSON):
17
- print("⏳ Extracting JSON using 7z...")
18
- result = subprocess.run(
19
- ["7z", "x", FILE_PATH, "-o/data/", "-y"],
20
- capture_output=True,
21
- text=True
22
- )
23
- if result.returncode == 0:
24
- print("✅ Extraction complete")
25
- else:
26
- print(f"❌ Extraction failed: {result.stderr}")
27
- else:
28
- print("❌ File not found")
29
-
30
- @app.get("/")
31
- async def root():
32
- exists = os.path.exists(EXTRACTED_JSON)
33
- return {
34
- "message": "🔍 Indian Database Search API",
35
- "developer": "Gopal Parmar",
36
- "status": "ready" if exists else "processing",
37
- "file_size_gb": round(os.path.getsize(EXTRACTED_JSON)/(1024**3), 2) if exists else 0
38
- }
39
 
40
- @app.get("/filetype")
41
- async def filetype():
42
- if not os.path.exists(FILE_PATH):
43
- return {"error": "File not found"}
44
 
45
- with open(FILE_PATH, 'rb') as f:
46
- header = f.read(20)
47
 
48
- if header.startswith(b'PK'):
49
- file_type = "ZIP"
50
- elif header.startswith(b'Rar!'):
51
- file_type = "RAR"
52
- elif header.startswith(b'7z'):
53
- file_type = "7ZIP"
54
- else:
55
- file_type = "UNKNOWN"
56
 
57
- return {
58
- "file": "HitekAll.zip",
59
- "detected_type": file_type,
60
- "magic_bytes": header.hex()
61
- }
62
-
63
- @app.get("/testzip")
64
- async def testzip():
65
- if not os.path.exists(FILE_PATH):
66
- return {"error": "File not found"}
67
 
68
- try:
69
- with zipfile.ZipFile(FILE_PATH, 'r') as zf:
70
- return {"files": zf.namelist()}
71
- except Exception as e:
72
- return {"error": str(e)}
73
-
74
- @app.get("/search")
75
- async def search(q: str = Query(..., min_length=4), limit: int = 10):
76
- if not os.path.exists(EXTRACTED_JSON):
77
- return {"error": "JSON not extracted yet. Check /filetype or /testzip"}
 
 
 
 
 
78
 
79
- results = []
80
- try:
81
- with open(EXTRACTED_JSON, 'r', encoding='utf-8', errors='ignore') as f:
82
- for line in f:
83
- if q in line:
84
- try:
85
- results.append(json.loads(line))
86
- except:
87
- results.append({"raw": line.strip()})
88
- if len(results) >= limit:
89
- break
90
- except Exception as e:
91
- return {"error": str(e)}
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
+ from flask import Flask, render_template, send_from_directory, send_file, abort
4
+ import mimetypes
5
 
6
+ app = Flask(__name__)
7
 
8
+ # Configuration
9
+ DATA_DIR = "/data"
10
+ VIDEO_FILE = "Free Python Course Hindi.mp4" # Change this to your actual filename
11
 
12
+ @app.route('/')
13
+ def index():
14
+ # Check if video exists
15
+ video_path = os.path.join(DATA_DIR, VIDEO_FILE)
16
+ video_exists = os.path.exists(video_path)
17
+
18
+ return render_template('index.html',
19
+ video_exists=video_exists,
20
+ video_title="Free Python Course Hindi",
21
+ channel="@GpsirEra")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
+ @app.route('/video')
24
+ def stream_video():
25
+ video_path = os.path.join(DATA_DIR, VIDEO_FILE)
 
26
 
27
+ if not os.path.exists(video_path):
28
+ abort(404)
29
 
30
+ # Get file size
31
+ file_size = os.path.getsize(video_path)
 
 
 
 
 
 
32
 
33
+ # Get range header for partial content
34
+ range_header = request.headers.get('Range', None)
 
 
 
 
 
 
 
 
35
 
36
+ if not range_header:
37
+ # Serve full file
38
+ return send_file(video_path,
39
+ mimetype='video/mp4',
40
+ as_attachment=False)
41
+
42
+ # Parse range
43
+ byte1, byte2 = 0, None
44
+ match = re.search(r'(\d+)-(\d*)', range_header)
45
+ groups = match.groups()
46
+
47
+ if groups[0]:
48
+ byte1 = int(groups[0])
49
+ if groups[1]:
50
+ byte2 = int(groups[1])
51
 
52
+ if byte2 is None:
53
+ byte2 = file_size - 1
 
 
 
 
 
 
 
 
 
 
 
54
 
55
+ length = byte2 - byte1 + 1
56
+
57
+ # Read partial content
58
+ with open(video_path, 'rb') as f:
59
+ f.seek(byte1)
60
+ data = f.read(length)
61
+
62
+ response = Response(data, 206, mimetype='video/mp4',
63
+ content_type='video/mp4',
64
+ direct_passthrough=True)
65
+ response.headers.add('Content-Range', f'bytes {byte1}-{byte2}/{file_size}')
66
+ response.headers.add('Accept-Ranges', 'bytes')
67
+ response.headers.add('Content-Length', str(length))
68
+
69
+ return response
70
+
71
+ @app.route('/poster')
72
+ def get_poster():
73
+ # Use online Python logo/poster
74
+ return redirect('https://www.python.org/static/img/python-logo.png')
75
+
76
+ if __name__ == '__main__':
77
+ app.run(host='0.0.0.0', port=7860)