-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocal_save.py
76 lines (61 loc) · 2.28 KB
/
local_save.py
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
import csv
import functions as func
import os
local_save = 'rank_competidores.csv'
exist = os.path.isfile(local_save)
cabecalho = ["Nome", "Avatar", "Display Name", "Bandeira", "Idioma", "XP", "STREAK"]
def adicionar_na_lista(user, lang):
data = func.collect_user_data(user)
if data == None:
return None
if lang == None:
return ''
display_name = data['fullname']
avatar_url = 'https:' + data['avatar'] + '/xlarge'
xp = '0'
streak = data['site_streak']
icon_language = func.get_bandeira(lang)
for i in data['languages']:
if i['learning'] == False:
continue
if i['language_string'] == lang:
xp = i['points']
break
already_in_list = False
with open(local_save, 'r') as arquivo:
csv_writer = csv.reader(arquivo, delimiter=';')
for line in csv_writer:
if line[0] == user:
already_in_list = True
break
continue
if not already_in_list:
with open('rank_competidores.csv', 'a', newline='') as arquivo:
csv_writer = csv.writer(arquivo, delimiter=';')
if not exist:
csv_writer.writerow(cabecalho)
csv_writer.writerow([user, avatar_url, display_name, icon_language, lang, xp, streak])
return True
return False
def pegar_competidores():
lista_competidores = []
with open('rank_competidores.csv', 'r') as arquivo:
csv_writer = csv.reader(arquivo, delimiter=';')
for item in csv_writer:
if item[0] == "Nome":
continue
lista_competidores.append(item)
return lista_competidores
def atualizar_rank():
competitors = pegar_competidores()
for competitor in competitors:
data = func.collect_user_data(competitor[0])
for i in data['languages']:
if i['language_string'] == competitor[4]:
competitor[5] = int(i['points'])
competitor[6] = int(data['last_streak']['length'])
with open('rank_competidores.csv', 'w', newline='') as arquivo:
csv_writer = csv.writer(arquivo, delimiter=';')
csv_writer.writerow(cabecalho)
for competitor in competitors:
csv_writer.writerow(competitor)