~bronikowski/elevencal

ref: da8d74152eee65cd77ad7442ba028c70e603cd57 elevencal/polsat.py -rw-r--r-- 1.8 KiB
da8d7415Emil Oppeln-Bronikowski Make a big-ass calendar 8 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#! env python3

import json
import requests
from datetime import datetime, date
from pprint import pprint
from const import polsat_options, POLSAT_URL
from icalendar import Event, Calendar

data = requests.get(POLSAT_URL.format(date.today), headers={'User-agent': 'Mozilla/5.0'})
data.encoding = 'utf-8'
data = data.json()

def prepare_calendar(title) -> Calendar:
    calendar = Calendar()
    calendar['summary'] = title
    calendar.add('prodid', '-//{}//'.format(title))
    calendar.add('version', '2.0')
    calendar.add('tzid', 'Europe/Warsaw')
    return calendar

def populate_calendar(calendar: Calendar, program):
    for a in program:
        if not a['live']:
            continue
        event = Event()
        event.add('dtstart', datetime.fromtimestamp(a['emissionDate']/1000))
        event.add('dtend', datetime.fromtimestamp( (a['emissionDate']/1000) + 3659 ) )
        event.add('dtstamp', datetime.now())
        event.add('description', a['preview'])
        event.add('summary', a['title'])
        event.add('uid', a['id'])
        calendar.add_component(event)
    return calendar

big_ass_calendar = []
for c in data['channels']:
    if  c['title'] not in polsat_options.keys():
        continue
    calendar = prepare_calendar(c['title'])
    calendar = populate_calendar(calendar, c['programs'])
    big_ass_calendar.append(calendar)
    with open('{}.ics'.format(polsat_options[c['title']]), 'wb') as f:
        f.write(calendar.to_ical())

if big_ass_calendar:
    calendar = prepare_calendar('Polsat')
    for c in big_ass_calendar:
        for s in c.subcomponents:
            description = c['summary'] + ': ' + str(s.pop('description'))
            s.add('description', description)
            calendar.add_component(s)
    with open('polsat.ics', 'wb') as f:
        f.write(calendar.to_ical())