def indx_cb(indx): version = 0 # box version def update(data): """ Assert data into the INDX. If the version is incorrect, the correct version will be grabbed and the update re-sent. data -- An object to assert into the box. """ global version try: response = indx.update(version, data) version = response['data']['@version'] # update the version except Exception as e: if isinstance(e, urllib2.HTTPError) and e.code == 409: # handle a version incorrect error, and update the version response = e.read() json_response = json.loads(response) version = json_response['@version'] update(data) # try updating again now the version is correct else: logging.error("Error updating INDX: {0}".format(e)) sys.exit(0) def get_last_date(): try: resp = indx.get_by_ids([args['statusid']]) data = resp['data'] val = data[args['statusid']]['last_date'][0] return val['@value'] except Exception as e: logging.error("Error reading last date (usually means this is the first time this has been run - not a problem), e: {0}".format(e)) return args['earliest'] def set_last_date(last_date): data = {"@id": args['statusid'], "last_date": last_date} update(data) def yesterday(): from datetime import date, timedelta yesterday = date.today() - timedelta(1) return yesterday.strftime("%Y-%m-%d") last_date = get_last_date() nikeplus = nikeplus.NikePlus() nikeplus.login(args['email'], password) nikeplus.get_token() activities = nikeplus.get_activities(last_date, yesterday()) for activity_container in activities: for activity in activity_container: activity_id = activity['activityId'] logging.debug("activity id: {0}".format(activity_id)) detail = nikeplus.get_activity_detail(activity_id) logging.debug("activity_details: {0}".format(pprint.pformat(detail))) detail['@id'] = "nikeplus-activity-detail-{0}".format(activity_id) update(detail) set_last_date(yesterday())
parser.add_argument('email', type = str, help = "E-mail address of the user in the Nike+ system") parser.add_argument('start_date', type = str, help = "Start date, like: 2013-03-20") parser.add_argument('end_date', type = str, help = "End date, like: 2013-03-21") parser.add_argument('--debug', default = False, action="store_true", help = "Turn on verbose debugging") args = vars(parser.parse_args()) password = getpass.getpass() if args['debug']: logging.basicConfig(level = logging.DEBUG) nikeplus = nikeplus.NikePlus() nikeplus.login(args['email'], password) nikeplus.get_token() activities = nikeplus.get_activities(args['start_date'], args['end_date']) #activities_all = [] activitiesDir = os.path.join(os.path.dirname(__file__), 'activities') for activity_list in activities: if type(activity_list) != type([]): activity_list = [activity_list] for activity in activity_list: activity_id = activity['activityId'] activity_timedate = activity['startTime'][0:10] + '_' + activity['startTime'][11:13] + '-' + activity['startTime'][14:16] + '-' + activity['startTime'][17:19] logging.debug("activity id: {0}".format(activity_id)) logging.debug("activity_details: {0}".format(pprint.pformat(nikeplus.get_activity_detail(activity_id)))) logging.debug("gps_data: {0}".format(pprint.pformat(nikeplus.get_gps_data(activity_id)))) #activities_all.append(nikeplus.get_activity_detail(activity_id)) #activities_all.append(nikeplus.get_gps_data(activity_id)) if not os.path.exists(activitiesDir):
def set_last_date(last_date): data = {"@id": args['statusid'], "last_date": last_date} update(data) def yesterday(): from datetime import date, timedelta yesterday = date.today() - timedelta(1) return yesterday.strftime("%Y-%m-%d") last_date = get_last_date() nikeplus = nikeplus.NikePlus() nikeplus.login(args['email'], password) nikeplus.get_token() activities = nikeplus.get_activities(last_date, yesterday()) for activity_container in activities: for activity in activity_container: activity_id = activity['activityId'] logging.debug("activity id: {0}".format(activity_id)) detail = nikeplus.get_activity_detail(activity_id) logging.debug("activity_details: {0}".format(pprint.pformat(detail))) detail['@id'] = "nikeplus-activity-detail-{0}".format(activity_id) update(detail) set_last_date(yesterday())
logging.error("Error reading last date (usually means this is the first time this has been run - not a problem), e: {0}".format(e)) return args['earliest'] def set_last_date(last_date): data = {"@id": args['statusid'], "last_date": last_date} update(data) def yesterday(): from datetime import date, timedelta yesterday = date.today() - timedelta(1) return yesterday.strftime("%Y-%m-%d") last_date = get_last_date() nikeplus = nikeplus.NikePlus() nikeplus.login(args['email'], password) nikeplus.get_token() activities = nikeplus.get_activities(last_date, yesterday()) for activity_container in activities: for activity in activity_container: activity_id = activity['activityId'] logging.debug("activity id: {0}".format(activity_id)) detail = nikeplus.get_activity_detail(activity_id) logging.debug("activity_details: {0}".format(pprint.pformat(detail))) detail['@id'] = "nikeplus-activity-detail-{0}".format(activity_id) update(detail) set_last_date(yesterday())