summaryrefslogtreecommitdiff
path: root/plugin.video.mediathek/mediathek/ndr.py
blob: cbe57df35917cce096b581c8f226b22049b2e1fb (plain)
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# -*- coding: utf-8 -*-
# -------------LicenseHeader--------------
# plugin.video.Mediathek - Gives access to most video-platforms from German public service broadcasters
# Copyright (C) 2010  Raptor 2101 [raptor2101@gmx.de]
#
# 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 3 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/>.
import re
import datetime
import time
import calendar
from mediathek import *
from xml.dom import minidom


class NDRMediathek(Mediathek):
    @classmethod
    def name(self):
        return "NDR"

    def isSearchable(self):
        return True

    def __init__(self, simpleXbmcGui):
        self.gui = simpleXbmcGui

        self.rootLink = "http://www.ndr.de"

        self.searchLink = self.rootLink+"/suche10.html?search_mediathek=1&"

        # Hauptmenue
        tmp_menu = []
        extractBroadcasts = re.compile("<a href=\"/mediathek/mediatheksuche105_broadcast-(.*?).html\" class=\"link_arrow\">(.*?)</a>")
        htmlPage = self.loadPage("http://www.ndr.de/mediathek/sendungen_a-z/index.html").decode('utf-8')

        x = 0
        for menuNode in extractBroadcasts.finditer(htmlPage):
            menuId = menuNode.group(1)
            menuItem = menuNode.group(2)
            menuLink = self.rootLink+"/mediatheksuche105_broadcast-"+menuId+"_format-video_page-1.html"
            tmp_menu.append(TreeNode("0."+str(x), menuItem, menuLink, True))
            x = x+1

        self.menuTree = [
            TreeNode("0", "Sendungen von A-Z", "", False, tmp_menu),
            TreeNode("1", "Sendung verpasst?", "sendungverpasst", True),
            TreeNode("2","Live","livestream",True),#Livestream ruckelt zu stark :-(
        ]

    def buildPageMenuSendungVerpasst(self, action):
        htmlPage = self.loadPage("http://www.ndr.de/mediathek/sendung_verpasst/epg1490_display-onlyvideo.html")

        regex_verpasstNow = re.compile(
            '<h1 class="viewdate">\n.*?(\\d{2})\.(\\d{2})<span class="notbelow30em">\.(\\d{4})</span>'
        )
        verpasstNow = ".".join(regex_verpasstNow.search(htmlPage).groups())
        try:
            dateTimeTmp = datetime.datetime.strptime(verpasstNow, "%d.%m.%Y")
        except TypeError:
            dateTimeTmp = datetime.datetime(*(time.strptime(verpasstNow, "%d.%m.%Y")[0:6]))

        nodeCount = 0

        if action == "":
            verpasstHeute = dateTimeTmp.strftime("%Y-%m-%d")
            dateTimeTmp = dateTimeTmp-datetime.timedelta(1)
            verpasstGestern = dateTimeTmp.strftime("%Y-%m-%d")
            dateTimeTmp = dateTimeTmp-datetime.timedelta(1)
            verpasstVorGestern = dateTimeTmp.strftime("%Y-%m-%d")

            self.gui.buildVideoLink(DisplayObject("Heute", "", "", "description", self.rootLink + "/mediathek/sendung_verpasst/epg1490_date-" + verpasstHeute + "_display-onlyvideo.html", False), self, 1)
            self.gui.buildVideoLink(DisplayObject("Gestern", "", "", "description", self.rootLink + "/mediathek/sendung_verpasst/epg1490_date-" + verpasstGestern + "_display-onlyvideo.html", False), self, 2)
            self.gui.buildVideoLink(DisplayObject("Vorgestern", "", "", "description", self.rootLink + "/mediathek/sendung_verpasst/epg1490_date-" + verpasstVorGestern + "_display-onlyvideo.html", False), self, 3)
            self.gui.buildVideoLink(DisplayObject("Datum waehlen", "", "", "description", "sendungverpasstselect", False), self, 4)
        elif action == "select":
            dateTimeTmp = dateTimeTmp-datetime.timedelta(3)
            verpasstStartYear = int(dateTimeTmp.strftime("%Y"))
            for verpasstStart in reversed(range(1, int(dateTimeTmp.strftime("%m")))):
                menu_title = str(verpasstStart)+"."+str(verpasstStartYear)
                menu_action = "sendungverpasstselectmonth" + str(verpasstStartYear) + str(verpasstStart)
                self.gui.buildVideoLink(DisplayObject(menu_title, "", "", "description", menu_action, False), self, nodeCount)
                verpasstStart = verpasstStart - 1
                nodeCount = nodeCount + 1

            while verpasstStartYear > 2008:
                verpasstStartYear = verpasstStartYear - 1
                menu_title = str(verpasstStartYear)
                self.gui.buildVideoLink(DisplayObject(menu_title, "", "", "description", "sendungverpasstselectyear" + str(verpasstStartYear), False), self, nodeCount)
        elif action[0:11] == "selectmonth":
            action = action[11:]
            action_year = action[0:4]
            action_month = action[4:]

            try:
                dateTimeTmp2 = datetime.datetime.strptime(action_year+action_month, "%Y%m")
            except TypeError:
                dateTimeTmp2 = datetime.datetime(*(time.strptime(action_year+action_month, "%Y%m")[0:6]))

            if dateTimeTmp.strftime("%Y%m") == dateTimeTmp2.strftime("%Y%m"):
                startDay = int(dateTimeTmp2.strftime("%d"))
            else:
                startDay = calendar.monthrange(int(action_year), int(action_month))[1]

            try:
                dateTimeTmp2 = datetime.datetime.strptime(action_year + action_month + str(startDay), "%Y%m%d")
            except TypeError:
                dateTimeTmp2 = datetime.datetime(*(time.strptime(action_year + action_month + str(startDay), "%Y%m%d")[0:6]))

            for i in reversed(range(1, startDay)):
                verpasstDatum = dateTimeTmp2.strftime("%Y-%m-%d")
                menu_title = dateTimeTmp2.strftime("%d.%m.%Y")
                menu_action = self.rootLink + "/mediathek/sendung_verpasst/epg1490_date-" + verpasstDatum + "_display-onlyvideo.html"
                self.gui.buildVideoLink(DisplayObject(menu_title, "", "", "description", menu_action, False), self, nodeCount)
                nodeCount = nodeCount + 1
                dateTimeTmp2 = dateTimeTmp2-datetime.timedelta(1)
        elif action[0:10] == "selectyear":
            action = action[10:]
            action_year = action[0:4]
            for startMonth in reversed(range(1, 12)):
                menu_title = str(startMonth) + "." + action_year
                menu_action = "sendungverpasstselectmonth" + action_year + str(startMonth)
                self.gui.buildVideoLink(DisplayObject(menu_title, "", "", "description", menu_action, False), self, nodeCount)
                nodeCount = nodeCount + 1

    def buildPageMenuLivestream(self):
            nodeCount = 0

            # Hamburg
            nodeCount = nodeCount+1
            links = {}
            links[0] = SimpleLink("http://ndr_fs-lh.akamaihd.net/i/ndrfs_hh@119223/master.m3u8", 0)
            self.gui.buildVideoLink(DisplayObject("Hamburg", "", "", "", links, True), self, nodeCount)

            # Mecklenburg-Vorpommern
            nodeCount = nodeCount+1
            links = {}
            links[0] = SimpleLink("http://ndr_fs-lh.akamaihd.net/i/ndrfs_mv@119226/master.m3u8", 0)
            self.gui.buildVideoLink(DisplayObject("Mecklenburg-Vorpommern", "", "", "", links, True), self, nodeCount)

            # Niedersachsen
            nodeCount = nodeCount+1
            links = {}
            links[0] = SimpleLink("http://ndr_fs-lh.akamaihd.net/i/ndrfs_nds@119224/master.m3u8", 0)
            self.gui.buildVideoLink(DisplayObject("Niedersachsen", "", "", "", links, True), self, nodeCount)

            # Schleswig-Holstein
            nodeCount = nodeCount+1
            links = {}
            links[0] = SimpleLink("http://ndr_fs-lh.akamaihd.net/i/ndrfs_sh@119225/master.m3u8", 0)
            self.gui.buildVideoLink(DisplayObject("Schleswig-Holstein", "", "", "", links, True), self, nodeCount)

    def buildPageMenuVideoListVerpasst(self, link, initCount):
        self.gui.log("buildPageMenuVerpasst: " + link)

        htmlPage = self.loadPage(link)

        re_video_item = re.compile(
            '<div class="videolinks"><a href="(.*?)" title=".*?" class=\'button epgbutton\' >'
        )
        # won't parse "Beiträge", they are only cutted parts of the main
        # program
        video_items = re.findall(re_video_item, htmlPage)
        nodeCount = initCount + len(video_items)
        for video_link in video_items:
            if not re.compile("http://www.n-joy.de/.*").search(video_link):
                print video_link
                self.extractVideoInformation(video_link, None, nodeCount)


    def buildPageMenuVideoList(self, link, initCount):
        self.gui.log("buildPageMenu: " + link)

        htmlPage = self.loadPage(link)

        regex_extractVideoItems = re.compile(
            "<div class=\"teaserpadding\">"
            "(.*?)"
            "(</p>\n</div>\n</div>|\n</div>\n</div>\n</li>)", re.DOTALL)
        regex_extractVideoItemHref = re.compile("<a title=\".*?\" href=\"(.*?/[^/]*?\.html)\".*?>", re.DOTALL)
        regex_extractVideoItemDate = re.compile("<div class=\"subline\" style=\"cursor: pointer;\">.*?(\\d{2}\.\\d{2}\.\\d{4} \\d{2}:\\d{2})</div>")

        videoItems = regex_extractVideoItems.findall(htmlPage)
        nodeCount = initCount + len(videoItems)

        for videoItem in videoItems:
            print "link: {0}".format(link)
            print "videoItem: {0}".format(videoItem[0])
            if "<div class=\"subline\">" not in videoItem[0]:
                continue
            videoLink = regex_extractVideoItemHref.search(videoItem[0]).group(1)
            try:
                dateString = regex_extractVideoItemDate.search(videoItem[0]).group(1)
                dateTime = time.strptime(dateString, "%d.%m.%Y %H:%M")
            except:
                dateTime = None
            # TODO: Some videos from Extra 3 are located on http://www.n-joy.de/
            #       which cannot be parsed by this script, yet.
            if not re.compile("http://www.n-joy.de/.*").search(videoLink):
                self.extractVideoInformation(videoLink, dateTime, nodeCount)

        # Pagination (weiter)
        regex_extractNextPage = re.compile(
            "<a class=\"square button\" href=\"(.*?)\" title=\"(.*?)\".*?>"
        )
        for nextPageHref in regex_extractNextPage.finditer(htmlPage):
            menuItemName = nextPageHref.group(2).decode("UTF-8")
            link = self.rootLink+nextPageHref.group(1)
            self.gui.buildVideoLink(DisplayObject(menuItemName, "", "", "description", link, False), self, nodeCount)

    def buildPageMenu(self, link, initCount):

        print link
        if link[0:15] == "sendungverpasst":
            self.buildPageMenuSendungVerpasst(link[15:])
        elif link == "livestream":
            self.buildPageMenuLivestream()
        elif "/sendung_verpasst/" in link:
            self.buildPageMenuVideoListVerpasst(link, initCount)
        else:
            self.buildPageMenuVideoList(link, initCount)

    def searchVideo(self, searchText):
        searchText = searchText.encode("UTF-8")
        searchText = urllib.urlencode({"query": searchText})
        self.buildPageMenu(self.searchLink+searchText, 0)

    def extractVideoInformation(self, videoLink, pubDate, nodeCount):

        regexFindVideoLink = re.compile("http://.*(hq.mp4|hi.mp4|lo.flv)")
        regexFindImageLink = re.compile("/.*v-ardgalerie.jpg")
        regexFindMediaData = re.compile(
            "<div class=\"padding group\">\\s*?<div class=\"textinfo\">\\s*?<h\\d.*?>"
            "(.*?)"
            "</h\\d>\\s*?.*?<div class=\"subline\">.*?</div>\\s*?<p.*?>"
            "(.*?)"
            "</p>", re.DOTALL
        )
        if not videoLink.startswith(self.rootLink):
            videoLink = self.rootLink+videoLink
        videoPage = self.loadPage(videoLink)

        self.gui.log("videolink: {0}".format(videoLink))
        videoLink = {}
        videoLink[0] = SimpleLink(regexFindVideoLink.search(videoPage).group(0), 0)

        try:
            pictureLink = self.rootLink+regexFindImageLink.search(videoPage).group(0)
        except:
            pictureLink = None
        if '<article class="w66 ">' not in videoPage:
            searchResult = regexFindMediaData.search(videoPage)
            title = searchResult.group(1).decode('utf-8')
            description = searchResult.group(2).decode('utf-8')
        else:
            title = re.search(
                'var trackTitle = "(.*?)"',
                videoPage
            ).group(1).decode('utf-8')
            description = re.search(
                '<meta name="description" content="(.*?)"',
                videoPage
            ).group(1).decode('utf-8')

        self.gui.buildVideoLink(DisplayObject(title, "", pictureLink, description, videoLink, True, pubDate, 0), self, nodeCount)