def handle_json(self, user, file):
        for line in file: 
            try:
                obj = json.loads(line)
                self.stdout.write(str(obj) + '\n')
            
                if obj[u'workout_type'] == u'Running': 
                    run = Run()
                    run.user = user

                    date = datetime.strptime(obj[u'date'], "%m/%d/%y")
                    self.stdout.write("Date: " + str(date) + ', ')
                    run.date = date
                    
                    distance = float(obj[u'distance'])
                    self.stdout.write("Distance: " + str(distance) + ', ')
                    run.distance = distance
                    
                    duration_in_secs = int(obj[u'duration'])
                    duration = run.set_duration(0,0,duration_in_secs)
                    self.stdout.write("Duration: " + str(duration) + ', ')
                    
                    if not obj[u'hr_avg'] == None: 
                        heart_rate = int(obj[u'hr_avg'])
                        run.average_heart_rate = heart_rate
                        self.stdout.write("HR: " + str(heart_rate) + ', ')
                    self.stdout.write('\n')
                    
                    run.save()
            except: 
                 print "Skipping line: " + line            
    def handle_csv(self, user, file):     
        reader = csv.reader(file, dialect=csv.excel)
        for row in reader:
            try: 
                run = Run()
                run.user = user
                
                date = datetime.strptime(row[0], "%b %d, %Y")
                run.date = date
                self.stdout.write("Date: " + str(date) + ', ')
                
                minutes = int(row[1])
                seconds = int(row[2])
                d = run.set_duration(0,minutes,seconds)
                self.stdout.write("Dur: " + str(d) + ', ')
                
                distance = Decimal(float(row[3]))
                run.distance = distance
                self.stdout.write("Dist: " + str(distance) + ', ')
                
                if (row[5]):
                    hr = int(row[5])
                    run.average_heart_rate = hr
                    self.stdout.write("HR: " + str(hr) + ', ')
                    
                if (row[6]): 
                    calories = int(row[6])
                    run.calories = calories
                    self.stdout.write("Cal: " + str(calories) + ', ')
                else: 
                    run.set_calories()
                    self.stdout.write("Cal': " + str(run.calories) + ', ')
                
                run.set_zone()
                self.stdout.write("Zone: " + str(run.zone) + '\n')

                run.save()
            except ValueError:
                self.stdout.write("Skipping: " + ', '.join(row) + '\n')
Beispiel #3
0
def obj_to_run(obj):
    
    _date = u'date'
    _duration = u'duration'
    _average_heart_rate = u'average_heart_rate'
    _calories = u'calories'
    _distance = u'distance'
    
    run = Run()
    
    # try to parse date field
    run.date = datetime.datetime.strptime(obj[_date], "%Y-%m-%d").date()

    # try to parse the duration and distance fields
    run.duration = hms_to_time(0,0,int(obj[_duration]))
    run.distance = float(obj[_distance]) / 1609.344

    # try to parse the HR and calories fields, which may be null
    if obj[_average_heart_rate]: 
        run.average_heart_rate = int(obj[_average_heart_rate])
    if obj[_calories]: 
        run.calories = int(obj[_calories])
    return run