SHOW:
|
|
- or go back to the newest paste.
1 | def epoch_time(d): | |
2 | """Seriously this is the best way to do this???""" | |
3 | return int((d - date(1970,1,1)).total_seconds()) | |
4 | ||
5 | TODAY = date.today() | |
6 | ONE_DAY = timedelta(days=1).total_seconds() | |
7 | NUM_WEEKS = 2 | |
8 | WEEK_AGO = TODAY - timedelta(weeks=NUM_WEEKS) | |
9 | TWO_WEEKS_AGO = TODAY - timedelta(weeks=NUM_WEEKS * 2) | |
10 | TWO_WEEKS_AGO_EPOCH = epoch_time(TWO_WEEKS_AGO) | |
11 | ||
12 | GoalMetadata = collections.namedtuple('GoalMetadata', ['today_count', 'week_count', 'prior_week_count']) | |
13 | Datapoint = collections.namedtuple('Datapoint', ['timestamp', 'value', 'comment', 'id', 'updated_at', 'requestid', 'daystamp', 'canonical']) | |
14 | ||
15 | beeminder_url = 'https://www.beeminder.com/api/v1/users/me.json' | |
16 | beeminder_url += ('?diff_since=%s&' % TWO_WEEKS_AGO_EPOCH) + urllib.urlencode( | |
17 | {'auth_token':secrets.BEEMINDER_AUTH_TOKEN}) | |
18 | user_data = json.loads(urllib2.urlopen(beeminder_url).read()) | |
19 | ||
20 | goal_metadata = collections.defaultdict(lambda: GoalMetadata(0, 0, 0)) | |
21 | for goal in user_data['goals']: | |
22 | points = [Datapoint(**p) for p in goal['datapoints']] | |
23 | points = [p._replace(daystamp = datetime.strptime(p.daystamp, '%Y%m%d').date()) for p in points] | |
24 | ||
25 | for point in reversed(points): | |
26 | if point.daystamp == TODAY: | |
27 | goal_metadata[goal['title']] = goal_metadata[goal['title']]._replace(today_count=point.value + goal_metadata[goal['title']].today_count) | |
28 | if point.daystamp >= WEEK_AGO: | |
29 | goal_metadata[goal['title']] = goal_metadata[goal['title']]._replace(week_count=point.value + goal_metadata[goal['title']].week_count) | |
30 | - | elif point.daystamp >= TWO_WEEKS_AGO and goal['initday'] < TWO_WEEKS_AGO_EPOCH: |
30 | + | elif point.daystamp >= TWO_WEEKS_AGO: |
31 | goal_metadata[goal['title']] = goal_metadata[goal['title']]._replace(prior_week_count=point.value + goal_metadata[goal['title']].prior_week_count) |