-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrikanalen.py
175 lines (139 loc) · 5.1 KB
/
frikanalen.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# -*- coding: utf-8 -*-
#
# Copyright (C) 2016-2017 Alexander Alemayhu
# 2018 Petter Reinholdtsen
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import unicode_literals
import datetime
from requests import Session
import urllib
session = Session()
session.headers['User-Agent'] = 'kodi.tv'
session.headers['app-version-android'] = '999'
def stream_url():
return 'http://video.nuug.no/frikanalen.webm'
def stream_url_hd():
return 'http://video.nuug.no/frikanalen-hd.webm'
class Video:
id = None
description = None
duration = None
header = None
name = None
ogv_url = None
large_thumbnail_url = None
organization = None
categories = []
# NB: some fields have been skipped
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
@staticmethod
def from_response(r):
if 'header' in r and r['header'] is not None:
r['header'] = r['header'] + "\n\nID #%s" % r['id']
else:
r['header'] = "ID #%s" % r['id']
return Video(
description=r['description'],
duration=duration2sec(r['duration']),
header=r['header'],
name=r['name'],
ogv_url=r['ogv_url'],
large_thumbnail_url=r['large_thumbnail_url'],
organization=r['organization'],
categories=r['categories'],
)
class ScheduleItem:
# TODO: Review all properties
default_name = None
video_id = None
video = None
schedulereason = None
starttime = None
duration = None
# NB: some fields have been skipped
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
@staticmethod
def from_response(r):
if 'starttime' in r:
starttime = iso2datetime(r['starttime'])
else:
starttime=None
return ScheduleItem(
default_name=r['default_name'],
video_id=r['video_id'],
video=Video.from_response(r['video']),
schedulereason=r['schedulereason'],
starttime=starttime,
duration=duration2sec(r['duration']),
)
def _get(path):
r = session.get("https://frikanalen.no/api/" + path)
r.raise_for_status()
return r.json()
def is_today(t):
return t.day == datetime.datetime.today().day
def videosearch(query):
query= urllib.quote_plus(query)
search_response = _get('videos/?has_tono_records=false&proper_import=true&publish_on_web=true&q=%s' % query)
videos= [Video.from_response(item) for item in search_response['results']]
return videos
def categories():
categories_response = _get('categories/')
return [item['name'] for item in categories_response['results']]
def in_category(category):
category = urllib.quote_plus(category)
category_response = _get('videos/?categories__name__icontains=%s&has_tono_records=false&proper_import=true&publish_on_web=true' % category)
videos= [Video.from_response(item) for item in category_response['results']]
return videos
def today_programs():
schedule_response = _get('scheduleitems/?date=today')
items = [ScheduleItem.from_response(item) for item in schedule_response['results']]
return [item for item in items if is_today(item.starttime) == True]
def whats_on():
now = datetime.datetime.now()
program = sorted(today_programs())
for item in program:
t = item.starttime
if t.hour == now.hour:
print("> [{:d}:{:02d}] {:s} {:f}".format(t.hour, t.minute, item.video.name, item.duration))
else:
print("[{:d}:{:02d}] {:s} {:f}".format(t.hour, t.minute, item.video.name, item.duration))
return ""
def iso2datetime(datestr):
# Workaround for fractional seconds in the output
if '+' in datestr:
# FIXME this is a horrid way to handle timezones, simply throwing it away.
datestr = datestr.split('+')[0]
if '.' in datestr:
return datetime.datetime.strptime(datestr, "%Y-%m-%dT%H:%M:%S.%f")
else:
return datetime.datetime.strptime(datestr, "%Y-%m-%dT%H:%M:%S")
else:
if '.' in datestr:
return datetime.datetime.strptime(datestr, "%Y-%m-%dT%H:%M:%S.%fZ")
else:
print("datestr=", datestr)
return datetime.datetime.strptime(datestr, "%Y-%m-%dT%H:%M:%SZ")
def duration2sec(duration):
"""Convert duration on format H:M:S.frac to seconds"""
p = duration.split(':')
d = 0
m = 1
for part in p[::-1]:
d = d + float(part) * m
m = m * 60
return d