Last active
April 30, 2022 20:44
-
-
Save hunterhector/283066a8d317d7a55f8a98c7060f07aa to your computer and use it in GitHub Desktop.
Present RescueTime's productivity output as a pie chart, could be used in stuff like geektools.
This file contains 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
import json | |
import urllib.request | |
import urllib.parse | |
import sys | |
import datetime | |
import os | |
output_dir = sys.argv[1] | |
apikey = sys.argv[2] | |
date = datetime.datetime.today().strftime('%Y-%m-%d') | |
url = 'https://www.rescuetime.com/anapi/data?key={api:s}&perspective=rank&restrict_kind=productivity&restrict_begin={begin:s}&restrict_end={end:s}&format=json'.format(api=apikey, begin=date, end=date) | |
f = urllib.request.urlopen(url) | |
data = json.loads(f.read()) | |
product_map = { | |
0 : 'Neutral', | |
1 : 'Productive', | |
2 : 'Very Productive', | |
-1: 'Distracting', | |
-2: 'Very Distracting', | |
} | |
weights = { | |
'Neutral': 50, | |
'Productive': 75, | |
'Very Productive': 100, | |
'Distracting': 25, | |
'Very Distracting': 0, | |
} | |
if not data['rows']: | |
sys.exit(1) | |
product = dict([(product_map[prod], seconds * 1.0 / 60) for rank, seconds, num, prod in data['rows']]) | |
labels = ['Very Distracting','Distracting', 'Neutral', 'Productive', 'Very Productive'] | |
sizes = [product.get(key, 0) for key in labels] | |
# Comput pulse. | |
prod_sum = sum(product.values()) | |
percents = dict([(k,p/prod_sum) for k, p in product.items()]) | |
pulse = int(round(sum([percents.get(name, 0) * weight for name, weight in weights.items()]))) | |
import matplotlib | |
matplotlib.use('agg') | |
import matplotlib.pyplot as plt | |
# Pie chart | |
colors = ['#D71600', '#DD675A', '#B1C1BF', '#3F7DDD', '#0C50BF'] | |
circle_size = 0.5 | |
pct_dist = 0.7 | |
explosion = 0.0 | |
pulse_size = 75 | |
label_dist = 1.1 | |
label_size = 0 | |
auto_size = 18 | |
#pulse_color = '#ED7E36' | |
if pulse < 60: | |
pulse_color = 'red' | |
else: | |
pulse_color = 'black' | |
circle_color = '#d9e1fc' | |
def make_autopct(values): | |
def my_autopct(pct): | |
total = sum(values) | |
val = pct*total/100.0 | |
hours = int(val / 60) | |
minutes = int(val % 60) | |
time = '{h:d} Hr \n{m:d} Min'.format(h=hours,m=minutes) if hours else '{m:d} Min'.format(m=minutes) | |
return '{p:.0f}% \n{t:s}'.format(p=pct,t=time) if pct > 10 else '' | |
return my_autopct | |
explode = [explosion] * 5 | |
fig1, ax1 = plt.subplots() | |
labels = None | |
patches, texts, autotexts = ax1.pie(sizes, colors = colors, labels=labels, autopct=make_autopct(sizes), labeldistance=label_dist, pctdistance=pct_dist, startangle=90, explode=explode, rotatelabels=False) | |
#draw circle | |
centre_circle = plt.Circle((0,0),circle_size,fc=circle_color) | |
fig = plt.gcf() | |
fig.gca().add_artist(centre_circle) | |
plt.text(0, 0, pulse, ha='center', va='center', size=pulse_size, color=pulse_color) | |
for text in texts: | |
text.set_color('black') | |
text.set_size(label_size) | |
for autotext in autotexts: | |
autotext.set_color('black') | |
autotext.set_size(auto_size) | |
# Equal aspect ratio ensures that pie is drawn as a circle | |
ax1.axis('equal') | |
plt.tight_layout() | |
plt.savefig(os.path.join(output_dir, 'chart.png'), transparent=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Requirements:
Usage:
python ~/.geektool/Rescue/rescue_pie.py [script directory] [your RescueTime api key]
This will create a chart image at the script directory, using your API key: "~/.geektool/Rescue/chart.png"
a. you could either browse to it via "set local path"
b. or if you are comfortable to mac's local file URL format, it is simply: "file://localhost/[full path to the image source]"
Notes:
a. If you use Python 2.7, you could change urllib.request.urlopen to urllib2.urlopen, see this StackOverflow post
b. If you use Python 2.7, you should consider an upgrade to 3.
a. For example, some version does not support "rotatelabels" attribute.
b. I am not sure how it will looks like after removal.