import datetime def calculate_score(row): current_time = datetime.datetime.now() delta = current_time - row['Horodateur'] base_score = delta.total_seconds() / 60 text_score = get_text_score(row) temp_score = get_score_temp(row) return base_score + text_score + temp_score def get_temp(lat, lon): url = f'https://api.openweathermap.org/data/2.5/forecast?lat={lat}&lon={lon}&appid={API_KEY}' response = requests.get(url) if response.status_code == 200: data = response.json() temp = sum([single_point['main']['temp_min'] for single_point in data['list']])/40 else: print(f'Error: Unable to fetch weather data. Status code: {response.status_code}') return temp NEED_COL = 'ما هي احتياجاتك؟ (أضفها إذا لم يتم ذكرها)' COOR_COL = 'هل يمكنك تقديم الإحداثيات الدقيقة للموقع؟ (ادا كنت لا توجد بعين المكان) متلاً \n31.01837503440344, -6.781405948842175' def get_text_score(row): score = 0 need = row[NEED_COL] needs = need.split(' ') if 'وماء' in needs:#water score += 500 if 'طعام' in needs:#food score += 500 if 'مساعدة طبية' in needs: #medical score += 1000 if 'إغاثة' in needs:#secours score+=800 if 'لنقود' in needs: #secours score += 800 if 'الخيام' in needs: #tent score += 500 if 'ولملابس' in needs:#clothes score += 250 if 'الأغطية' in needs: #covers score += 250 if 'أفرشة' in needs: #matress score+=100 return score def get_score_temp(row): score = 0 need = row[NEED_COL] needs = need.split(' ') # tent, clothes or cover if ('الخيام' not in needs) and ('ولملابس' not in needs) and ('الأغطية' not in needs): return score lat, lon = row[COOR_COL].split(',') lon = lon.strip() lat = lat.strip() average_temp = get_temp(lat, lon) if average_temp < 283: score += 1000 if average_temp < 273: score += 1000 return score def sort_request(requests): current_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') requests['Horodateur'].fillna(current_time, inplace=True) scores = [] for index, row in requests.iterrows(): scores.append(calculate_score(row)) requests['score'] = scores requests = requests.sort_values(by='score', ascending=False) return requests