Created
January 5, 2016 20:57
-
-
Save jnnt/20933b1cff2c0bc06dd0 to your computer and use it in GitHub Desktop.
Export script for RescueTime data. Saves user's activity data in a CSV file for every month between START_DATE and END_DATE, with the highest available resolution (minutes).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#-*- coding: utf-8 -*- | |
# RescueTime Data Export | |
import os | |
import requests | |
from datetime import date, datetime | |
from dateutil.rrule import rrule, MONTHLY | |
from dateutil.parser import parse | |
from itertools import izip | |
API_URL = "https://www.rescuetime.com/anapi/data" | |
API_KEY = "api_key" | |
DUMP_DIR = "rescuetime_dump" | |
FILE_PREFIX = "rt" | |
START_DATE = '2012-01-01' | |
END_DATE = '2015-12-31' | |
def get_rt_data(): | |
dates = iter(list(rrule( | |
MONTHLY, | |
bymonthday=(-1,1,), | |
dtstart=parse(START_DATE), | |
until=parse(END_DATE)))) | |
for (month_start, month_end) in izip(dates, dates): | |
print "Getting Data for Interval", month_start, "to", month_end | |
month_start = month_start.date() | |
month_end = month_end.date() | |
response = requests.get(API_URL, { | |
'key': API_KEY, | |
'perspective': 'interval', | |
'format': 'csv', | |
'restrict_begin': month_start.isoformat(), | |
'restrict_end': month_end.isoformat(), | |
'resolution_time': 'minute' | |
}) | |
file_name = ( | |
FILE_PREFIX + | |
month_start.strftime('%Y%m%d') + | |
"-" + | |
month_end.strftime('%Y%m%d') + | |
".csv" | |
) | |
file_path = os.path.join(DUMP_DIR, file_name) | |
if not os.path.exists(DUMP_DIR): | |
os.mkdir(DUMP_DIR) | |
with open(file_path, 'w') as f: | |
f.write(response.text.encode('utf-8')) | |
print "Data Saved to", file_path | |
print "" | |
if __name__ == '__main__': | |
get_rt_data() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment