forked from Sturalke/FauxHollowsProbabilisticSolver
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.py
209 lines (194 loc) · 7.28 KB
/
scraper.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
from __future__ import print_function
import json
import os.path
from apiclient import discovery
from google.oauth2 import service_account
from googleapiclient.errors import HttpError
"""
Don't judge me. I don't know Python well.
"""
SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
# This is necessary. Service worker with Google Sheets API access.
SERVICE_ACCOUNT_FILE = 'service_account.json'
# Safe because it's public anyway
SAMPLE_SPREADSHEET_ID = '1mUyCzlzDmdXMwaSTUgWXtEA45oJNn-iB4_bVM43zf58'
# Definitely a better way to do this... Oh well.
SAMPLE_RANGE_NAME = ['\'A ↑ \'!3:56',
'\'A →\'!3:56',
'\'A ↓\'!3:56',
'\'A ←\'!3:56',
'\'B ↑\'!3:40',
'\'B →\'!3:40',
'\'B ↓\'!3:40',
'\'B ←\'!3:40',
'\'C ↑\'!3:47',
'\'C →\'!3:48',
'\'C ↓\'!3:48',
'\'C ←\'!3:48',
'\'D ↑\'!3:56',
'\'D →\'!3:56',
'\'D ↓\'!3:56',
'\'D ←\'!3:56']
"""
Sheet color object definitions
There's probably a better way to do this.
"""
COLOR_OOB = {
'red': 0.8509804,
'green': 0.8509804,
'blue': 0.8509804
}
COLOR_EMPTY = {
'red': 1,
'green': 1,
'blue': 1
}
COLOR_BLOCK = {
'red': 0.6,
'green': 0.6,
'blue': 0.6
}
COLOR_BOX = {
'red': 0.91764706,
'green': 0.6,
'blue': 0.6
}
COLOR_SWORD = {
'red': 0.62352943,
'green': 0.77254903,
'blue': 0.9098039
}
COLOR_SWORD_ALT = {
'red': 0.6431373,
'green': 0.7607843,
'blue': 0.95686275
}
COLOR_CONF = {
'red': 1,
'blue': 1
}
COLOR_POT = {
'red': 1,
'green': 0.6
}
"""
End color definitions
"""
def main():
try:
secret_file = os.path.join(os.getcwd(), SERVICE_ACCOUNT_FILE)
credentials = service_account.Credentials.from_service_account_file(secret_file, scopes=SCOPES)
service = discovery.build('sheets', 'v4', credentials=credentials)
# Call the Sheets API
include_grid_data = True
sheet = service.spreadsheets()
result = sheet.get(spreadsheetId=SAMPLE_SPREADSHEET_ID,
ranges=SAMPLE_RANGE_NAME,
includeGridData=include_grid_data).execute()
if not result:
print('No data found.')
return
# Manually extract background color data
color_arr = []
for sheet in result['sheets']:
color_sheet = []
for row in sheet['data'][0]['rowData']:
color_row = []
for cell in row['values']:
col = cell['effectiveFormat']['backgroundColor']
val = 0
if col == COLOR_OOB:
val = 0
elif col == COLOR_EMPTY:
val = 1
elif col == COLOR_BLOCK:
val = 2
elif col == COLOR_BOX:
val = 3
elif col == COLOR_SWORD:
val = 4
elif col == COLOR_SWORD_ALT:
val = 4
elif col == COLOR_CONF:
val = 5
elif col == COLOR_POT:
val = 6
else:
val = -1
print(col)
color_row.append(val)
color_sheet.append(color_row)
color_arr.append(color_sheet)
# This whole section is fucked.
# Not looking forward to fixing if it breaks.
final_arr = []
for c_sheet in color_arr:
orientation = []
current_boards = []
repeat_empty_row = True
current_board_index = 0
current_board_row_index = 0
for c_row in c_sheet:
current_board_index = 0
empty_row = True
current_board_row = []
current_board_row_written = False
for elem in c_row:
if not elem == 0:
empty_row = False
repeat_empty_row = False
val = -1
if elem == 1:
val = 0
elif elem == 2:
val = 1
elif elem == 3:
val = 2
elif elem == 4:
val = 3
elif elem == 5:
val = 4
elif elem == 6:
val = 5
else:
val = -1
current_board_row.append(val)
current_board_row_written = False
# If you're actually reading through this, why?
else:
if not empty_row:
if not current_board_row_written:
if len(current_boards) < current_board_index + 1:
current_boards.insert(current_board_index, [current_board_row])
else:
current_boards[current_board_index].insert(current_board_row_index,
current_board_row)
current_board_row_written = True
current_board_row = []
# This isn't worth your sanity.
current_board_index = current_board_index + 1
if empty_row:
if not repeat_empty_row:
for board in current_boards:
orientation.append(board)
current_boards = []
repeat_empty_row = True
current_board_row_index = 0
else:
current_board_row_index = current_board_row_index + 1
if len(current_boards) > 0:
for board in current_boards:
orientation.append(board)
final_arr.append(orientation)
with open("data.js", "w") as f:
f.write("/*\n\tScraped and parsed automatically from\n\t"
"https://docs.google.com/spreadsheets/d/1mUyCzlzDmdXMwaSTUgWXtEA45oJNn-iB4_bVM43zf58"
"\n\tCredit to u/Ylandah on r/FFXIV for creating and maintaining the spreadsheet.\n\t"
"Thanks to them and all contributors for collating this information.\n\t"
"Scraped using Google Sheets API and parsed using Python.\n*/\n")
f.write("const fhs_sheet_fox = ")
json.dump(final_arr, f, separators=(",", ":"))
except HttpError as err:
print(err)
if __name__ == '__main__':
main()