Passed
Branch BonHowi (242417)
by Bartosz
01:38
created

build.modules.pull_config.pull_config.main()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
import pandas as pd
2
import os.path
3
from googleapiclient.discovery import build
4
from google_auth_oauthlib.flow import InstalledAppFlow
5
from google.auth.transport.requests import Request
6
from google.oauth2.credentials import Credentials
7
import json
8
from modules import get_settings
9
10
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
11
SAMPLE_RANGE_NAME = 'A1:AA68'
12
CREDENTIALS_FILE = 'pull_config/credentials/client_secret.com.json '
13
14
SAMPLE_SPREADSHEET_ID_input = get_settings.get_settings("EXCEL_ID")
15
16
17
def import_from_sheets():
18
    """
19
20
    :return:
21
    :rtype:
22
    """
23
    creds = None
24
    # The file token.json stores the user's access and refresh tokens, and is
25
    # created automatically when the authorization flow completes for the first time
26
    if os.path.exists('token.json'):
27
        creds = Credentials.from_authorized_user_file('token.json', SCOPES)
28
    # If there are no (valid) credentials available, let the user log in
29
    if not creds or not creds.valid:
30
        if creds and creds.expired and creds.refresh_token:
31
            creds.refresh(Request())
32
        else:
33
            flow = InstalledAppFlow.from_client_secrets_file(
34
                CREDENTIALS_FILE, SCOPES)
35
            creds = flow.run_local_server(port=0)
36
        # Save the credentials for the next run
37
        with open('token.json', 'w') as token:
38
            token.write(creds.to_json())
39
40
    service = build('sheets', 'v4', credentials=creds)
41
42
    # Call the Sheets API
43
    sheet = service.spreadsheets()
44
    result_input = sheet.values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID_input, range=SAMPLE_RANGE_NAME).execute()
45
    values_input = result_input.get('values', [])
46
47
    if not values_input:
48
        print('No data found.')
49
    return values_input
50
51
52
def get_config():
53
    """
54
55
    :return:
56
    :rtype:
57
    """
58
    pd.set_option('mode.chained_assignment', None)
59
    print("Loading data")
60
    values_input = import_from_sheets()
61
    df = pd.DataFrame(values_input[1:], columns=values_input[0])
62
63
    print("Transforming data")
64
    monsters_df = df[["name", "type"]]
65
    monsters_df["type"] = pd.to_numeric(df["type"])
66
67
    triggers = df.drop(['name', 'role', 'type', 'id'], axis=1)
68
    triggers = triggers.applymap(lambda s: s.lower() if type(s) == str else s)
69
    # triggers = triggers.applymap(lambda s: unidecode.unidecode(s) if type(s) == str else s)
70
71
    triggers_list = []
72
    for row in triggers.itertuples(index=False):
73
        helpt = pd.Series(row)
74
        helpt = helpt[~helpt.isna()]
75
        # Drop empty strings
76
        helpt = pd.Series(filter(None, helpt))
77
        # Copy strings with spaces without keeping them
78
        for trigger in helpt:
79
            trigger_nospace = trigger.replace(' ', '')
80
            helpt = helpt.append(pd.Series(trigger_nospace))
81
        helpt = helpt.drop_duplicates()
82
        triggers_list.append(helpt)
83
84
    print("Creating trigger structure")
85
    triggers_def = []
86
    for i in triggers_list:
87
        triggers_def.append(list(i))
88
    triggers_def_series = pd.Series(triggers_def)
89
    monsters_df.insert(loc=0, column='triggers', value=triggers_def_series)
90
91
    print("Creating output")
92
    types = {'id': [4, 3, 2, 1, 0], 'label': ["Common", "Event0", "Event1", "Legendary", "Rare"]}
93
    types_df = pd.DataFrame(data=types)
94
    milestones = {'total': [150, 1000, 2000, 3000, 4000, 5000],
95
                  'name': ["Rare Spotter", "Pepega Spotter", "Pog Spotter", "Pogmare Spotter", "Legendary Spotter",
96
                           "Mythic Spotter"]}
97
    milestones_df = pd.DataFrame(data=milestones)
98
    json_final = {'milestones': milestones_df, 'types': types_df, 'commands': monsters_df}
99
100
    # convert dataframes into dictionaries
101
    data_dict = {
102
        key: json_final[key].to_dict(orient='records')
103
        for key in json_final
104
    }
105
106
    # write to disk
107
    with open('server_files/config.json', 'w', encoding='utf8') as f:
108
        json.dump(
109
            data_dict,
110
            f,
111
            indent=4,
112
            ensure_ascii=False,
113
            sort_keys=False
114
        )
115
    print(".json saved")
116
117
118
def main():
119
    get_config()
120
121
122
if __name__ == "__main__":
123
    main()
124