-
Notifications
You must be signed in to change notification settings - Fork 0
/
modules.py
432 lines (335 loc) · 14.5 KB
/
modules.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import requests
import time
import csv
import os
import re
import tkinter as tk
import webbrowser
from tkinter import filedialog
from datetime import datetime, timedelta
from typing import List
import hashlib
import random
from PIL import Image, ImageDraw
# Timeline
def get_unique_strings(lists_of_strings):
unique_strings = set()
for lst in lists_of_strings:
unique_strings.update(lst)
return unique_strings
def get_intervals(default=24):
return int(input ("Intervals (default " + str(default) + "): ") or default)
def get_csv():
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename(initialdir="./data")
return file_path
def generate_timeline(file_path, num_intervals=24):
intervals_by_day = {}
# read CSV and fill intervals_by_day dictionary
with open(file_path, 'r') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
for row in csv_reader:
game_name, timestamp = row[0], row[1]
if not timestamp or len(timestamp.split()) < 2:
continue
timestamp_dt = datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S')
day = timestamp_dt.date()
if day not in intervals_by_day:
intervals_by_day[day] = [None] * num_intervals
hour, minute, second = timestamp_dt.time().hour, timestamp_dt.time().minute, timestamp_dt.time().second
interval_length = 1440 // num_intervals
interval = hour * 60 // interval_length + minute // interval_length
if 0 <= interval < num_intervals:
if intervals_by_day[day][interval] is None:
intervals_by_day[day][interval] = {}
if game_name.lower() == "<online>" or game_name.lower() == "<offline>":
intervals_by_day[day][interval]["<online>"] = [timestamp]
else:
if game_name in intervals_by_day[day][interval]:
intervals_by_day[day][interval][game_name].append(timestamp)
else:
intervals_by_day[day][interval][game_name] = [timestamp]
# fill in missing days with <offline> values
earliest_day = min(intervals_by_day.keys())
latest_day = max(intervals_by_day.keys())
for day in sorted((earliest_day + timedelta(days=i) for i in range((latest_day - earliest_day).days + 1))):
if day not in intervals_by_day:
intervals_by_day[day] = [None] * num_intervals
for i in range(num_intervals):
intervals_by_day[day][i] = "<offline>"
# compute max game played for each interval in each day
max_games_by_day = []
current_day = None
current_day_intervals = None
for day, intervals in sorted(intervals_by_day.items(), reverse=True):
if current_day is None:
current_day = day
current_day_intervals = [None] * num_intervals
elif current_day != day:
max_games_by_day.append(current_day_intervals)
current_day = day
current_day_intervals = [None] * num_intervals
for i, game_counts in enumerate(intervals):
if not isinstance(game_counts, dict):
current_day_intervals[i] = None
else:
if "<online>" in game_counts and "<offline>" in game_counts:
current_day_intervals[i] = "<online>"
else:
max_game = max(game_counts, key=lambda g: (len(game_counts[g]), game_counts[g][-1]), default=None)
current_day_intervals[i] = max_game
if current_day_intervals is not None:
max_games_by_day.append(current_day_intervals)
return max_games_by_day
# Graphics - colours
def dim_rgb(rgb, brightness=0.67):
r = rgb[0]
g = rgb[1]
b = rgb[2]
return round(r*brightness), round(g*brightness), round(b*brightness)
def string_to_rgb(s):
# Hash the input string using SHA-256
hash_value = hashlib.sha256(s.encode()).hexdigest()
# Take the first 6 characters of the hash as the RGB values
r, g, b = int(hash_value[:2], 16), int(hash_value[2:4], 16), int(hash_value[4:6], 16)
# Randomly adjust the brightness of the color
# brightness = random.uniform(0.5, 1.0)
# r = int(r * brightness)
# g = int(g * brightness)
# b = int(b * brightness)
# Format the RGB values as a hex color code
color_code = "#{:02x}{:02x}{:02x}".format(r, g, b)
return r, g, b
def string_to_color(s):
r, g, b = string_to_rgb(s)
# Format the RGB values as a hex color code
return "#{:02x}{:02x}{:02x}".format(r, g, b)
# Graphics - rendering
def antialias_img(img):
upsampled = img.resize((img.width * 2, img.height * 2), resample=Image.NEAREST)
return upsampled.resize((img.width // 2, img.height // 2), resample=Image.ANTIALIAS)
def draw_squares(lists_of_strings: List[List[str]]):
# Define constants for the size and spacing of the squares
SQUARE_SIZE = 32
SQUARE_SPACING = 8
SQUARE_PADDING = 64
# Outline
OUTLINE_WIDTH = 4
OUTLINE_RADIUS = 8
# Determine the dimensions of the image
num_rows = len(lists_of_strings)
num_cols = max(len(lst) for lst in lists_of_strings)
img_width = num_cols * (SQUARE_SIZE + SQUARE_SPACING) - SQUARE_SPACING + SQUARE_PADDING*2
img_height = num_rows * (SQUARE_SIZE + SQUARE_SPACING) - SQUARE_SPACING + SQUARE_PADDING*2
# Create a new image
img = Image.new("RGB", (img_width, img_height), "white")
draw = ImageDraw.Draw(img)
# Loop over the lists of strings and draw the squares
for row_idx, lst in enumerate(lists_of_strings):
for col_idx, s in enumerate(lst):
if s is None:
# Draw a square with a light red outline and no fill
outline_color = (255, 128, 128)
fill_color = None
elif s == "<offline>":
# Draw a square with a light gray outline and no fill
outline_color = (192, 192, 192)
fill_color = None
elif s == "<online>":
# Draw a square with a dodger blue outline and no fill
outline_color = (30, 144, 255)
fill_color = None
else:
# Convert the string to a color using the string_to_color function
color_code = string_to_color(s)
outline_color = color_code
fill_color = color_code
# Calculate the coordinates of the square
x0 = col_idx * (SQUARE_SIZE + SQUARE_SPACING) + SQUARE_PADDING
y0 = row_idx * (SQUARE_SIZE + SQUARE_SPACING) + SQUARE_PADDING
x1 = x0 + SQUARE_SIZE
y1 = y0 + SQUARE_SIZE
# Draw the square
draw.rounded_rectangle((x0, y0, x1, y1), fill=fill_color, outline=outline_color, radius=OUTLINE_RADIUS, width=OUTLINE_WIDTH)
img = antialias_img(img)
# Display the image
img.show()
# CSS
def generate_css(row_length=24) -> str:
# Set minimum size of a square in pixels.
# Also a scaling factor in vw, so it won't be overrun in case of too many intervals
scale = (32 / row_length)
min_size=12
# Define constants for the size and spacing of the squares
SQUARE_SIZE = f'max({min_size}px, {scale}vw)'
SQUARE_SPACING = f'max({min_size // 4}px, {scale / 4}vw)'
SQUARE_PADDING = f'max({min_size}px, {scale / 4}vw)'
# Outline. Must be min 2px for graphics to render as intended
OUTLINE_WIDTH = f'max(2px, {min_size // 6}px, {scale / 6}vw)'
OUTLINE_RADIUS = f'max(2px, {min_size // 6}px, {scale / 6}vw)'
# Start the CSS string with the opening <style> tag
css = '<style>\n'
# Headings & text
css += f'h1, h2, h3, h4, h5, h6, p, text {{ font-family: Helvetica, sans-serif; }}\n'
css += f'h1, h2, h3, h4, h5, h6, p, text {{ font-family: Helvetica, sans-serif; }}\n'
# Wrapper div. Center the chart
css += f'.wrapper {{ white-space: nowrap; text-align: left; margin-left: 20vw; }}\n'
# Legend
css += f'.legend {{ white-space: nowrap; text-align: left; }}\n'
css += f'.legend tr {{ padding: {SQUARE_SPACING}; }}\n'
css += f'.legend text {{ font-size: {SQUARE_SIZE}; }}\n'
# Add the CSS styles for the squares
css += f'.square {{ border-style: solid; width: {SQUARE_SIZE}; height: {SQUARE_SIZE}; border-radius: {OUTLINE_RADIUS}; border-width: {OUTLINE_WIDTH}; margin: {SQUARE_SPACING}; padding: 0; display: inline-block; }}\n'
css += f'.offline {{ border-color: rgb(192, 192, 192); }}\n'
css += f'.online {{ border-color: rgb(30, 144, 255); }}\n'
css += f'.error {{ border-color: rgb(255, 128, 128); }}\n'
# Special case: squares in legend div
css += f'.legend .square {{ margin: 0; padding: 0; }}\n'
# Finish the CSS string with the closing </style> tag
css += '</style>\n'
return css
# HTML
# Return a single html div representing a square
def html_square(s):
if s is None:
# Add a square with a light red outline and no fill
return f'<div class="error square"></div>'
elif s == "<offline>":
# Add a square with a light gray outline and no fill
return f'<div class="offline square"></div>'
elif s == "<online>":
# Add a square with a dodger blue outline and no fill
return f'<div class="online square"></div>'
else:
# Convert the string to a color using the string_to_color function
# Also use a slightly dimmer tone for the outline
rgb_fill = string_to_rgb(s)
rgb_outline = dim_rgb(rgb_fill)
return f'<div class="game square" style="background-color: rgb{rgb_fill}; border-color: rgb{rgb_outline};"></div>'
# Returns legend div
def html_legend(timeline):
# Unique strings from timeline
uniques = get_unique_strings(timeline)
# Skip non-game squares
game_states = []
html = ''
for s in uniques:
square = html_square(s)
if "game" in square:
game_states.append(s)
# Game states
html += '<tr><td><text><b>Game states</b></text></td></tr>'
for s in game_states:
print(s)
html += f'<tr><td>{html_square(s)}</td><td><text>{s}</text></td></tr>'
# Special states
special_states ={"<offline>" : "Offline", "<online>" : "Online", None : "N/A"}
html += '<tr><td><text><b>Persona states</b></text></td></tr>'
for k in special_states.keys():
v = special_states[k]
html += f'<tr><td>{html_square(k)}</td><td><text>{v}</text></td></tr>'
# Wrap the HTML code in a div and return it
return '<table class="legend">{}</table>'.format(html)
def generate_html_chart(timeline: List[List[str]], name=None) -> str:
# Generate internal stylesheet
css = generate_css(len(timeline[0]))
title = "Steamlapse"
# Different title if name is not set
if name is not None and type(name) == str:
title = (name+"'s Steamlapse")
# Start the HTML string
html = f'<html><head><meta name="viewport" content="width=device-width, initial-scale=1">{css}<title>{title}</title></head><body><div class="wrapper">'
html += f'<h1>{title}</h1>'
# Loop over the lists of strings and add the squares to the HTML string
for row_idx, lst in enumerate(timeline):
for col_idx, s in enumerate(lst):
html += html_square(s)
# Add a line break between rows
html += f'<br>'
# Legend
html += "<h2>Legend</h2>"
html += html_legend(timeline)
# End HTML wrapper div
html += "</div>"
# Finish the HTML string
html += f'</body></html>'
return html
# Processing
def valid_steam_id(steam_id):
if len(steam_id) != 17:
return False
if not steam_id.isnumeric():
return False
return True
def get_time():
# Avoid jet lag
return time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())
def get_player_data(steam_id, api_key):
url = f"https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v2/?key={api_key}&steamids={steam_id}"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
return data
else:
return None
def get_persona(player_data):
return player_data['response']['players'][0]['personaname']
def get_game(player_data):
if not player_data['response']['players']:
return "<offline>"
elif 'gameextrainfo' in player_data['response']['players'][0]:
return player_data['response']['players'][0]['gameextrainfo']
else:
return '<online>'
def append_csv_header(file_path):
if not os.path.exists(file_path) or os.stat(file_path).st_size == 0:
with open(file_path, 'a', newline='') as csv_file:
writer = csv.writer(csv_file)
writer.writerow(['game', 'timestamp'])
def append_csv_entry(file_path, player_data):
append_csv_header(file_path)
game = get_game(player_data)
timestamp = get_time()
with open(file_path, mode='a', newline='') as file:
writer = csv.writer(file)
writer.writerow([game, timestamp])
# Persona name and Steam ID from path
def split_filename(filename):
pattern = r"(.+)_([0-9]{17})\.csv"
match = re.match(pattern, filename)
if match:
return match
else:
return None
# These are not used in the same program, hence this factoring
def persona_from_path(filename):
return os.path.basename(split_filename(filename).group(1))
def id_from_path(filename):
return split_filename(filename).group(2)
def save_html_chart(input_path="data/data.css", output_path=None, prompt=True, persona=None, intervals=24, in_place=False):
if prompt:
intervals = get_intervals()
print("Spawning file dialog.")
input_path = get_csv()
else:
print("Using default intervals:", intervals)
print("Using default input:", input_path)
# Persona can be set in function args for headless operations - if not set, generate from path
if not persona:
persona = persona_from_path(input_path)
print("Persona option not set. Using", persona)
if not output_path:
output_path = f"html/{persona}.html"
print("Output path option not set. Using", output_path)
print("Generating chart for " + persona + "...")
html = generate_html_chart(generate_timeline(input_path, intervals), persona)
# Return HTML as a string if in_place is enabled - else, return the output path
if in_place:
return html
else:
f = open(output_path, "w")
f.write(html)
f.close()
print("Saved to", output_path)
return output_path