Skip to content

Instantly share code, notes, and snippets.

@AlexLamson
Last active May 6, 2020 19:08
Show Gist options
  • Save AlexLamson/59554e9e9a521bf4460906c39951367a to your computer and use it in GitHub Desktop.
Save AlexLamson/59554e9e9a521bf4460906c39951367a to your computer and use it in GitHub Desktop.
A simple python script to backup rescuetime data every month
#!/usr/bin/python3
# backup-rescuetime.py: Backup all of last month's rescuetime data
#
# Columns are Date,Time Spent (seconds),Number of People,Activity,Category,Productivity
# Rows are hourly breakdowns of the activities done
#
# Must have directory called 'rescuetime_data' in same directory as script
#
# Sample crontab entry to backup at 6AM on the 1st of every month:
# 0 6 1 * * python3 ~/rescuetime/backup-rescuetime.py
#
# You must also create an API key to use the script.
# Scroll to the bottom of https://www.rescuetime.com/anapi/manage
# Make the key in the form and copy it into the string below
api_key = 'YOUR_KEY_GOES_HERE'
# determine the strings for getting last month's data
import datetime
now = datetime.datetime.now()
if now.month == 1:
get_m = 12
get_y = now.year-1
else:
get_m = now.month-1
get_y = now.year
start_time = '{:04}-{:02}-{:02}'.format(get_y, get_m, 1)
end_time = '{:04}-{:02}-{:02}'.format(now.year, now.month, 1)
# get the data
from urllib.request import urlopen
body=[
'key={}'.format(api_key),
'by=interval',
'i=minute',
'restrict_begin={}'.format(start_time),
'restrict_end={}'.format(end_time),
'format=csv'
]
response = urlopen('https://www.rescuetime.com/anapi/data?{}'.format('&'.join(body)))
# write it to a file
with open('rescuetime_data/data-{}.csv'.format(start_time), 'w+b') as f:
for i, line in enumerate(response.readlines()):
f.write(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment