parent
400885627b
commit
24deb2198d
@ -1,34 +1,107 @@ |
||||
import csv |
||||
import os |
||||
import re |
||||
from datetime import datetime |
||||
|
||||
from tournaments.models.enums import FederalCategory |
||||
|
||||
def clean_licence_id(licence_id): |
||||
# This regex matches the trailing letters (non-digits) and removes them |
||||
cleaned_licence_id = re.sub(r'\D+$', '', str(licence_id)) # \D+ matches non-digits at the end |
||||
return cleaned_licence_id |
||||
|
||||
def get_player_name_from_csv(licence_id): |
||||
# Define the file path |
||||
file_path = '/Users/razmig/Documents/XLR Sport/padelclub_backend/tournaments/static/rankings/CLASSEMENT-PADEL-MESSIEURS-11-2024.csv' |
||||
def get_player_name_from_csv(category, licence_id, base_folder="/Users/razmig/Documents/XLR Sport/padelclub_backend/tournaments/static"): |
||||
""" |
||||
Search for a player's first name, last name, and rank in the most recent rankings file. |
||||
|
||||
:param licence_id: The licence ID to search for. |
||||
:param base_folder: Base folder containing the rankings folder. |
||||
:return: A tuple (first_name, last_name, rank) or (None, None, None) if not found. |
||||
""" |
||||
folder_path = os.path.join(base_folder, "rankings") |
||||
cleaned_licence_id = clean_licence_id(licence_id) |
||||
print(cleaned_licence_id) |
||||
# Open the file and read it |
||||
|
||||
def extract_date(file_name): |
||||
""" |
||||
Extract the date (MM-YYYY) from the file name and return it as a datetime object. |
||||
""" |
||||
match = re.search(r"(\d{2}-\d{4})", file_name) |
||||
if match: |
||||
return datetime.strptime(match.group(1), "%m-%Y") |
||||
return None |
||||
|
||||
def find_most_recent_file(file_type): |
||||
""" |
||||
Find the most recent file for the given type (-MESSIEURS- or -DAMES-). |
||||
""" |
||||
files = [ |
||||
f for f in os.listdir(folder_path) |
||||
if file_type in f and re.search(r"\d{2}-\d{4}", f) |
||||
] |
||||
files_with_dates = [(f, extract_date(f) or datetime.min) for f in files] |
||||
if not files_with_dates: |
||||
return None |
||||
# Sort by date in descending order |
||||
files_with_dates.sort(key=lambda x: x[1], reverse=True) |
||||
return os.path.join(folder_path, files_with_dates[0][0]) |
||||
|
||||
def search_file(file_path, is_woman): |
||||
if not file_path or not os.path.exists(file_path): |
||||
return None, False |
||||
|
||||
last_rank = None |
||||
last_rank_count = 0 |
||||
with open(file_path, newline='', encoding='utf-8') as file: |
||||
reader = csv.reader(file, delimiter=';') |
||||
rows = list(reader) # Read all rows at once to process later |
||||
|
||||
# Iterate through each row in the CSV |
||||
for row in reader: |
||||
# Ensure the row is not empty and has the expected number of columns |
||||
if len(row) >= 13: |
||||
current_licence_id = row[5] # The 5th column contains the licence_id |
||||
|
||||
# Check if the current row matches the given licence_id |
||||
for row in rows: |
||||
if len(row) >= 15: # Ensure row has enough columns |
||||
current_licence_id = row[5] |
||||
if current_licence_id == str(cleaned_licence_id): |
||||
# Return first name and last name from the row (3rd and 4th columns) |
||||
first_name = row[3] # 4th column: first name |
||||
last_name = row[2] # 3rd column: last name |
||||
rank = row[1] # 3rd column: last name |
||||
return first_name, last_name, rank |
||||
|
||||
# Return None if no match is found |
||||
return None, None, None |
||||
data = { |
||||
"first_name": row[3], # 4th column: first name |
||||
"last_name": row[2], # 3rd column: last name |
||||
"rank": row[1], |
||||
"points": row[6], |
||||
"assimilation": row[7], |
||||
"tournament_count": row[8], |
||||
"ligue_name": row[9], |
||||
"club_name": row[11], |
||||
"birth_year": row[14], |
||||
"is_woman": is_woman, |
||||
} |
||||
return data, True |
||||
|
||||
# Determine the rank for an unranked player |
||||
if rows: |
||||
# Find the last rank in the file |
||||
last_row = rows[-1] |
||||
if len(last_row) >= 2: |
||||
last_rank = last_row[1] |
||||
|
||||
# Count how many times the last rank appears |
||||
last_rank_count = sum(1 for row in rows if len(row) >= 2 and row[1] == last_rank) |
||||
|
||||
if last_rank is not None: |
||||
unranked_rank = int(last_rank) + last_rank_count + 1 |
||||
data = { |
||||
"rank": str(unranked_rank), |
||||
"is_woman": is_woman |
||||
} |
||||
return data, False |
||||
|
||||
return None, False |
||||
|
||||
|
||||
dames_file = find_most_recent_file("CLASSEMENT-PADEL-DAMES-") |
||||
result, found = search_file(dames_file, True) |
||||
if found or category is FederalCategory.WOMEN: |
||||
return result |
||||
|
||||
messieurs_file = find_most_recent_file("CLASSEMENT-PADEL-MESSIEURS-") |
||||
result, found = search_file(messieurs_file, False) |
||||
return result |
||||
|
||||
# Return None if not found in either file |
||||
return None |
||||
|
||||
Loading…
Reference in new issue