def dump(dump_dir, year, month, verbose=False): time = datetime(year=year, month=month, day=1) month_start = get_month_start(time) month_end = get_month_end(time) month = '%d-%02d' % (year, month) if verbose: print(month + ':', ) print(month_start.isoformat(), 'to', month_end.isoformat(), '...') toggl = api.TogglAPI(API_TOKEN, TIMEZONE) data = toggl.get_time_entries(month_start.isoformat(), month_end.isoformat()) content = json.dumps(data) if not os.path.exists(dump_dir): os.makedirs(dump_dir) filename = os.path.join(dump_dir, '%s.json' % month) def md5(string): return hashlib.md5(string).digest() if os.path.exists(filename) and md5(content) == md5(open(filename).read()): if verbose: print(filename, 'not changed.') else: file(filename, 'w').write(content) if verbose: print(filename, 'saved.') return data
def main(): w = workingtime.WorkingTime(config.WORKING_HOURS_PER_DAY, config.BUSINESS_DAYS, config.WEEK_DAYS) a = api.TogglAPI(config.API_TOKEN, config.TIMEZONE) t = target.Target() print("Hi") print("Checking Internet connectivity...") if not internet_on(): print("OMG! There is no internet connection!") print("Good Bye Cruel World!") sys.exit() print("Internet seems fine!") print("\nTrying to connect to Toggl, hang on!\n") try: t.achieved_hours = a.get_hours_tracked(start_date=w.month_start, end_date=w.now) except: print("OMG! Toggle request failed for some mysterious reason!") print("Good Bye Cruel World!") sys.exit() t.required_hours = w.required_hours_this_month t.tolerance = config.TOLERANCE_PERCENTAGE normal_min_hours, crunch_min_hours = t.get_minimum_daily_hours( w.business_days_left_count, w.days_left_count) print("So far this month, you have tracked", ) print(hilite("{0:.2f} hours".format(t.achieved_hours), True, True)) print("\nBusiness days left till deadline : {}".format( w.business_days_left_count)) print("Total days left till deadline : {}".format(w.days_left_count)) print("\nThis month targets [Required (minimum)] : {} ({})".format( w.required_hours_this_month, w.required_hours_this_month - (w.required_hours_this_month * config.TOLERANCE_PERCENTAGE))) print( "\nTo achieve the minimum:\n\tyou should log {0:.2f} hours every business day" .format(normal_min_hours)) print("\tor log {0:.2f} hours every day".format(crunch_min_hours)) print("\tleft is : {0:.2f}".format( (w.required_hours_this_month - (w.required_hours_this_month * config.TOLERANCE_PERCENTAGE)) - t.achieved_hours)) normal_required_hours, crunch_required_hours = t.get_required_daily_hours( w.business_days_left_count, w.days_left_count) print( "\nTo achieve the required :\n\tyou should log {0:.2f} hours every business day" .format(normal_required_hours)) print("\tor log {0:.2f} hours every day".format(crunch_required_hours)) print("\tleft is : {0:.2f}".format(w.required_hours_this_month - t.achieved_hours)) print("\nHow your progress looks:") bar = percentile_bar(t.achieved_percentage, config.TOLERANCE_PERCENTAGE) print(bar)
#!/usr/bin/env python# encoding: utf8 import hashlibimport jsonimport osfrom datetime import datetime, timedelta from togglapi import api from config import API_TOKEN, TIMEZONE START_MONTH = '2015-03' def get_month_start(time): return time.replace(day=1, hour=0, minute=0, second=0, microsecond=0) def get_month_end(time): month_start = get_month_start(time) next_month_start = (month_start + timedelta(days=31)).replace(day=1) return next_month_start - timedelta(seconds=1) def dump(dump_dir, year, month, verbose=False): time = datetime(year=year, month=month, day=1) month_start = get_month_start(time) month_end = get_month_end(time) month = '%d-%02d' % (year, month) if verbose: print month + ':', print month_start.isoformat(), 'to', month_end.isoformat(), '...' toggl = api.TogglAPI(API_TOKEN, TIMEZONE) data = toggl.get_time_entries(month_start.isoformat(), month_end.isoformat()) content = json.dumps(data) if not os.path.exists(dump_dir): os.makedirs(dump_dir) filename = os.path.join(dump_dir, '%s.json' % month) def md5(string): return hashlib.md5(string).digest() if os.path.exists(filename) and md5(content) == md5(open(filename).read()): if verbose: print filename, 'not changed.' else: file(filename, 'w').write(content) if verbose: print filename, 'saved.' return data def backup(backup_dir, verbose=False): time = datetime.now() while True: month = '%d-%02d' % (time.year, time.month) if month < START_MONTH: break dump(backup_dir, time.year, time.month, verbose=verbose) prev_month = (time - timedelta(days=31)).replace(day=1) time = prev_month if __name__ == '__main__': import sys if len(sys.argv) == 1: print 'Usage: %s backup_dir [-v]' % sys.argv[0] sys.exit(1) try: verbose = sys.argv[2] == '-v' except: verbose = False backup_dir = sys.argv[1] backup(backup_dir, verbose=verbose)