forked from fsteffek/steamplog
/
steamplog.py
executable file
·215 lines (183 loc) · 6.78 KB
/
steamplog.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
#!/usr/bin/python
"""steamplog - log and plot your steam gaming time
usage:
steamplog.py log
steamplog.py plot [bar | point | line] [-a | [<DATE_FROM>] [<DATE_TO>]]
[-lc] [-o FILE | -i] [-v]
steamplog.py update-appnames
steamplog.py create-config
steamplog.py stats [--full]
plot options:
-a, --all plot every available playtime
[<DATE_FROM>] include every playtime from this date (format: YYYY-MM-DD)
[default: last 14 days]
[<DATE_TO>] include every playtime from this date (format: YYYY-MM-DD)
[default: today]
-c, --color top 10 games have different colors
-l, --legend include a legend
-o FILENAME, --output FILENAME
FILENAME of the output image without extension
-i, --individual plot each game in a new image
stats options:
--full print every game
other:
-v, --verbose be verbose
-h, --help show this help
"""
import sys
import json
import time
import datetime
from docopt import docopt
from steamplog.config import Config
from steamplog.appmgr import AppMGR, App
import steamplog.plot as plot
import steamplog.utils as utils
def main(argv=None):
if argv is None:
argv = sys.argv
global options
global application_name
options = docopt(__doc__, argv=argv[1:])
application_name = argv[0]
config = Config()
config.path = "config.json"
if options['create-config']:
config.create()
sys.exit(0)
config.read('config.json')
AM = AppMGR(config.DB_engine,
date_limit=config.date_limit)
if options['--verbose']:
print 'Engine:', config.DB_engine
AM.db.create_tables()
if options['update-appnames']:
print >> sys.stderr, application_name + \
': INFO: This might take a while'
AM.update_names()
AM.db.close()
sys.exit(0)
if options['stats']:
AM.get_max_minutes()
total_sum = 0
for app in AM.applist:
total_sum = total_sum + app.playtime
print 'Steam total playtime:', "%0.2f" % (total_sum/60.0), 'hours', \
"(%0.2f days)" % (total_sum/60.0/24.0)
print ''
if options['--full']:
for game in AM.applist:
print "%0.2f" % (game.playtime/60.0), 'hours','\t', game.name
else:
for game in AM.applist[:10]:
print "%0.2f" % (game.playtime/60.0), 'hours','\t', game.name
if options['plot']:
# Set options
if options['<DATE_TO>'] == None:
AM.set_to(utils.datetime2unix(datetime.datetime.now()))
else:
AM.set_to(utils.datetime2unix(parse_date(options['<DATE_TO>'])))
if options['<DATE_FROM>'] == None:
AM.set_from(AM.get_to() - 1209600)
else:
AM.set_from(utils.datetime2unix(
parse_date(options['<DATE_FROM>'])))
# Set date to all available logs
if options['--all']:
AM.set_to(utils.datetime2unix(datetime.datetime.now()))
AM.set_from(utils.datetime2unix(datetime.datetime(2014, 6, 1)))
if options['--verbose']:
print 'Plotting...'
AM.games_played = AM.find_games_played()
AM.process_games_played()
AM.sort_most_played()
makePlot(AM)
AM.db.close()
sys.exit(0)
owned_games = utils.get_owned_games(config.API_key, config.Steam_ID)
if 'games' not in owned_games:
print >> sys.stderr, application_name + ': ERROR: No games found'
AM.db.close()
sys.exit(0)
if options['log']:
data = [(x['appid'], x['playtime_forever']) for x in
owned_games['games']]
now = utils.round_datetime(datetime.datetime.utcnow())
AM.db.log_playtime(data, utils.datetime2unix(now))
time.sleep(10)
# Disconnect from database
AM.db.close()
def parse_date(date):
return datetime.datetime.strptime(date, "%Y-%m-%d")
def makePlot(AM):
import copy
# Set plot type
plot_type = 'bar'
if options['point']:
plot_type = 'point'
if options['line']:
plot_type = 'line'
# Set x-limits
xlim = (AM.get_dt_from(), AM.get_dt_to())
if not AM.applist:
info = application_name + ': INFO: No data found between ' + \
AM.get_dt_from().strftime("%Y-%m-%d") + ' and ' + \
AM.get_dt_to().strftime("%Y-%m-%d")
print >> sys.stderr, info
sys.exit(0)
# 3 different plotting options
if options['--color']: # different color apps
data = []
offset = {}
# Combine all minutes off all apps
for app in AM.applist:
one_data = []
for d, m in zip(app.date, app.playtime):
if d not in offset:
offset[d] = 0
one_data.append((datetime.datetime.utcfromtimestamp(d),
m, offset[d]))
offset[d] = int(offset[d] + m)
data.append(copy.copy(one_data))
one_data[:] = []
if options['--legend']:
legend = [app.name[:25] for app in AM.applist[:10]] + ['Other']
else:
legend = None
if options['--output'] is None:
options['--output'] = 'plot_detailed'
plot.plot(data, xlim=xlim, fname=options['--output'], plot_type='bar',
legend=legend,
title='Steamplog ('+options['--output']+')')
print '\'' + options['--output'] + '.png\''
elif options['--individual']:
for app in AM.applist:
app_data = {}
for date, minutes in zip(app.date, app.playtime):
if date in app_data:
app_data[date] = app_data[date] + minutes
else:
app_data[date] = minutes
data = [(datetime.datetime.utcfromtimestamp(key),
app_data[key]) for key in app_data]
data.sort()
plot.plot(data, xlim=xlim, fname='plots/'+app.name,
plot_type=plot_type, title=app.name)
print '\'plots/' + app.name + '.png\''
app_data.clear()
data[:] = []
else: # plot all in one plot
if options['--output'] is None:
options['--output'] = 'plot_all'
data = merge_playtimes(AM.applist)
plot.plot(data, xlim=xlim, fname=options['--output'],
plot_type=plot_type, title='Steamplog')
print '\'' + options['--output'] + '.png\''
def merge_playtimes(app_list):
playtime = {}
for app in app_list:
for d, m in zip(app.date, app.playtime):
playtime[d] = playtime[d] + m if d in playtime else m
return [(utils.unix2datetime(d), playtime[d]) for d in playtime]
if __name__ == "__main__":
sys.exit(main())