summaryrefslogtreecommitdiff
path: root/plugin.video.embycon/resources/lib/trakttokodi.py
blob: 3e27740f0df911cce1eb8606a9fb9cceb6789c00 (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
# Gnu General Public License - see LICENSE.TXT

import urllib
import encodings

import xbmc
import xbmcgui

from simple_logging import SimpleLogging
from datamanager import DataManager

from translation import i18n

log = SimpleLogging(__name__)
dataManager = DataManager()

details_string = 'EpisodeCount,SeasonCount,Path,Etag,MediaStreams'
icon = xbmc.translatePath('special://home/addons/plugin.video.embycon/icon.png')


def not_found(content_string):
    xbmcgui.Dialog().notification('EmbyCon', i18n('not_found_') % content_string, icon=icon, sound=False)


def playback_starting(content_string):
    xbmcgui.Dialog().notification('EmbyCon', i18n('playback_starting_') % content_string, icon=icon, sound=False)


def search(item_type, query):
    content_url = ('{server}/emby/Search/Hints?searchTerm=' + query +
                   '&IncludeItemTypes=' + item_type +
                   '&UserId={userid}'
                   '&StartIndex=0' +
                   '&Limit=25' +
                   '&IncludePeople=false&IncludeMedia=true&IncludeGenres=false&IncludeStudios=false&IncludeArtists=false')

    result = dataManager.GetContent(content_url)
    return result


def get_items(video_type, item_id=None, parent_id=None):
    content_url = None
    result = dict()

    if video_type == 'season':
        content_url = ('{server}/emby/Shows/' + item_id +
                       '/Seasons'
                       '?userId={userid}' +
                       '&Fields=' + details_string +
                       '&format=json')

    elif video_type == 'movie' or video_type == 'episode':
        content_url = ('{server}/emby/Users/{userid}/items' +
                       '?ParentId=' + parent_id +
                       '&IsVirtualUnAired=false' +
                       '&IsMissing=false' +
                       '&Fields=' + details_string +
                       '&format=json')

    if content_url:
        result = dataManager.GetContent(content_url)

    return result


def get_item(item_id):
    result = dataManager.GetContent('{server}/emby/Users/{userid}/Items/' + item_id + '?Fields=ProviderIds&format=json')
    return result


def get_imdb_id(item_id):
    item = get_item(item_id)
    imdb = item.get('ProviderIds', {}).get('Imdb')
    return imdb


def get_season_id(parent_id, season):
    season_items = get_items('season', parent_id)
    season_items = season_items.get('Items')

    if season_items is None:
        season_items = []

    for season_item in season_items:
        if season_item.get('IndexNumber') == int(season):
            season_id = season_item.get('Id')
            return season_id

    return None


def get_episode_id(parent_id, episode):
    episode_items = get_items('episode', parent_id=parent_id)
    episode_items = episode_items.get('Items')

    if episode_items is None:
        episode_items = []

    for episode_item in episode_items:
        if episode_item.get('IndexNumber') == int(episode):
            episode_id = episode_item.get('Id')
            return episode_id

    return None


def get_match(item_type, title, year, imdb_id):
    query = urllib.quote(title)

    results = search(item_type, query=query)
    results = results.get('SearchHints')
    if results is None:
        results = []
    log.debug('SearchHints jsonData: {0}', results)

    potential_matches = []

    for item in results:
        name = item.get('Name')
        production_year = item.get('ProductionYear')
        if (name == title and int(year) == production_year) or (int(year) == production_year):
            potential_matches.append(item)

    log.debug('Potential matches: {0}', potential_matches)

    for item in potential_matches:
        item_imdb_id = get_imdb_id(item.get('ItemId'))
        if item_imdb_id == imdb_id:
            log.debug('Found match: {0}', item)
            return item

    return None


def entry_point(parameters):
    item_type = None
    action = parameters.get('action', None)
    video_type = parameters.get('video_type', None)

    title = urllib.unquote(parameters.get('title', ''))

    year = parameters.get('year', '')
    episode = parameters.get('episode', '')
    season = parameters.get('season', '')
    imdb_id = parameters.get('imdb_id', '')

    if video_type == 'show' or video_type == 'season' or video_type == 'episode':
        item_type = 'Series'
    elif video_type == 'movie':
        item_type = 'Movie'

    if not item_type:
        return

    match = get_match(item_type, title, year, imdb_id)

    if not match:
        title_search_word = ''
        title_words = title.split(' ')

        for word in title_words:
            if len(word) > len(title_search_word):
                title_search_word = word

        title_search_word = title_search_word.replace(':', '')

        if title_search_word:
            match = get_match(item_type, title_search_word, year, imdb_id)

    str_season = str(season)
    if len(str_season) == 1:
        str_season = '0' + str_season
    str_episode = str(episode)
    if len(str_episode) == 1:
        str_episode = '0' + str_episode

    if action == 'play':
        play_item_id = None

        if video_type == 'movie':
            if match:
                play_item_id = match.get('ItemId')

            if not play_item_id:
                not_found('{title} ({year})'.format(title=title, year=year))

        elif video_type == 'episode':
            if not season or not episode:
                return

            if match:
                item_id = match.get('ItemId')
                season_id = get_season_id(item_id, season)

                if season_id:
                    episode_id = get_episode_id(season_id, episode)
                    if episode_id:
                        play_item_id = episode_id

            if not play_item_id:
                not_found('{title} ({year}) - S{season}E{episode}'.format(title=title, year=year, season=str_season, episode=str_episode))

        if play_item_id:
            if video_type == 'episode':
                playback_starting('{title} ({year}) - S{season}E{episode}'.format(title=title, year=year, season=str_season, episode=str_episode))
            else:
                playback_starting('{title} ({year})'.format(title=title, year=year))
            xbmc.executebuiltin('RunPlugin(plugin://plugin.video.embycon/?mode=PLAY&item_id={item_id})'.format(item_id=play_item_id))

    elif action == 'open':
        url = media_type = None

        if video_type == 'show':
            if match:
                item_id = match.get('ItemId')
                media_type = 'series'
                url = ('{server}/emby/Shows/' + item_id +
                       '/Seasons'
                       '?userId={userid}' +
                       '&Fields=' + details_string +
                       '&format=json')

            if not url:
                not_found('{title} ({year})'.format(title=title, year=year))

        elif video_type == 'season':
            if not season:
                return

            if match:
                item_id = match.get('ItemId')
                season_id = get_season_id(item_id, season)

                if season_id:
                    media_type = 'episodes'

                    url = ('{server}/emby/Users/{userid}/items' +
                           '?ParentId=' + season_id +
                           '&IsVirtualUnAired=false' +
                           '&IsMissing=false' +
                           '&Fields=' + details_string +
                           '&format=json')

            if not url:
                not_found('{title} ({year}) - S{season}'.format(title=title, year=year, season=str_season))

        if url and media_type:
            xbmc.executebuiltin('ActivateWindow(Videos, plugin://plugin.video.embycon/?mode=GET_CONTENT&url={url}&media_type={media_type})'.format(url=urllib.quote(url), media_type=media_type))