-
Notifications
You must be signed in to change notification settings - Fork 0
/
mystats.py
69 lines (58 loc) · 1.85 KB
/
mystats.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
#!/usr/bin/env python
import sqlite3
import json
import urllib2
import ConfigParser
import anki.storage
import anki.stats
# load our configuration
config = ConfigParser.ConfigParser()
config.read('mystats.ini')
def configSectionMap(section):
configMap = {}
options = config.options(section)
for option in options:
try:
configMap[option] = config.get(section,option)
except:
configMap[option] = None
return configMap
print
# path to the sqlite collection file
collection_path = configSectionMap('Collection')['path']
# initialize a collection object for this collection
# and obtain the statistics
ankicollection = anki.storage.Collection(collection_path)
stats = anki.stats.CollectionStats(ankicollection)
# whole collection or a single deck?
if int(configSectionMap('Stats')['all']) == 1:
stats.wholeCollection = True
else:
stats.wholeCollection = False
deckname = configSectionMap('Stats')['deck']
deck_id = stats._didForDeckName(deckname)
print 'Deck id = {0}'.format(deck_id)
if deck_id == -1:
print "ERROR: no deck with configured name"
raise ValueError('There is no deck with the configured name')
# configure url for data upload
url = configSectionMap('Server')['url'] + ':'
url += configSectionMap('Server')['port']
url += configSectionMap('Server')['uploadpath']
#stats._didForDeckName("Every card")
req = urllib2.Request(url)
req.add_header('Content-Type', 'application/json')
json_data = json.dumps(stats.todayStats_(), cls=anki.stats.StatsEncoder)
print json_data
try:
response = urllib2.urlopen(req, json_data )
except urllib2.HTTPError as e:
if e.code == 404:
print 'Unknown path'
else:
print 'Unknown error: {}'.format(e.code)
except urllib2.URLError as e:
print 'Error - not HTTP specific'
else:
body = response.read()
print body