Last active
September 8, 2020 17:27
-
-
Save hardillb/4754527 to your computer and use it in GitHub Desktop.
Blink(1)/Digispark+RGB Meeting warning light. A script to find your next appointment in your Google Calendar and change the colour of a your device depending on how much time before it starts. Moving from green at 20min (or more) to red in the last 5 mins before a meeting. The light turns blue for the duration of the meeting.
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
#!/usr/bin/python | |
import sys | |
import getopt | |
from array import array | |
import pycurl | |
import subprocess | |
import StringIO | |
from icalendar import Calendar, Event | |
from datetime import datetime, timedelta | |
def main(argv): | |
try: | |
opts, args = getopt.getopt(argv,"bdhu:f:") | |
except getopt.GetoptError: | |
print 'icsBlink.py -u <URL for calendar>|-f <path to ics file> -b|-d' | |
print '-u <URL> - URL pointing to Google calendar' | |
print '-f <File> - ICS file' | |
print '-b - Use Blink(1)' | |
print '-d - Use Digispark + RGB' | |
sys.exit(2) | |
out = 'digi' | |
rgb = array('i',[0,200,0]) | |
for opt, arg in opts: | |
if opt == '-h': | |
print 'icsBlink.py -u <URL for calendar>|-f <path to ics file> -b|-d' | |
print '-u <URL> - URL pointing to Google calendar' | |
print '-f <File> - ICS file' | |
print '-b - Use Blink(1)' | |
print '-d - Use Digispark + RGB' | |
sys.exit(1) | |
elif opt == '-u': | |
curl = pycurl.Curl() | |
curl.setopt(pycurl.URL,arg) | |
body = StringIO.StringIO() | |
curl.setopt(pycurl.WRITEFUNCTION, body.write) | |
curl.perform() | |
cal = Calendar.from_ical(body.getvalue()) | |
body.close() | |
elif opt == '-f': | |
cal = Calendar.from_ical(open(arg,'rb').read()) | |
elif opt == '-d': | |
out = 'digi' | |
elif opt == '-b': | |
out = 'blink' | |
now = datetime.now() | |
offset = 3600 | |
for component in cal.walk(): | |
if component.name == "VEVENT": | |
dtstart = component["dtstart"].to_ical() | |
dtend = component["dtend"].to_ical() | |
if "T" in dtstart: | |
dt = datetime.strptime(dtstart, "%Y%m%dT%H%M%SZ") | |
end = datetime.strptime(dtend, "%Y%m%dT%H%M%SZ") | |
if dt > now: | |
delta = dt - now | |
if delta.days == 0: | |
if delta.seconds < offset: | |
offset = delta.seconds | |
elif end > now and dt < now: | |
offset = -1 | |
break | |
if offset > 0 and offset < 300: | |
rgb[0] = 200 | |
rgb[1] = 0 | |
rgb[2] = 0 | |
elif offset >= 300 and offset < 600: | |
rgb[0] = 150 | |
rgb[1] = 50 | |
rgb[2] = 0 | |
elif offset >= 600 and offset < 900: | |
rgb[0] = 100 | |
rgb[1] = 100 | |
rgb[2] = 0 | |
elif offset >= 900 and offset < 1200: | |
rgb[0] = 50 | |
rgb[1] = 150 | |
rgb[2] = 0 | |
elif offset == -1: | |
rgb[0] = 0 | |
rgb[1] = 0 | |
rgb[2] = 200 | |
else: | |
rgb[0] = 0 | |
rgb[1] = 200 | |
rgb[2] = 0 | |
if out == 'blink': | |
subprocess.call(['blink1-tool','--rgb','{0},{1},{2}'.format(rgb[0],rgb[1],rgb[2])]) | |
elif out == 'digi': | |
subprocess.call(['DigiRGB.py','{0}'.format(rgb[0]),'{0}'.format(rgb[1]),'{0}'.format(rgb[2])]) | |
if __name__ == "__main__": | |
main(sys.argv[1:]) |
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
Add the following your crontab to have the script check your calendar every 5 mins | |
*/5 * * * * /path/to/icsBlink.py <calendar URL> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment