forked from Ludo6431/sleepbot2ical
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsleep_as_android2ical.py
190 lines (138 loc) · 5.61 KB
/
sleep_as_android2ical.py
1
# -*- coding: utf-8 -*-# script by Ludovic Lacoste <[email protected]>#Id,Tz,From,To,Sched,Hours,Rating,Comment,Framerate,Snore,Noise,Cycles,Eventfrom datetime import datetimeimport urllib2import shutilimport urlparseimport osdef readSB(f): sleeps = list() keys = f.readline() keys = keys.split(',') #set to make sure we don't repeat stuff unique_sleeps = set() while True: line = f.readline() if not line: break #data rows alwys start with a timestamp such as 1406218286546 if line.startswith('"1'): values = line.split(',') d = dict(zip(keys, values)) # processing certain keys d["Comment"] = d["Comment"].strip('"').strip('#home').strip('#newmoon').strip('manually added') d["Rating"] = d["Rating"].strip('"') if (d["Id"] in unique_sleeps): print "duplicate sleepcloud entry: " + d["Id"] else: sleeps.append(d) unique_sleeps.add(d["Id"]) return sleepsdef sleep2dates(sleep): sleep_stop = datetime.strptime(sleep['To'], '"%d. %m. %Y %H:%M"') sleep_start = datetime.strptime(sleep['From'], '"%d. %m. %Y %H:%M"') return [sleep_start, sleep_stop]#return true if contains midnightdef containsMidnight(dts0, dts1): if (dts0.hour > dts1.hour): return True else: return Falsedef writeIcal(sleeps, f, cleanFlag = False, htmlFlag = False): from icalendar import Calendar, Event import md5 cal = Calendar() # header data cal.add('calscale', 'Gregorian') cal.add('method', 'publish') cal.add('x-wr-caldesc', "iCal feed from sleep as android data. see https://github.com/stevenqzhang/sleepasandroid2ical") cal.add('prodid', 'calendar') cal.add('version', '3.0') cal.add('X-WR-CALNAME', 'Sleep As Android Import') cal.add('X-WR-TIMEZONE', 'America/Los_Angeles') summary_fmt = '{Comment} ' description_fmt = 'Rating {Rating} \n \n Made with github.com/stevenqzhang/sleepasandroid2ical' for sleep in sleeps: dts = sleep2dates(sleep) event = Event() # print "cleanFlag: " + str(cleanFlag) if cleanFlag == False: event.add('summary', summary_fmt.format(**sleep)) event.add('description', description_fmt.format(**sleep)) else: event.add('description', "blank") event.add('summary', "blank") event.add('dtstart', dts[0]) event.add('dtend', dts[1]) event['uid'] = md5.new(str(dts[0])+'SleepBot'+str(dts[1])).hexdigest() cal.add_component(event) print "added event" if (htmlFlag): f.write("Made with <a href = 'goo.gl/1psE1m'> https://github.com/stevenqzhang/sleepasandroid2ical </a> </br>") lastDtendDay = -1 for event in cal.subcomponents: start = event['dtstart'].dt stop = event['dtend'].dt #todo fix this boundary by actually grouping all times by days... # define day boundary, if(start.day != lastDtendDay): f.write("================" +start.strftime("%m-%d-%Y") + "=============<br/>") lastDtendDay = stop.day if "nap" in str(event['summary']): pass # if sleep start is before midnight, and sleep end is after midnight, there should be ... containsMidight = containsMidnight(start, stop) if(containsMidight): f.write("<font color='green'> GOOD! <br/> ") f.write("sleep start" + start.strftime('%X') + "<br/> sleep end:" + stop.strftime('%X') + "<br/><br/>") #print ("sleep start" + start.strftime('%X') + "<br/> sleep end:" + stop.strftime('%X') + "<br/><br/>") #print start.strftime('%x') print "wrote record starting at " + start.strftime('%X') if(containsMidight): f.write("</font>") print "Finished writing HTML" else: f.write(cal.to_ical()) print "Finished writing iCal" f.close()# from http://stackoverflow.com/a/2067142/1621636def download(url, fileName=None): def getFileName(url,openUrl): if 'Content-Disposition' in openUrl.info(): # If the response has Content-Disposition, try to get filename from it cd = dict(map( lambda x: x.strip().split('=') if '=' in x else (x.strip(),''), openUrl.info()['Content-Disposition'].split(';'))) if 'filename' in cd: filename = cd['filename'].strip("\"'") if filename: return filename # if no filename was found above, parse it out of the final URL. return os.path.basename(urlparse.urlsplit(openUrl.url)[2]) r = urllib2.urlopen(urllib2.Request(url)) try: fileName = fileName or getFileName(url,r) with open(fileName, 'wb') as f: shutil.copyfileobj(r,f) finally: r.close()if __name__ == "__main__": import sys import argparse parser = argparse.ArgumentParser() parser.add_argument('--cleancomments', required=false) #html, ical, or csv parser.add_argument('--outtype', required=true) parser.add_argument('--csv_file', required=true) args = parser.parse_args() download(sys.argv[1], "temp_csv.txt") csvfile = open("temp_csv.txt", 'rb') sleeps = readSB(csvfile) icalfile = open(sys.argv[2], 'wb') cleanFlag = False if sys.argv[3] == "-c": cleanFlag = True #todo get these to parse with blanks htmlFlag = False if sys.argv[4] == "-p": htmlFlag = True writeIcal(sleeps, icalfile, cleanFlag, htmlFlag)