-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats_tracker.py
292 lines (249 loc) · 10.1 KB
/
stats_tracker.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
import requests
import time
import threading
import tkinter as tk
from tkinter import messagebox
from openpyxl import Workbook, load_workbook
import os
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.dates as mdates
from datetime import datetime
import yaml
# Initialize global variables
API_KEY = ''
WORKSHOP_ITEM_ID = ''
interval = 60
excel_file_path = 'workshop_data.xlsx'
tracking_data = []
labels = {}
fields = []
canvas = None
line_subs = None
ax = None
fig = None
def open_config_ui():
# Create the configuration window as the main window
config_window = tk.Tk()
config_window.title("Configuration")
# Configure grid to make column 1 (Entry widgets) expand
config_window.columnconfigure(0, weight=0)
config_window.columnconfigure(1, weight=1)
# Create labels and entry widgets for each configuration parameter
tk.Label(config_window, text="API Key:").grid(row=0, column=0, sticky='e', padx=5, pady=5)
api_key_entry = tk.Entry(config_window)
api_key_entry.grid(row=0, column=1, sticky='ew', padx=5, pady=5)
tk.Label(config_window, text="Workshop Item ID:").grid(row=1, column=0, sticky='e', padx=5, pady=5)
workshop_id_entry = tk.Entry(config_window)
workshop_id_entry.grid(row=1, column=1, sticky='ew', padx=5, pady=5)
tk.Label(config_window, text="Interval (seconds):").grid(row=2, column=0, sticky='e', padx=5, pady=5)
interval_entry = tk.Entry(config_window)
interval_entry.grid(row=2, column=1, sticky='ew', padx=5, pady=5)
tk.Label(config_window, text="Excel File Path:").grid(row=3, column=0, sticky='e', padx=5, pady=5)
excel_path_entry = tk.Entry(config_window)
excel_path_entry.grid(row=3, column=1, sticky='ew', padx=5, pady=5)
# Load existing configuration if available
try:
with open('config.yaml', 'r') as config_file:
existing_config = yaml.safe_load(config_file) or {}
api_key_entry.insert(0, existing_config.get('api_key', ''))
workshop_id_entry.insert(0, existing_config.get('workshop_item_id', ''))
interval_entry.insert(0, str(existing_config.get('interval', '60')))
excel_path_entry.insert(0, existing_config.get('excel_file_path', 'workshop_data.xlsx'))
except FileNotFoundError:
# No existing config, fields remain empty
print("config.yaml not found. Starting with empty configuration.")
except Exception as e:
messagebox.showerror("Error", f"Failed to load configuration: {e}")
print(f"Error loading configuration: {e}")
def apply_settings():
# Get values from entry widgets
api_key = api_key_entry.get().strip()
workshop_id = workshop_id_entry.get().strip()
interval_value = interval_entry.get().strip()
excel_path = excel_path_entry.get().strip()
if not api_key or not workshop_id:
messagebox.showerror("Error", "API Key and Workshop Item ID are required.")
return
# Validate interval
try:
interval_value = int(interval_value)
except ValueError:
messagebox.showerror("Error", "Interval must be an integer.")
return
# Save to config.yaml
new_config = {
'api_key': api_key,
'workshop_item_id': workshop_id,
'interval': interval_value,
'excel_file_path': excel_path
}
try:
with open('config.yaml', 'w') as config_file:
yaml.safe_dump(new_config, config_file)
messagebox.showinfo("Success", "Configuration applied successfully.")
except Exception as e:
messagebox.showerror("Error", f"Failed to save configuration: {e}")
print(f"Error saving configuration: {e}")
def start_program():
# Apply settings first
apply_settings()
# Close the configuration window
config_window.destroy()
# Start the main program
main_program()
# Create a frame for the buttons
button_frame = tk.Frame(config_window)
button_frame.grid(row=4, column=0, columnspan=2, pady=10, padx=5, sticky='e')
# Place the buttons inside the frame
apply_button = tk.Button(button_frame, text="Apply", command=apply_settings)
apply_button.pack(side='right', padx=5)
start_button = tk.Button(button_frame, text="Start", command=start_program)
start_button.pack(side='right')
# Start the Tkinter event loop
config_window.mainloop()
def main_program():
# Load configuration
try:
with open('config.yaml', 'r') as config_file:
config = yaml.safe_load(config_file) or {}
except FileNotFoundError:
messagebox.showerror("Error", "Configuration file not found. Please apply settings first.")
return
except Exception as e:
messagebox.showerror("Error", f"Failed to load configuration: {e}")
print(f"Error loading configuration: {e}")
return
# Access configuration parameters
global API_KEY, WORKSHOP_ITEM_ID, interval, excel_file_path
API_KEY = config.get('api_key', '')
WORKSHOP_ITEM_ID = config.get('workshop_item_id', '')
interval = config.get('interval', 60)
excel_file_path = config.get('excel_file_path', 'workshop_data.xlsx')
if not API_KEY or not WORKSHOP_ITEM_ID:
messagebox.showerror("Error", "API Key and Workshop Item ID are required. Please apply settings first.")
return
# Create the main UI window
global root
root = tk.Tk()
root.title("Steam Workshop Item Tracker")
# Initialize main UI components
initialize_main_ui()
# Start the Tkinter event loop for the main UI
root.mainloop()
def initialize_main_ui():
global labels, fields, canvas, line_subs, ax, fig
labels = {}
fields = [
'timestamp', 'title', 'views', 'subscriptions', 'favorites',
'lifetime_subscriptions', 'lifetime_favorited'
]
for idx, field in enumerate(fields):
tk.Label(root, text=field.replace('_', ' ').title() + ":").grid(row=idx, column=0, sticky='e')
labels[field] = tk.Label(root, text="")
labels[field].grid(row=idx, column=1, sticky='w')
# Initialize matplotlib figure
fig, ax = plt.subplots(figsize=(8, 5))
# Plot line for subscriptions
line_subs, = ax.plot([], [], 's-', label='Subscriptions', color='green')
ax.set_xlabel('Time')
ax.set_ylabel('Count')
ax.set_title('Workshop Item Metrics Over Time')
ax.legend()
# Embed the figure in the Tkinter window
global canvas
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.draw()
canvas.get_tk_widget().grid(row=len(fields), column=0, columnspan=2)
# Start tracking in a separate thread
tracking_thread = threading.Thread(target=track_item)
tracking_thread.daemon = True
tracking_thread.start()
def fetch_workshop_item_details():
url = 'https://api.steampowered.com/IPublishedFileService/GetDetails/v1/'
params = {
'key': API_KEY,
'publishedfileids[0]': WORKSHOP_ITEM_ID
}
try:
response = requests.get(url, params=params)
if response.status_code == 200:
data = response.json()
item_details = data['response']['publishedfiledetails'][0]
return item_details
else:
print(f"Error fetching data: {response.status_code}")
return None
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
return None
def save_to_excel(data):
# Get current month and year
current_month = datetime.now().strftime('%m-%Y')
# Update the excel file path with the current month
excel_file = excel_file_path.replace('.xlsx', f'_{current_month}.xlsx')
if not os.path.exists(excel_file):
wb = Workbook()
ws = wb.active
ws.title = "Tracking Data"
ws.append([
'Timestamp', 'Title', 'Views', 'Subscriptions', 'Favorites',
'Lifetime Subscriptions', 'Lifetime Favorites'
])
wb.save(excel_file)
else:
wb = load_workbook(excel_file)
ws = wb["Tracking Data"]
latest_data = data[-1]
ws.append([
latest_data['timestamp'],
latest_data['title'],
latest_data['views'],
latest_data['subscriptions'],
latest_data['favorites'],
latest_data['lifetime_subscriptions'],
latest_data['lifetime_favorited'],
])
wb.save(excel_file)
def update_ui(data):
for field in fields:
labels[field].config(text=str(data[field]))
# Update the graph data
times = [point['timestamp'] for point in tracking_data]
subscriptions = [point['subscriptions'] for point in tracking_data]
# Convert time strings to matplotlib date format
time_nums = [mdates.datestr2num(t) for t in times]
# Update data for the line
line_subs.set_data(time_nums, subscriptions)
# Adjust the axes
ax.relim()
ax.autoscale_view()
# Format the x-axis to show time properly
ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S'))
fig.autofmt_xdate()
canvas.draw()
root.update_idletasks()
def track_item():
while True:
item_details = fetch_workshop_item_details()
if item_details:
data_point = {
'timestamp': time.strftime('%Y-%m-%d %H:%M:%S'),
'title': item_details.get('title', 'N/A'),
'views': int(item_details.get('views', 0)),
'subscriptions': int(item_details.get('subscriptions', 0)),
'favorites': int(item_details.get('favorited', 0)),
'lifetime_subscriptions': int(item_details.get('lifetime_subscriptions', 0)),
'lifetime_favorited': int(item_details.get('lifetime_favorited', 0)),
}
tracking_data.append(data_point)
# Schedule the UI update on the main thread
root.after(0, update_ui, data_point)
print(f"Data fetched at {data_point['timestamp']}")
save_to_excel(tracking_data)
else:
print("Failed to fetch data.")
time.sleep(interval)
if __name__ == "__main__":
# Start with the configuration UI
open_config_ui()