Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import mysql.connector
- import urllib2
- import os
- def createFilepath(id):
- if not os.path.exists('/var/www/html/ics'):
- os.makedirs('/var/www/html/ics')
- fileName = "calendar_"
- fileName += str(id)
- fileName += '.ics'
- return os.path.join('/var/www/html/ics', fileName)
- def flushD():
- global fileContent, event
- for key in courses:
- if key in event:
- fileContent += str(event)
- break
- for key in searchterms:
- if key in event:
- fileContent += str(event)
- break
- event = ""
- def getFromUrl(url):
- response = urllib2.urlopen(url)
- text = response.read()
- lines = text.splitlines()
- return lines
- def filterLines(lines):
- global event
- for line in lines:
- if "BEGIN:VEVENT" in line:
- clearEvent()
- elif "SUMMARY" in line:
- line += "; " + groups[currentGroup]
- event += line + "\n"
- if "END:VEVENT" in line or "END:VCALENDAR" in line:
- flushD()
- def clearEvent():
- global event
- event = ""
- cnx = mysql.connector.connect(user='uhasselt', password='', host='localhost', database='uhasselt')
- cursor = cnx.cursor()
- query = ("SELECT * FROM calendars")
- cursor.execute(query)
- calendars = cursor.fetchall()
- for (id, name, user_id) in calendars:
- filepath = createFilepath(id)
- f = open(filepath, "w")
- groups = []
- urls = []
- courses = []
- searchterms = []
- event = ""
- currentGroup = 0
- fileContent = "BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-//uhasselt/lesroosters//NONSGML v1.0//EN\nMETHOD:PUBLISH\nCALSCALE:GREGORIAN\nMETHOD:PUBLISH\nX-WR-CALNAME:%s\nX-WR-TIMEZONE:Europe/Brussels\nX-WR-CALDESC:\nX-PUBLISHED-TTL:PT12H\n" % name
- queryUrls = ("SELECT * FROM urls WHERE calendar = %s" % id)
- queryCourses = ("SELECT * FROM courses WHERE calendar = %s" % id)
- cursor.execute(queryUrls)
- urlData = cursor.fetchall()
- cursor.execute(queryCourses)
- courseData = cursor.fetchall()
- for(id, data, calendar, name) in urlData:
- urls.append(data.encode("ascii"))
- groups.append(name.encode("ascii"))
- for(id, data, calendar, name) in courseData:
- courses.append(data.encode("ascii"))
- searchterms.append(name.encode("ascii"))
- for url in urls:
- try:
- lines = getFromUrl(url)
- filterLines(lines)
- currentGroup += 1
- except:
- pass
- fileContent += "END:VCALENDAR"
- f.write(fileContent)
- f.close()
- cursor.close()
- cnx.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement