Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Google Calendar iCal
#1
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!
Reply
#2
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.
Reply
#3
Thank you very much, this helped a lot.  Big Grin
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Online calendar for booking. SpongeB0B 6 3,369 Nov-15-2023, 11:27 AM
Last Post: Woki
  Follow Up: Web Calendar based Extraction AgileAVS 0 1,469 Feb-23-2020, 05:39 AM
Last Post: AgileAVS
  Extracting Data from Calendar AgileAVS 2 3,675 Feb-16-2020, 01:06 PM
Last Post: AgileAVS
  Google calendar pthon flask auth Kireta 0 2,046 Sep-16-2019, 04:50 PM
Last Post: Kireta

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020