from pyquery import PyQuery as pq
from icalendar import Calendar, Event
import datetime
from datetime import date, timedelta
from dateutil.relativedelta import *
from dateutil.parser import *
from pprint import pprint as pp
clubid = 722
url = "https://www.lafitness.com/Pages/ClassSchedulePrintVersion.aspx?clubid=%s" % clubid
d = pq(url=url)
elements = d("#tblSchedule > tr > td")
# Calculate some important variables.
now = datetime.datetime.now()
hours = [x.text.replace('\t','').replace('\r\n','') for x in elements.find('h5')]
location = d("td[valign=middle]").text().replace('\t','').replace('\r\n',' ')
classes = []
for ele in elements:
cur = []
for e in ele.findall("b"):
print e.getchildren()[0].text.encode('ascii','ignore')
try:
print cur
cur.extend([e.getchildren()[0].text.encode('ascii','ignore')])
except:
pass
classes.append(cur)
print pp(classes)
# Restructure Classes
rclass = []
for x in range(0,len(classes),8):
rclass.append(classes[x+1:x+8])
cal = Calendar()
cal.add('prodid', '-//LA Fitness//mxm.dk//')
cal.add('version', '2.0')
cal.add('dtstart', now-timedelta(days=7))
i = 0
for time in enumerate(hours):
for f,dayset in enumerate(rclass[time[0]]):
if dayset != []:
for day in dayset:
i += 1
event = Event()
event.add('summary',day)
event['uid'] = "lafitness" + str(clubid) + "_" + str(i)
t = parse(time[1])
event.add('dtstart', datetime.datetime.combine(date.today(),t.time())+relativedelta(weekday=f-1))
event.add('dtend', datetime.datetime.combine(date.today(),t.time())+relativedelta(weekday=f-1,hours=1))
event.add('RRULE:FREQ=WEEKLY','',parameters={'COUNT': '100'})
event.add('URL',url)
event.add('LOCATION',location.title())
cal.add_component(event)
cal['summary'] = 'LA Fitness iCal'
f = open('LA Fitness %s.ics' % clubid,'wr')
f.write(cal.to_ical())