File size: 2,519 Bytes
2067d6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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