Spaces:
Running
Running
Upload main.py
Browse files
main.py
CHANGED
|
@@ -43,7 +43,11 @@ VALUES_DUPE_SPREADSHEET_ID = os.getenv('VALUES_DUPE_SPREADSHEET_ID', '1Toe07o3P5
|
|
| 43 |
# New spreadsheet for Teams and Servers data
|
| 44 |
TEAMS_SERVERS_SPREADSHEET_ID = os.getenv('TEAMS_SERVERS_SPREADSHEET_ID', '1HWnvzPd4AtRdAroXnwQaN4Dcpj6lZbtnCBavqiAxokQ')
|
| 45 |
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
# Sheet Names and Ranges within SCAMMER_DWC_SPREADSHEET_ID
|
| 49 |
USER_SCAMMER_SHEET = "User Scammer Files"
|
|
@@ -1280,20 +1284,60 @@ class TeamMemberLike(BaseModel):
|
|
| 1280 |
|
| 1281 |
@app.post("/api/teams/like")
|
| 1282 |
async def like_team_member(data: TeamMemberLike):
|
| 1283 |
-
"""Increment like count for a team member
|
| 1284 |
check_cache_readiness()
|
| 1285 |
|
| 1286 |
# Find the team member by discord_user_id
|
| 1287 |
discord_user_id = data.discord_user_id.strip()
|
| 1288 |
teams = cache.get("teams", [])
|
| 1289 |
|
| 1290 |
-
for member in teams:
|
| 1291 |
if member.get("discord_user_id") == discord_user_id:
|
| 1292 |
-
#
|
| 1293 |
-
|
| 1294 |
-
|
| 1295 |
-
member["
|
| 1296 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1297 |
|
| 1298 |
raise HTTPException(status_code=404, detail=f"Team member with discord_user_id {discord_user_id} not found")
|
| 1299 |
|
|
|
|
| 43 |
# New spreadsheet for Teams and Servers data
|
| 44 |
TEAMS_SERVERS_SPREADSHEET_ID = os.getenv('TEAMS_SERVERS_SPREADSHEET_ID', '1HWnvzPd4AtRdAroXnwQaN4Dcpj6lZbtnCBavqiAxokQ')
|
| 45 |
|
| 46 |
+
# Scopes for read and write access to spreadsheets
|
| 47 |
+
SCOPES = [
|
| 48 |
+
'https://www.googleapis.com/auth/spreadsheets.readonly',
|
| 49 |
+
'https://www.googleapis.com/auth/spreadsheets' # Added write access
|
| 50 |
+
]
|
| 51 |
|
| 52 |
# Sheet Names and Ranges within SCAMMER_DWC_SPREADSHEET_ID
|
| 53 |
USER_SCAMMER_SHEET = "User Scammer Files"
|
|
|
|
| 1284 |
|
| 1285 |
@app.post("/api/teams/like")
|
| 1286 |
async def like_team_member(data: TeamMemberLike):
|
| 1287 |
+
"""Increment like count for a team member and update spreadsheet"""
|
| 1288 |
check_cache_readiness()
|
| 1289 |
|
| 1290 |
# Find the team member by discord_user_id
|
| 1291 |
discord_user_id = data.discord_user_id.strip()
|
| 1292 |
teams = cache.get("teams", [])
|
| 1293 |
|
| 1294 |
+
for index, member in enumerate(teams):
|
| 1295 |
if member.get("discord_user_id") == discord_user_id:
|
| 1296 |
+
# Increment the like count
|
| 1297 |
+
current_likes = member.get("likes", 0)
|
| 1298 |
+
new_likes = current_likes + 1
|
| 1299 |
+
member["likes"] = new_likes
|
| 1300 |
+
|
| 1301 |
+
# Update the spreadsheet
|
| 1302 |
+
try:
|
| 1303 |
+
# We need to use sheets.spreadsheets().values().update()
|
| 1304 |
+
# First, find the row index in the sheet for this member
|
| 1305 |
+
if sheets_service:
|
| 1306 |
+
# Get all values to find the correct row
|
| 1307 |
+
result = sheets_service.spreadsheets().values().get(
|
| 1308 |
+
spreadsheetId=TEAMS_SERVERS_SPREADSHEET_ID,
|
| 1309 |
+
range=f"{quote_sheet_name(TEAMS_SHEET)}!B:C" # Get discord usernames and IDs
|
| 1310 |
+
).execute()
|
| 1311 |
+
|
| 1312 |
+
values = result.get('values', [])
|
| 1313 |
+
row_index = None
|
| 1314 |
+
|
| 1315 |
+
# Find the row with matching discord_user_id (column C in the sheet)
|
| 1316 |
+
for i, row in enumerate(values):
|
| 1317 |
+
if len(row) > 1 and row[1] == discord_user_id:
|
| 1318 |
+
# Add 1 for header row and +5 since we start from row 6
|
| 1319 |
+
row_index = i + 6
|
| 1320 |
+
break
|
| 1321 |
+
|
| 1322 |
+
if row_index:
|
| 1323 |
+
# Update likes (column J)
|
| 1324 |
+
sheets_service.spreadsheets().values().update(
|
| 1325 |
+
spreadsheetId=TEAMS_SERVERS_SPREADSHEET_ID,
|
| 1326 |
+
range=f"{quote_sheet_name(TEAMS_SHEET)}!J{row_index}",
|
| 1327 |
+
valueInputOption="RAW",
|
| 1328 |
+
body={"values": [[new_likes]]}
|
| 1329 |
+
).execute()
|
| 1330 |
+
|
| 1331 |
+
logger.info(f"Updated likes for user {discord_user_id} to {new_likes} in spreadsheet")
|
| 1332 |
+
else:
|
| 1333 |
+
logger.error(f"Could not find row for user {discord_user_id} in spreadsheet")
|
| 1334 |
+
else:
|
| 1335 |
+
logger.error("Google Sheets service not available for updating likes")
|
| 1336 |
+
except Exception as e:
|
| 1337 |
+
logger.error(f"Error updating likes in spreadsheet: {e}")
|
| 1338 |
+
# Continue with returning updated likes even if spreadsheet update fails
|
| 1339 |
+
|
| 1340 |
+
return {"success": True, "likes": new_likes}
|
| 1341 |
|
| 1342 |
raise HTTPException(status_code=404, detail=f"Team member with discord_user_id {discord_user_id} not found")
|
| 1343 |
|