Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.29 KB | None | 0 0
  1. import mysql.connector
  2. import urllib2
  3. import os
  4.  
  5. def createFilepath(id):
  6.     if not os.path.exists('/var/www/html/ics'):
  7.         os.makedirs('/var/www/html/ics')
  8.  
  9.     fileName = "calendar_"
  10.     fileName += str(id)
  11.     fileName += '.ics'
  12.     return os.path.join('/var/www/html/ics', fileName)
  13.  
  14. def flushD():
  15.     global fileContent, event
  16.  
  17.     for key in courses:
  18.         if key in event:
  19.             fileContent += str(event)
  20.             break
  21.  
  22.     for key in searchterms:
  23.         if key in event:
  24.             fileContent += str(event)
  25.             break
  26.  
  27.     event = ""
  28.  
  29. def getFromUrl(url):
  30.     response = urllib2.urlopen(url)
  31.     text = response.read()
  32.     lines = text.splitlines()
  33.     return lines
  34.  
  35. def filterLines(lines):
  36.     global event
  37.  
  38.     for line in lines:
  39.         if "BEGIN:VEVENT" in line:
  40.             clearEvent()
  41.         elif "SUMMARY" in line:
  42.             line += "; " + groups[currentGroup]
  43.         event += line + "\n"
  44.  
  45.         if "END:VEVENT" in line or "END:VCALENDAR" in line:
  46.             flushD()
  47.  
  48. def clearEvent():
  49.     global event
  50.     event = ""
  51.  
  52. cnx = mysql.connector.connect(user='uhasselt', password='', host='localhost', database='uhasselt')
  53. cursor = cnx.cursor()
  54. query = ("SELECT * FROM calendars")
  55. cursor.execute(query)
  56. calendars = cursor.fetchall()
  57. for (id, name, user_id) in calendars:
  58.     filepath = createFilepath(id)
  59.     f = open(filepath, "w")
  60.  
  61.     groups = []
  62.     urls = []
  63.     courses = []
  64.     searchterms = []
  65.     event = ""
  66.     currentGroup = 0
  67.     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
  68.  
  69.     queryUrls = ("SELECT * FROM urls WHERE calendar = %s" % id)
  70.     queryCourses = ("SELECT * FROM courses WHERE calendar = %s" % id)
  71.  
  72.     cursor.execute(queryUrls)
  73.     urlData = cursor.fetchall()
  74.  
  75.     cursor.execute(queryCourses)
  76.     courseData = cursor.fetchall()
  77.  
  78.     for(id, data, calendar, name) in urlData:
  79.         urls.append(data.encode("ascii"))
  80.         groups.append(name.encode("ascii"))
  81.  
  82.     for(id, data, calendar, name) in courseData:
  83.         courses.append(data.encode("ascii"))
  84.         searchterms.append(name.encode("ascii"))
  85.  
  86.     for url in urls:
  87.         try:
  88.             lines = getFromUrl(url)
  89.             filterLines(lines)
  90.             currentGroup += 1
  91.         except:
  92.             pass
  93.  
  94.     fileContent += "END:VCALENDAR"
  95.  
  96.     f.write(fileContent)
  97.     f.close()
  98.  
  99. cursor.close()
  100. cnx.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement