Fix compatibility with latest Kodi 18
[plugin.video.crunchyroll.git] / resources / lib / view.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
# -*- coding: utf-8 -*-
# Crunchyroll
# Copyright (C) 2018 MrKrabat
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
import re
import sys
try:
    from urllib import quote_plus
except ImportError:
    from urllib.parse import quote_plus
 
import xbmc
import xbmcgui
import xbmcplugin
 
 
# keys allowed in setInfo
types = ["count", "size", "date", "genre", "country", "year", "episode", "season", "sortepisode", "top250", "setid",
         "tracknumber", "rating", "userrating", "watched", "playcount", "overlay", "cast", "castandrole", "director",
         "mpaa", "plot", "plotoutline", "title", "originaltitle", "sorttitle", "duration", "studio", "tagline", "writer",
         "tvshowtitle", "premiered", "status", "set", "setoverview", "tag", "imdbnumber", "code", "aired", "credits",
         "lastplayed", "album", "artist", "votes", "path", "trailer", "dateadded", "mediatype", "dbid"]
 
 
def endofdirectory():
    # sort methods are required in library mode
    xbmcplugin.addSortMethod(int(sys.argv[1]), xbmcplugin.SORT_METHOD_NONE)
 
    # let xbmc know the script is done adding items to the list
    xbmcplugin.endOfDirectory(handle = int(sys.argv[1]))
 
 
def add_item(args, info, isFolder=True, total_items=0, mediatype="video"):
    """Add item to directory listing.
    """
 
    # create list item
    li = xbmcgui.ListItem(label = info["title"])
 
    # get infoLabels
    infoLabels = make_infolabel(args, info)
 
    # get url
    u = build_url(args, info)
 
    if isFolder:
        # directory
        li.setInfo(mediatype, infoLabels)
    else:
        # playable video
        infoLabels["mediatype"] = "video"
        li.setInfo(mediatype, infoLabels)
        li.setProperty("IsPlayable", "true")
 
        # add context menue
        cm = []
        if u"series_id" in u:
            cm.append((args._addon.getLocalizedString(30045), "XBMC.Container.Update(%s)" % re.sub(r"(?<=mode=)[^&]*", "series", u)))
        if u"collection_id" in u:
            cm.append((args._addon.getLocalizedString(30046), "XBMC.Container.Update(%s)" % re.sub(r"(?<=mode=)[^&]*", "episodes", u)))
        if len(cm) > 0:
            li.addContextMenuItems(cm)
 
    # set media image
    li.setArt({"thumb":  info.get("thumb",  "DefaultFolder.png"),
               "poster": info.get("thumb",  "DefaultFolder.png"),
               "banner": info.get("thumb",  "DefaultFolder.png"),
               "fanart": info.get("fanart", xbmc.translatePath(args._addon.getAddonInfo("fanart"))),
               "icon":   info.get("thumb",  "DefaultFolder.png")})
 
    # add item to list
    xbmcplugin.addDirectoryItem(handle     = int(sys.argv[1]),
                                url        = u,
                                listitem   = li,
                                isFolder   = isFolder,
                                totalItems = total_items)
 
 
def quote_value(value, PY2):
    """Quote value depending on python
    """
    if PY2:
        if not isinstance(value, basestring):
            value = str(value)
        return quote_plus(value.encode("utf-8") if isinstance(value, unicode) else value)
    else:
        if not isinstance(value, str):
            value = str(value)
        return quote_plus(value)
 
 
def build_url(args, info):
    """Create url
    """
    s = ""
    # step 1 copy new information from info
    for key, value in list(info.items()):
        if value:
            s = s + "&" + key + "=" + quote_value(value, args.PY2)
 
    # step 2 copy old information from args, but don't append twice
    for key, value in list(args.__dict__.items()):
        if value and key in types and not "&" + str(key) + "=" in s:
            s = s + "&" + key + "=" + quote_value(value, args.PY2)
 
    return sys.argv[0] + "?" + s[1:]
 
 
def make_infolabel(args, info):
    """Generate infoLabels from existing dict
    """
    infoLabels = {}
    # step 1 copy new information from info
    for key, value in list(info.items()):
        if value and key in types:
            infoLabels[key] = value
 
    # step 2 copy old information from args, but don't overwrite
    for key, value in list(args.__dict__.items()):
        if value and key in types and key not in infoLabels:
            infoLabels[key] = value
 
    return infoLabels