-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayTime.py
252 lines (187 loc) · 8.04 KB
/
playTime.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
import datetime
import json
import os
currn_dir = os.getcwd()
def getTime(instance_name):
os.chdir(currn_dir)
with open("launcherProfiles.json", "r") as js_read:
s = js_read.read()
s = s.replace('\t','') #Trailing commas in dict cause file read problems, these lines will fix it.
s = s.replace('\n','') #Found this on stackoverflow.
s = s.replace(',}','}')
s = s.replace(',]',']')
data = json.loads(s)
#print(json.dumps(data, indent=4,))
timePlayed = data['all-instances'][0][instance_name][0]['timePlayed']
if timePlayed == 0:
return "00:00:00"
else:
return timePlayed
def addTime(instance_name, time_amt):
if instance_name == "Latest Release" or instance_name == "Latest Snapshot":
print("Cannot add time to the latest release or snapshot instance.")
return
os.chdir(currn_dir)
with open("launcherProfiles.json", "r") as js_read:
s = js_read.read()
s = s.replace('\t','') #Trailing commas in dict cause file read problems, these lines will fix it.
s = s.replace('\n','') #Found this on stackoverflow.
s = s.replace(',}','}')
s = s.replace(',]',']')
data = json.loads(s)
#print(json.dumps(data, indent=4,))
timePlayed = data['all-instances'][0][instance_name][0]['timePlayed']
hrs,mins,secs=timePlayed.split(':')
oghrs = int(hrs)
ogmins = int(mins)
ogsecs = int(secs)
originalTime=datetime.datetime.combine(datetime.date.today(), datetime.time(hour=oghrs,minute=ogmins,second=ogsecs))
hd,md,sd=str(time_amt).split(':')
newhrs = int(hd)
newmins = int(md)
newsecs = int(sd)
addedTime=datetime.timedelta(hours=newhrs, minutes=newmins, seconds=newsecs)
updatedTime = originalTime + addedTime
timeToPut = updatedTime.time()
data['all-instances'][0][instance_name][0]['timePlayed'] = str(timeToPut)
with open("launcherProfiles.json", "w") as js_write:
js_write.write(json.dumps(data, indent=4))
def timeToWords(time_str):
"""Formats a time string (HH:MM:SS) into words."""
hour, minute, second = map(int, time_str.split(':'))
return f"{hour}h {minute}m {second}s"
def fancyTimeToWords(time_str):
"""Formats a time string (HH:MM:SS) into words."""
hour, minute, second = map(int, time_str.split(':'))
if hour == 0:
return f"{minute} minutes"
else:
return f"{hour} hours and {minute} minutes"
def getTotalTimePlayed():
with open("launcherProfiles.json") as f:
data = json.load(f)
all_insts = data['all-instances']
all_time = "00:00:00"
for inst_dict in all_insts:
for instance_name, instance_data_list in inst_dict.items():
instance_data = instance_data_list[0]
time_amt = instance_data["timePlayed"]
timePlayed = all_time
hrs,mins,secs=timePlayed.split(':')
oghrs = int(hrs)
ogmins = int(mins)
ogsecs = int(secs)
originalTime=datetime.datetime.combine(datetime.date.today(), datetime.time(hour=oghrs,minute=ogmins,second=ogsecs))
hd,md,sd=str(time_amt).split(':')
newhrs = int(hd)
newmins = int(md)
newsecs = int(sd)
addedTime=datetime.timedelta(hours=newhrs, minutes=newmins, seconds=newsecs)
updatedTime = originalTime + addedTime
timeToPut = updatedTime.time()
timeToPut = str(timeToPut)
all_time = timeToPut
return all_time
def getFavInstance():
with open("launcherProfiles.json") as f:
data = json.load(f)
all_insts = data['all-instances']
if all_insts == [{}]:
return "No instance"
all_time = "00:00:00"
fav_instance = ""
fav_time = "00:00:00"
playTimes = []
for inst_dict in all_insts:
for instance_name, instance_data_list in inst_dict.items():
instance_data = instance_data_list[0]
time_amt = instance_data["timePlayed"]
timePlayed = all_time
hd,md,sd=str(time_amt).split(':')
newhrs = int(hd)
newmins = int(md)
newsecs = int(sd)
addedTime=datetime.timedelta(hours=newhrs, minutes=newmins, seconds=newsecs)
playTimes.append(str(addedTime))
time_objects = [datetime.datetime.strptime(t, '%H:%M:%S') for t in playTimes]
max_time = max(time_objects)
max_time_str = max_time.strftime('%H:%M:%S')
for inst_dict in all_insts:
for instance_name, instance_data_list in inst_dict.items():
instance_data = instance_data_list[0]
time_amt = instance_data["timePlayed"]
timePlayed = all_time
hrs,mins,secs=timePlayed.split(':')
oghrs = int(hrs)
ogmins = int(mins)
ogsecs = int(secs)
originalTime=datetime.datetime.combine(datetime.date.today(), datetime.time(hour=oghrs,minute=ogmins,second=ogsecs))
hd,md,sd=str(time_amt).split(':')
newhrs = int(hd)
newmins = int(md)
newsecs = int(sd)
addedTime=datetime.timedelta(hours=newhrs, minutes=newmins, seconds=newsecs)
updatedTime = originalTime + addedTime
updatedTime = updatedTime.time()
updatedTime = str(updatedTime)
if updatedTime == max_time_str:
fav_instance = instance_name
fav_time = updatedTime
if fav_instance == "":
return "No instance"
else:
return fav_instance
def getFavInstIcon():
with open("launcherProfiles.json") as f:
data = json.load(f)
all_insts = data['all-instances']
if all_insts == [{}]:
return "release"
all_time = "00:00:00"
fav_instance = ""
fav_time = "00:00:00"
playTimes = []
for inst_dict in all_insts:
for instance_name, instance_data_list in inst_dict.items():
instance_data = instance_data_list[0]
time_amt = instance_data["timePlayed"]
timePlayed = all_time
hd,md,sd=str(time_amt).split(':')
newhrs = int(hd)
newmins = int(md)
newsecs = int(sd)
addedTime=datetime.timedelta(hours=newhrs, minutes=newmins, seconds=newsecs)
playTimes.append(str(addedTime))
time_objects = [datetime.datetime.strptime(t, '%H:%M:%S') for t in playTimes]
max_time = max(time_objects)
max_time_str = max_time.strftime('%H:%M:%S')
for inst_dict in all_insts:
for instance_name, instance_data_list in inst_dict.items():
instance_data = instance_data_list[0]
time_amt = instance_data["timePlayed"]
timePlayed = all_time
hrs,mins,secs=timePlayed.split(':')
oghrs = int(hrs)
ogmins = int(mins)
ogsecs = int(secs)
originalTime=datetime.datetime.combine(datetime.date.today(), datetime.time(hour=oghrs,minute=ogmins,second=ogsecs))
hd,md,sd=str(time_amt).split(':')
newhrs = int(hd)
newmins = int(md)
newsecs = int(sd)
addedTime=datetime.timedelta(hours=newhrs, minutes=newmins, seconds=newsecs)
updatedTime = originalTime + addedTime
updatedTime = updatedTime.time()
updatedTime = str(updatedTime)
if updatedTime == max_time_str:
fav_instance = instance_name
fav_time = updatedTime
with open("launcherProfiles.json") as f:
data = json.load(f)
all_insts = data['all-instances']
for inst_dict in all_insts:
for instance_name, instance_data_list in inst_dict.items():
if instance_name == fav_instance:
return instance_data_list[0]["icon"]
return "release"
getFavInstIcon()