Skip to content

Instantly share code, notes, and snippets.

@dreamalligator
Last active April 27, 2017 07:01
Show Gist options
  • Save dreamalligator/83cf267bf4799b5c6dad to your computer and use it in GitHub Desktop.
Save dreamalligator/83cf267bf4799b5c6dad to your computer and use it in GitHub Desktop.
Script to import your Google calendar into Orage.
url.txt
*.pyc
from crontab import CronTab
HRS = None # How many hours to periodically update
MIN = 2
ON_REBOOT = False # Or only perform on reboot (my current preference)
#cron = CronTab(tab='')
#cron = CronTab(user='tom')
cron = CronTab(user=True)#unix-only
cmd = '/usr/bin/python ~/projects/83cf267bf4799b5c6dad/orage.py' #test with `/usr/bin/firefox` or such
job = cron.new(command=cmd,comment='Sync Orage with Google calendar')
if(ON_REBOOT): # only on reboot
job.every_reboot()
else: # or by HRS specified above
if(HRS!=None):
job.hour.every(HRS)
if(MIN!=None):
job.minute.every(MIN)
cron.write()
print cron.render()
job.enable()
if(True==job.is_valid()):
print('Sync set up! (or at least valid) \ncommand: {0}\n{1}'.format(job.command,job.comment))
else:
print('Cron setup failed (not valid):(\nGoogle calendar not synced.')
print('printing all jobs ...')
for job in cron:
print job
import sys
PY3K = sys.version_info >= (3, 0)
if PY3K:
import urllib.request as urllib
else:
import urllib
import wget, getpass, os, shutil
with open('url.txt') as u:
url = u.read().strip()
USERNAME = getpass.getuser()
url = urllib.unquote(url).decode('utf8') # Convert HTML URL Encoding (%40 --> @)
# If your preference is to set it as aforeign file, you have to enable dbus and do `orage -a /home/[username]/calendar.ics`
# output = '/home/'+USERNAME+'/calendar.ics' if USE_ICAL else '/home/'+USERNAME+'/calendar'
output = '/home/'+USERNAME+'/.local/share/orage/orage.ics'
file = wget.download(url,output)
if os.path.exists(output): # Overwrite file if already exists
shutil.move(file,output)
import getpass
from icalendar import Calendar, Event
from datetime import datetime
wages = [0,0]
this_month = datetime.now().month
if this_month == 12:
next_month = 1
else:
next_month = this_month + 1
wage = 40.00
USERNAME = getpass.getuser()
f = open('/home/'+USERNAME+'/.local/share/orage/orage.ics','r')
gcal = Calendar.from_ical(f.read())
for component in fcal.walk():
if component.name == "VEVENT" and (component.get('summary').startswith('ET') or component.get('summary').startswith('Expert Tutoring')):
summary = component.get('summary')
start = component.get('dtstart').dt
end = component.get('dtend').dt
timedelta = end - start
timespent = timedelta.seconds / 3600.00
earning = wage * timespent
#print('{0}, {1}hrs, ${2:.2f}'.format(summary,timespent,earning))
if start.month == this_month:
wages[0] = wages[0] + earning
elif start.month == next_month:
wages[1] = wages[1] + earning
f.close()
print('your projected earning this month is ${0:.2f}, and next month you project ${1:.2f}'.format(wages[0],wages[1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment