Last active
October 11, 2022 18:20
-
-
Save danielecook/8592678 to your computer and use it in GitHub Desktop.
Generates an ics (icalendar / ical) for an LA fitness club. You must know the club ID, which you can get by going through their website, finding your club, and going to the fitness classes page. It's at the end of the URL (clubid=...)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment