Python Forum

Full Version: Google Calendar iCal
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I´m currently trying to write a Python-Script that does:
- get the .ics file of a certain calendar from Google Calendar
- extracts the events in it
- writes them to a table in an HTML-file

I´m using the module ics 0.3.1 (can´t provide a link here, but it´s one of the first Google results if you search for "python ical"), but I somehow can´t import the calendar.

My code: (there are spaces in the used URL, because the software recognizes it as a clickable link, which I am not allowed to post)
from ics import Calendar
import urllib.request
from urllib.request import urlopen

url = 'https :// calendar.google.com/ calendar/ ical/ t6l47q578aqr8qjc3pq8cdnfv0%40group.calendar.google.com/ public/ basic.ics'   

title = 'Test'
body_margin = '3em'
background = '#eee'
header_padding = '1em'
article_padding = '2em'

def BeginHTML():
    print("""
    <!doctype html>
        <html>
        <head>
            <meta charset="utf-8">
            <title>""" + title + """</title>
            <style>
            body {margin: """ + body_margin + """;}
            header {background: """ + background + """; padding: """ + header_padding + """;}
            article {padding: """ + article_padding + """;}
            </style>
        </head>
        <body>""")
    
def EndHTML():
    print("""
        </body>
        </html>""")

try:
    urllib.request.urlretrieve(url)
except urllib.error.HTTPError as err:
    print(err.code)

c = Calendar(urlopen(url).read())
BeginHTML()



EndHTML()
I get the following error:

Error:
Traceback (most recent call last):   File "ical_test.py", line 38, in <module>     c = Calendar(urlopen(url).read())   File "//anaconda/lib/python3.5/site-packages/ics/icalendar.py", line 62, in __init__     container = lines_to_container(imports)   File "//anaconda/lib/python3.5/site-packages/ics/parse.py", line 162, in lines_to_container     return parse(tokenize_line(unfold_lines(lines)))   File "//anaconda/lib/python3.5/site-packages/ics/parse.py", line 153, in parse     for line in tokenized_lines:   File "//anaconda/lib/python3.5/site-packages/ics/parse.py", line 147, in tokenize_line     for line in unfolded_lines:   File "//anaconda/lib/python3.5/site-packages/ics/parse.py", line 132, in unfold_lines     if len(line.strip()) == 0: AttributeError: 'int' object has no attribute 'strip'
It would be really great, if there was someone who could help me out, understanding and preferably fixing the error.

Thanks in advance!
You need to decode to eg to utf-8 when read with in urlopen in Python 3.
If not you get byte string back.
c = Calendar(urlopen(url).read().decode('utf-8'))
Output:
https://calendar.google.com/calendar/ical/t6l47q578aqr8qjc3pq8cdnfv0%40group.calendar.google.com/public/basic.ics     <!doctype html>         <html>         <head>             <meta charset="utf-8">             <title>Test</title>             <style>             body {margin: 3em;}             header {background: #eee; padding: 1em;}             article {padding: 2em;}             </style>         </head>         <body>
Had you used Request then there is no need for decode.
Requests is getting correct encoding from web-site.
c = Calendar(requests.get(url).text)
Same output as over.
Thank you very much, this helped a lot.  Big Grin