summaryrefslogtreecommitdiff
path: root/plugin.video.mediathek/simplexbmc.py
blob: b4b3cb61ef050c14a4b1e70665428f9bf8a4e9b8 (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
# -*- coding: utf-8 -*-
#-------------LicenseHeader--------------
# plugin.video.mediathek - display german mediathekes
# 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 xbmc, xbmcgui, xbmcplugin,xbmcaddon, sys, urllib, os, time, re
from bs4 import BeautifulSoup;
import json
import hashlib
from mediathek import ComplexLink;

regex_findLink = re.compile("mms://[^\"]*wmv");

__plugin__ = "Mediathek"

settings = xbmcaddon.Addon(id='plugin.video.mediathek')
translation = settings.getLocalizedString

class SimpleXbmcGui(object):
  def __init__(self,settings):
    self.settings = xbmcaddon.Addon(id='plugin.video.mediathek');
    self.quality = int(xbmcplugin.getSetting(int(sys.argv[1]), "quality" ));
    self.preferedStreamTyp = int(xbmcplugin.getSetting(int(sys.argv[1]), "preferedStreamType"));
    self.log("quality: %s"%(self.quality));
    self.plugin_profile_dir = xbmc.translatePath(settings.getAddonInfo("profile"))
    if not os.path.exists(self.plugin_profile_dir):
      os.mkdir(self.plugin_profile_dir);

  def log(self, msg):
    if not isinstance(msg, (str, unicode)):
      xbmc.log("[%s]: %s" % (__plugin__, type(msg)))
    else:
      xbmc.log("[%s]: %s" % (__plugin__, msg.encode('utf8')))

  def buildVideoLink(self, displayObject, mediathek, objectCount):
    metaData = self.BuildMeteData(displayObject)
    if displayObject.picture is not None:
      listItem=xbmcgui.ListItem(metaData["title"], iconImage="DefaultFolder.png", thumbnailImage=displayObject.picture)
    else:
      listItem=xbmcgui.ListItem(metaData["title"], iconImage="DefaultFolder.png")
    listItem.setInfo("video",metaData);

    if(displayObject.isPlayable):
      if(displayObject.isPlayable == "PlayList"):
        link = displayObject.link[0]
        url = "%s?type=%s&action=openPlayList&link=%s" % (sys.argv[0],mediathek.name(), urllib.quote_plus(link.basePath))
        listItem.setProperty('IsPlayable', 'true');
        xbmcplugin.addDirectoryItem(int(sys.argv[1]),url,listItem,False,objectCount)
      elif(displayObject.isPlayable == "JsonLink"):
        link = displayObject.link
        url = "%s?type=%s&action=openJsonLink&link=%s" % (sys.argv[0],mediathek.name(), urllib.quote_plus(link))
        listItem.setProperty('IsPlayable', 'true');
        xbmcplugin.addDirectoryItem(int(sys.argv[1]),url,listItem, False, objectCount)
      else:
        link = self.extractLink(displayObject.link);
        if(isinstance(link,ComplexLink)):
          self.log("PlayPath:"+ link.playPath);
          listItem.setProperty("PlayPath", link.playPath);
        listItem.setProperty('IsPlayable', 'true');
        xbmcplugin.addDirectoryItem(int(sys.argv[1]),link.basePath,listItem,False,objectCount)
    else:
      url = "%s?type=%s&action=openTopicPage&link=%s" % (sys.argv[0],mediathek.name(), urllib.quote_plus(displayObject.link))
      xbmcplugin.addDirectoryItem(int(sys.argv[1]),url,listItem,True,objectCount)

  def BuildMeteData(self, displayObject):
    if(displayObject.subTitle is None or displayObject.subTitle == "" or displayObject.subTitle == displayObject.title):
      title = self.transformHtmlCodes(displayObject.title.rstrip());
    else:
      title = self.transformHtmlCodes(displayObject.title.rstrip() +" - "+ displayObject.subTitle.rstrip());
    if displayObject.date is not None:
      title = "(%s) %s"%(time.strftime("%d.%m",displayObject.date),title.rstrip());

    metaData = {
      "mediatype":"video",
      "title": title,
      "plotoutline":  self.transformHtmlCodes(displayObject.description)
    }

    if(displayObject.duration is not None):
      metaData["duration"] = int(displayObject.duration);

    if(displayObject.date is not None):
          metaData["date"] =time.strftime("%d.%m.%Y",displayObject.date);
          metaData["year"] =int(time.strftime("%Y",displayObject.date));
    return metaData;

  def transformHtmlCodes(self, content):
    return BeautifulSoup(content,"html.parser").prettify(formatter=None);

  def buildMenuLink(self,menuObject,mediathek,objectCount):
    title = menuObject.name;
    listItem=xbmcgui.ListItem(title, iconImage="DefaultFolder.png")
    url = "%s?type=%s&action=openMenu&path=%s" % (sys.argv[0],mediathek.name(), menuObject.path)
    xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=url,listitem=listItem,isFolder=True,totalItems = objectCount)

  def storeJsonFile(self,jsonObject,additionalIdentifier = None):
    hashGenerator = hashlib.md5();
    hashGenerator.update(sys.argv[2]);
    if(additionalIdentifier is not None):
      hashGenerator.update(additionalIdentifier);
    callhash = hashGenerator.hexdigest();
    storedJsonFile = os.path.join(self.plugin_profile_dir,"%s.json"%callhash);
    with open(storedJsonFile, 'wb') as output:
      json.dump(jsonObject,output);
    return callhash;

  def loadJsonFile(self,callhash):
    storedJsonFile = os.path.join(self.plugin_profile_dir,"%s.json"%callhash);
    with open(storedJsonFile,"rb") as input:
      return json.load(input);

  def buildJsonLink(self,mediathek,title,jsonPath,callhash,objectCount):
    listItem=xbmcgui.ListItem(title, iconImage="DefaultFolder.png")
    url = "%s?type=%s&action=openJsonPath&path=%s&callhash=%s" % (sys.argv[0],mediathek.name(), jsonPath,callhash)
    xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=url,listitem=listItem,isFolder=True,totalItems = objectCount)

  def listAvailableMediathekes(self, mediathekNames):
    rootPath = os.path.join(self.settings.getAddonInfo('path'),"resources/logos/png/");
    for name in mediathekNames:
      listItem=xbmcgui.ListItem(name, iconImage="DefaultFolder.png",thumbnailImage=os.path.join(rootPath,name+".png"))
      url = "%s?type=%s" % (sys.argv[0], name)
      xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=url,listitem=listItem,isFolder=True)
  def openMenuContext(self):
    self.dialogProgress = xbmcgui.DialogProgress();

  def closeMenuContext(self):
    xbmcplugin.endOfDirectory(int(sys.argv[1]))

  def getHomeDir(self):
    return self.settings.getAddonInfo("profile");

  def back(self):
    xbmc.executebuiltin("Action(PreviousMenu)");

  def keyboardInput(self):
    keyboard = xbmc.Keyboard("")
    keyboard.doModal();
    return keyboard;

  def addSearchButton(self,mediathek):
    title = translation(30100);
    listItem=xbmcgui.ListItem(title, iconImage="DefaultFolder.png")
    if(mediathek is not None):
      url = "%s?type=%s&action=search" % (sys.argv[0],mediathek.name())
    else:
      url = "%s?action=search" % (sys.argv[0])
    xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=url,listitem=listItem,isFolder=True)

  def readText(self,node,textNode):
    try:
      node = node.getElementsByTagName(textNode)[0].firstChild;
      return unicode(node.data);
    except:
      return "";

  def playPlaylist(self, remotePlaylist):
    player = xbmc.Player();

    playerItem = xbmcgui.ListItem(remotePlaylist);
    playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO);
    playlist.clear();
    for link in regex_findLink.findall(remotePlaylist):
      listItem=xbmcgui.ListItem(link);
      listItem.setProperty("PlayPath", link);
      playlist.add(url=link, listitem=listItem);

    player.play(playlist, playerItem, false);

  def errorOK(self,title="", msg=""):
    e = str( sys.exc_info()[ 1 ] )
    self.log(e)
    if not title:
      title = __plugin__
    if not msg:
      msg = "ERROR!"
    if(e == None):
      xbmcgui.Dialog().ok( title, msg, e )
    else:
      xbmcgui.Dialog().ok( title, msg)
  def play(self,links):
    link = self.extractLink(links);
    listItem = xbmcgui.ListItem(path=link.basePath)
    xbmcplugin.setResolvedUrl(int(sys.argv[1]), True, listitem=listItem)

  def extractLink(self, links):
    if(self.quality in links):
      return links[self.quality];
    else:
      selectedKey = -1;
      for key in links.keys():
        if(key < self.quality and key > selectedKey):
          selectedKey = key;
      if(selectedKey > -1):
        return links[selectedKey];
      else:
        selectedKey = links.keys()[0];
        for key in links.keys():
          if(key < selectedKey):
            selectedKey = key;
        return links[selectedKey];