Spaces:
Sleeping
Sleeping
Upload main.py
Browse files42 unmodified lines4344454646474849505152531230 unmodified lines128412851286128312871288128912901291129212931290129412951292129312941295129612961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134342 unmodified lines# New spreadsheet for Teams and Servers data
TEAMS_SERVERS_SPREADSHEET_ID = os.getenv('TEAMS_SERVERS_SPREADSHEET_ID', '1HWnvzPd4AtRdAroXnwQaN4Dcpj6lZbtnCBavqiAxokQ')
SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
# Scopes for read and write access to spreadsheets
SCOPES = [
'https://www.googleapis.com/auth/spreadsheets.readonly',
'https://www.googleapis.com/auth/spreadsheets' # Added write access
]
# Sheet Names and Ranges within SCAMMER_DWC_SPREADSHEET_ID
USER_SCAMMER_SHEET = "User Scammer Files"1230 unmodified lines
@app.post("/api/teams/like")
async def like_team_member(data: TeamMemberLike):
"""Increment like count for a team member (to be implemented with spreadsheet update)"""
"""Increment like count for a team member and update spreadsheet"""
check_cache_readiness()
# Find the team member by discord_user_id
discord_user_id = data.discord_user_id.strip()
teams = cache.get("teams", [])
for member in teams:
for index, member in enumerate(teams):
if member.get("discord_user_id") == discord_user_id:
# In a real implementation, this would update the spreadsheet
# For now, just update the cache
member["likes"] = member.get("likes", 0) + 1
member["last_updated"] = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S")
return {"success": True, "likes": member["likes"]}
# Increment the like count
current_likes = member.get("likes", 0)
new_likes = current_likes + 1
member["likes"] = new_likes
# Update the spreadsheet
try:
# We need to use sheets.spreadsheets().values().update()
# First, find the row index in the sheet for this member
if sheets_service:
# Get all values to find the correct row
result = sheets_service.spreadsheets().values().get(
spreadsheetId=TEAMS_SERVERS_SPREADSHEET_ID,
range=f"{quote_sheet_name(TEAMS_SHEET)}!B:C" # Get discord usernames and IDs
).execute()
values = result.get('values', [])
row_index = None
# Find the row with matching discord_user_id (column C in the sheet)
for i, row in enumerate(values):
if len(row) > 1 and row[1] == discord_user_id:
# Add 1 for header row and +5 since we start from row 6
row_index = i + 6
break
if row_index:
# Update likes (column J)
sheets_service.spreadsheets().values().update(
spreadsheetId=TEAMS_SERVERS_SPREADSHEET_ID,
range=f"{quote_sheet_name(TEAMS_SHEET)}!J{row_index}",
valueInputOption="RAW",
body={"values": [[new_likes]]}
).execute()
logger.info(f"Updated likes for user {discord_user_id} to {new_likes} in spreadsheet")
else:
logger.error(f"Could not find row for user {discord_user_id} in spreadsheet")
else:
logger.error("Google Sheets service not available for updating likes")
except Exception as e:
logger.error(f"Error updating likes in spreadsheet: {e}")
# Continue with returning updated likes even if spreadsheet update fails
return {"success": True, "likes": new_likes}
raise HTTPException(status_code=404, detail=f"Team member with discord_user_id {discord_user_id} not found")
main.py+52-8