First commit

This commit is contained in:
giuliof 2018-11-04 18:58:16 +01:00
parent 70d5ce2ab5
commit 0efa94c242
5 changed files with 179 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
config.py
.directory

7
.htaccess Normal file
View File

@ -0,0 +1,7 @@
SetEnv LANG en_US.UTF-8
Options +ExecCGI
AddHandler cgi-script .py
<Files "config.py">
Order Allow,Deny
Deny from all
</Files>

8
config.py Normal file
View File

@ -0,0 +1,8 @@
UNIX_SOCKET=''
HOST=''
USER=''
PASSWORD=''
DB=''
CALENDAR_NAME=''

111
getInfo.py Executable file
View File

@ -0,0 +1,111 @@
#!/usr/bin/python3
import mysql.connector as sql
import datetime as dt
import sys
import re
from config import *
def parseOptions(arr):
if len(arr) == 1:
return {'RAW':arr}
else:
options = {}
for i in arr:
k = i.split('=')
if len(k) == 1:
options['RAW'] = k[0]
else:
options[k[0]] = k[1]
return options
def getEvents(baseDay, interval):
# Calculate timestamp of previous and following monday
if interval == 'week':
leftLimit = (baseDay+dt.timedelta(days=-baseDay.weekday(), weeks=1)).strftime('%s')
elif interval == '4weeks':
leftLimit = (baseDay+dt.timedelta(days=-baseDay.weekday(), weeks=4)).strftime('%s')
else:
raise ValueError('Invalid argument passed to getEvents')
rightLimit = (baseDay-dt.timedelta(days=baseDay.weekday())).strftime('%s')
c = sql.connect(unix_socket=UNIX_SOCKET, host=HOST, user=USER, password=PASSWORD, db=DB)
mycursor = c.cursor()
# For repeated events (not yet supported)
#sql = "SELECT obj.calendardata FROM oc_calendarobjects AS obj INNER JOIN oc_calendars AS cal ON obj.calendarid = cal.id WHERE cal.displayname='prova'AND obj.firstoccurence < %s AND obj.lastoccurence > %s" % (leftLimit, rightLimit)
query = "SELECT obj.calendardata FROM oc_calendarobjects AS obj INNER JOIN oc_calendars AS cal ON obj.calendarid = cal.id WHERE cal.displayname='%s' AND obj.firstoccurence < %s AND obj.firstoccurence > %s" % (CALENDAR_NAME,leftLimit, rightLimit)
mycursor.execute(query)
result = mycursor.fetchall()
c.close()
events = []
for event in result:
#print (event)
event = event[0].decode('utf8') # i selected only first column
#print (event)
blockParsing = None
event_dict = {}
for item in event.split('\r\n'):
try:
k,v = item.split(':',1)
except:
continue
k = k.split(';')
v = re.split('(?<!\\\\);',v)
k += v
# Does not work well with nested blocks
# but nobody cares
if k[0] == "BEGIN":
blockParsing = k[1]
elif k[0] == "END":
blockParsing = None
else:
if blockParsing == "VEVENT":
if k[0] == 'SUMMARY':
# save event name
event_dict['NAME'] = k[1]
#print("Event Name: %s" % event_dict['NAME'])
elif k[0] == 'DTSTART':
# check if len(k) > 2
# if yes, date has a not-datetime format
# (i.e. only date for all-day events).
options = parseOptions(k[1:])
#print("Options %s" % options)
if 'VALUE' in options and options['VALUE'] == 'DATE':
event_dict['DATETIME'] = dt.datetime.strptime(options['RAW'],'%Y%m%d')
event_dict['ALLDAY'] = True
else:
event_dict['DATETIME'] = dt.datetime.strptime(options['RAW'],'%Y%m%dT%H%M%S')
event_dict['ALLDAY'] = False
#print("StartDate: %s" % event_dict['DATETIME'])
elif k[0] == 'RRULE':
continue
events += [event_dict]
# Thanks stackoverflow
# Return events sorted by date, AllDay first
return sorted(events, key=lambda k: "%s %d" % (k['DATETIME'],k['ALLDAY'] == 0))
# Check if this file is executed as main file or included
######## SHOW DATA #######
if __name__ == '__main__':
print("ZERo Optimized CALdav CAlendar Reader Engine")
print("--------------------------------------------")
events = getEvents(dt.datetime.today(),'week')
for event in events:
try:
print ("Event Name: %s" % event['NAME'])
print ("Event Date: %s" % event['DATETIME'].date())
if event['ALLDAY']:
print("All Day")
else:
print ("Event Time: %s" % event['DATETIME'].time())
except :
print("Malformed event")
print("----------------")

51
main.py Normal file
View File

@ -0,0 +1,51 @@
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import xml.etree.ElementTree as ET
import sys
import cgitb, cgi
import datetime as dt
import locale
from getInfo import *
locale.setlocale(locale.LC_TIME, 'it_IT')
# Start CGI handling for webserver
cgitb.enable()
inputvars = cgi.FieldStorage()
print('Content-Type: text/html; charset=utf-8')
print()
### End of HTTP headers: it is now safe to output things
##########################################################
tree = ET.ElementTree()
main = ET.Element('main')
tree._setroot(main)
# get BaseDate and interval
interval = 'week' if 'interval' not in inputvars else inputvars['interval'].value
basedate = dt.date.today() if 'basedate' not in inputvars else dt.datetime.strptime(inputvars['basedate'].value, '%Y-%m-%d')
for event in getEvents(basedate, interval):
name = event['NAME']
article = ET.Element('article')
h1 = ET.Element('h1')
span = ET.Element('span')
h1.text = name
span.text = event['DATETIME'].strftime('%-d %B %Y')
if not (event['ALLDAY']):
span.text += ' alle ' + event['DATETIME'].strftime('%-I:%-M')
article.append(h1)
article.append(span)
main.append(article)
htmlString = ET.tostring(tree.getroot(), encoding='unicode', method='html')
print(htmlString)