Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- src/utils/__init__.py +1 -0
- src/utils/database.py +78 -0
- src/utils/location.py +18 -0
src/utils/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
"""Utility functions for BharatPlay application."""
|
src/utils/database.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sqlite3
|
| 2 |
+
from datetime import datetime
|
| 3 |
+
|
| 4 |
+
def get_db_connection():
|
| 5 |
+
return sqlite3.connect('bharat_play.db')
|
| 6 |
+
|
| 7 |
+
def save_submission(submission_data):
|
| 8 |
+
conn = get_db_connection()
|
| 9 |
+
c = conn.cursor()
|
| 10 |
+
try:
|
| 11 |
+
c.execute('''
|
| 12 |
+
INSERT INTO submissions
|
| 13 |
+
(name, title, description, category, player_count, age_group,
|
| 14 |
+
equipment_needed, input_type, audio_url, image_url, video_url,
|
| 15 |
+
lat, lon, timestamp)
|
| 16 |
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
| 17 |
+
''', (
|
| 18 |
+
submission_data['name'],
|
| 19 |
+
submission_data['title'],
|
| 20 |
+
submission_data['description'],
|
| 21 |
+
submission_data['category'],
|
| 22 |
+
submission_data['player_count'],
|
| 23 |
+
submission_data['age_group'],
|
| 24 |
+
submission_data['equipment_needed'],
|
| 25 |
+
submission_data['input_type'],
|
| 26 |
+
submission_data['audio_url'],
|
| 27 |
+
submission_data['image_url'],
|
| 28 |
+
submission_data['video_url'],
|
| 29 |
+
submission_data['lat'],
|
| 30 |
+
submission_data['lon'],
|
| 31 |
+
datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
| 32 |
+
))
|
| 33 |
+
conn.commit()
|
| 34 |
+
return True
|
| 35 |
+
except Exception as e:
|
| 36 |
+
print(f"Error saving submission: {e}")
|
| 37 |
+
return False
|
| 38 |
+
finally:
|
| 39 |
+
conn.close()
|
| 40 |
+
|
| 41 |
+
def get_user_submissions(username):
|
| 42 |
+
conn = get_db_connection()
|
| 43 |
+
c = conn.cursor()
|
| 44 |
+
try:
|
| 45 |
+
# First check if the table exists
|
| 46 |
+
c.execute('''SELECT name FROM sqlite_master
|
| 47 |
+
WHERE type='table' AND name='submissions';''')
|
| 48 |
+
if not c.fetchone():
|
| 49 |
+
print("Submissions table does not exist")
|
| 50 |
+
return []
|
| 51 |
+
|
| 52 |
+
# If table exists, get the submissions
|
| 53 |
+
c.execute('''SELECT * FROM submissions
|
| 54 |
+
WHERE name = ?
|
| 55 |
+
ORDER BY timestamp DESC''', (username,))
|
| 56 |
+
columns = [description[0] for description in c.description]
|
| 57 |
+
results = []
|
| 58 |
+
for row in c.fetchall():
|
| 59 |
+
results.append(dict(zip(columns, row)))
|
| 60 |
+
return results
|
| 61 |
+
except Exception as e:
|
| 62 |
+
print(f"Error getting submissions: {e}")
|
| 63 |
+
return []
|
| 64 |
+
finally:
|
| 65 |
+
conn.close()
|
| 66 |
+
|
| 67 |
+
def get_all_submissions():
|
| 68 |
+
conn = get_db_connection()
|
| 69 |
+
c = conn.cursor()
|
| 70 |
+
try:
|
| 71 |
+
c.execute('SELECT * FROM submissions ORDER BY timestamp DESC')
|
| 72 |
+
columns = [description[0] for description in c.description]
|
| 73 |
+
results = []
|
| 74 |
+
for row in c.fetchall():
|
| 75 |
+
results.append(dict(zip(columns, row)))
|
| 76 |
+
return results
|
| 77 |
+
finally:
|
| 78 |
+
conn.close()
|
src/utils/location.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Location related functionality for BharatPlay."""
|
| 2 |
+
import requests
|
| 3 |
+
import urllib3
|
| 4 |
+
|
| 5 |
+
def get_ip_location():
|
| 6 |
+
"""Get user's location from IP address using ipinfo.io."""
|
| 7 |
+
try:
|
| 8 |
+
# Disable SSL warning
|
| 9 |
+
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
| 10 |
+
response = requests.get("https://ipinfo.io/json", verify=False)
|
| 11 |
+
data = response.json()
|
| 12 |
+
loc = data.get("loc")
|
| 13 |
+
if loc:
|
| 14 |
+
lat_str, lon_str = loc.split(",")
|
| 15 |
+
return float(lat_str), float(lon_str)
|
| 16 |
+
except Exception as e:
|
| 17 |
+
print(f"Error getting location: {e}")
|
| 18 |
+
return None, None
|