fdsgfuisfydgiuef commited on
Commit
28311a1
·
verified ·
1 Parent(s): 4011546

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -34
app.py CHANGED
@@ -1,62 +1,68 @@
1
  import os
2
- import sqlite3
 
3
  from fastapi import FastAPI
4
  from huggingface_hub import hf_hub_download
5
 
6
  app = FastAPI(title="Telegram API")
7
 
8
- DB_FILE = None
9
 
10
  def get_db():
11
- global DB_FILE
12
 
13
- if DB_FILE and os.path.exists(DB_FILE):
14
- return DB_FILE
 
 
 
 
 
15
 
16
- DB_FILE = hf_hub_download(
17
- repo_id="fdsgfuisfydgiuef/Telegram",
18
- repo_type="dataset",
19
- filename="Midnight_Master_v2.db",
20
- token=os.getenv("HF_TOKEN")
21
- )
22
-
23
- return DB_FILE
24
 
25
 
26
  @app.get("/")
27
  def home():
28
  return {
29
  "status": True,
30
- "message": "API Running"
31
  }
32
 
33
 
34
- @app.get("/tables")
35
- def tables():
36
- db = get_db()
37
-
38
- conn = sqlite3.connect(db)
39
- cur = conn.cursor()
40
-
41
- cur.execute("SELECT name FROM sqlite_master WHERE type='table'")
42
- tables = [x[0] for x in cur.fetchall()]
43
 
44
- conn.close()
45
 
46
- return {"tables": tables}
47
 
 
 
48
 
49
- @app.get("/columns/{table}")
50
- def columns(table: str):
51
- db = get_db()
 
 
 
 
52
 
53
- conn = sqlite3.connect(db)
54
- cur = conn.cursor()
 
 
 
55
 
56
- cur.execute(f"PRAGMA table_info({table})")
 
 
 
 
57
 
58
- cols = cur.fetchall()
 
59
 
60
- conn.close()
61
 
62
- return cols
 
 
1
  import os
2
+ import duckdb
3
+ import uvicorn
4
  from fastapi import FastAPI
5
  from huggingface_hub import hf_hub_download
6
 
7
  app = FastAPI(title="Telegram API")
8
 
9
+ DB_PATH = None
10
 
11
  def get_db():
12
+ global DB_PATH
13
 
14
+ if DB_PATH is None:
15
+ DB_PATH = hf_hub_download(
16
+ repo_id="fdsgfuisfydgiuef/Telegram",
17
+ repo_type="dataset",
18
+ filename="Midnight_Master_v2.db",
19
+ token=os.getenv("HF_TOKEN")
20
+ )
21
 
22
+ return DB_PATH
 
 
 
 
 
 
 
23
 
24
 
25
  @app.get("/")
26
  def home():
27
  return {
28
  "status": True,
29
+ "message": "Telegram API Running"
30
  }
31
 
32
 
33
+ @app.get("/search")
34
+ def search(q: str):
 
 
 
 
 
 
 
35
 
36
+ db = get_db()
37
 
38
+ con = duckdb.connect()
39
 
40
+ con.execute("INSTALL sqlite;")
41
+ con.execute("LOAD sqlite;")
42
 
43
+ try:
44
+ result = con.execute(f"""
45
+ SELECT *
46
+ FROM sqlite_scan('{db}','telegram_users')
47
+ WHERE user_id LIKE '{q}%'
48
+ LIMIT 20
49
+ """).fetchall()
50
 
51
+ return {
52
+ "status": True,
53
+ "count": len(result),
54
+ "results": result
55
+ }
56
 
57
+ except Exception as e:
58
+ return {
59
+ "status": False,
60
+ "error": str(e)
61
+ }
62
 
63
+ finally:
64
+ con.close()
65
 
 
66
 
67
+ if __name__ == "__main__":
68
+ uvicorn.run(app, host="0.0.0.0", port=7860)