-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunnerplus.py
executable file
·145 lines (125 loc) · 4.93 KB
/
runnerplus.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
#! /usr/bin/env python
import glob
import urllib
import os
import sys
from os.path import join
import subprocess
import ConfigParser
import shutil
import string
url = "http://www.runnerplus.com/"
script_version = 0.02
sync_successful = False
config_filename = join(os.path.expanduser("~"), ".runnerplusrc")
debug = True
testing = False
def push_data():
xmlfile = None
global sync_successful
global config_filename
# check for new versions of the script
new_version = version_check()
if new_version > script_version:
print "A new version of this script is now available. Please download it."
# read the config file
config = ConfigParser.SafeConfigParser({'dirname': '.runnerplus'})
config_found = config.read(config_filename)
if not config_found:
raise StandardError("Config File not found. See README file for instructions on creating a config file")
email = config.get('Login','email')
password = config.get('Login','password')
backupdir = join(os.path.expanduser("~"), config.get('Backup', 'dirname'), 'synced')
if debug: print "config info found for email:" + email
uid = validate_user(email, password)
if debug: print "uid == %s." % uid
if uid == "0":
raise StandardError("authentication failed")
# create the backup directory, if not present
if not os.path.exists(backupdir):
if debug: print "backup dir not found, so creating it"
os.makedirs(backupdir)
mount_point = get_ipod_mount()
if debug: print "iPod found at " + mount_point
path = join(mount_point, "iPod_Control", "Device", "Trainer", "Workouts", "Empeds")
if os.path.isdir(path):
stats = os.statvfs(path)
total_space = (stats[2] * stats[0]) / (1024 * 1024)
avail_space = (stats[4] * stats[0]) / (1024 * 1024)
print "iPod mounted at %s has a capacity of %d MB and has %d MB available" % \
(mount_point, total_space, avail_space)
filelist = glob.glob(join(path, '*', '*', '*-*.xml'))
for xmlfile in filelist:
if debug: print "debug found: " + xmlfile
post_to_runnerplus(uid, xmlfile, backupdir)
sync_successful = True
def post_to_runnerplus(uid, fullpath, backupdir):
basename = os.path.basename(fullpath)
if os.path.exists(join(backupdir, basename)):
if debug: print "File has been previously synced: " + basename
else:
if debug: print "Syncing file: " + basename
try:
f = open(fullpath)
data = f.read()
f.close()
except IOError as (errno, strerror):
print "reading file: I/O error({0}): {1}".format(errno, strerror)
except:
print "reading file: Unexpected Error:", sys.exc_info()[0]
raise
v = "Python uploader " + str(script_version) + " (Linux)"
post_data = urllib.urlencode({'uid' : uid, 'v' : v, 'data' : data })
post_url = url + "profile/api_postdata.asp"
if not testing:
try:
contents = urllib.urlopen(post_url, post_data).read()
# move to backup folder
if debug: print "Sync successful. Back up file:" + basename
shutil.copy(fullpath, backupdir)
except IOError as (errno, strerror):
print "I/O error({0}): {1}".format(errno, strerror)
except:
print "Unexpected Error: ", sys.exc_info()[0]
raise
else:
contents = "Testing"
if debug: print contents
def version_check():
print "checking for updates to script..."
sock = urllib.urlopen("http://www.kurup.org/software/runnerplus/version")
htmlSource = sock.read()
sock.close()
try:
version = string.atof(htmlSource)
except:
version = 0
return version
def validate_user(email, password):
if debug: print "validating user " + email + " ..."
user_url = url + "profile/api_validateuser.asp"
post_data = urllib.urlencode({'n' : email, 'p' : password })
if not testing:
contents = urllib.urlopen(user_url, post_data)
uid = contents.read()
else:
uid = "999"
return uid
def get_ipod_mount():
"""Search all mounted volumes for device which has NikePlus data"""
output = subprocess.Popen(['/bin/df','-P','-x','tmpfs'],stdout=subprocess.PIPE).communicate()[0]
devices = output.split('\n')[1:]
for line in devices:
if line:
# account for mountpoints with spaces in them
mount = ' '.join(line.split()[5:])
nike = join(mount, "iPod_Control", "Device", "Trainer", "Workouts", "Empeds")
if os.path.exists(nike):
return mount
raise StandardError("failed to find iPod-NikePlus file system in any filesystem")
if __name__ == "__main__":
push_data()
if sync_successful:
print "Successfully synced to runnerplus"
else:
print "Sync was unsuccessful"