mymeco.kodi.movie

mymeco/kodi/movie.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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
"""Movie module."""
# pylint: disable=too-many-public-methods
# pylint: disable=too-many-instance-attributes
import typing
from lxml import etree

from mymeco.files.video import Video


RatingType = typing.Mapping[str, typing.Union[int, bool]]
ActorType = typing.Mapping[str, typing.Union[str, int]]


class Movie:
    """
    Movie class.

    Aim to group all information that could be stored in a movie entry of Kodi
    NFO files, bind to an xml formatter.
    """

    def __init__(self, movieid=None, tmdb=None):
        """Init a new movie data structure."""
        self.__data = {}
        if tmdb is not None and movieid is not None:
            self.__fill_data_from_tmdb(tmdb, movieid)

    def __fill_data_from_tmdb(self, tmdb, movieid):
        staffs = tmdb.get_credits(movieid)
        details = tmdb.get_movie(movieid)

        self.title = details.get('title', None)
        self.originaltitle = details.get('original_title', None)
        self.rating('tmdb',
                    details.get('vote_average', None),
                    votes=details.get('vote_count', None), default=True)
        self.plot = details.get('overview', None)
        self.tagline = details.get('tagline', None)
        self.runtime = str(details.get('runtime', None))
        self.poster = details.get('poster_path', None)
        self.add_uniqueid('tmdb', str(movieid))
        self.add_uniqueid('imdb', details.get('imdb_id', None))
        for genre in details.get('genres', []):
            self.add_genre(genre['name'])
        for country in details.get('production_countries', []):
            self.add_country(country['name'])
        # print(set(staff['department'] for staff in staffs['crew']))
        # print(set(
        #     staff['job']
        #     for staff in staffs['crew']
        #     if staff['department'] == 'Directing'
        # ))
        for staff in staffs['crew']:
            if staff['department'] == 'Writing':
                self.add_writter(staff['name'])
            if (
                    staff['department'] == 'Directing' and
                    staff['job'] == 'Director'
            ):
                self.add_director(staff['name'])
        self.premiered = details.get('release_date', None)
        for studio in details.get('production_companies', []):
            self.add_studio(studio['name'])
        for staff in staffs['cast']:
            self.add_actor(
                staff['name'],
                staff['character'],
                staff['order'],
                staff['profile_path']
            )

    @property
    def title(self) -> typing.Optional[str]:
        """Get localized title of movie."""
        return self.__data.get('title', None)

    @title.setter
    def title(self, title: str):
        if title is not None:
            self.__data['title'] = title

    @property
    def originaltitle(self) -> typing.Optional[str]:
        """Get original title of movie."""
        return self.__data.get('originaltitle', self.title)

    @originaltitle.setter
    def originaltitle(self, originaltitle: str):
        if originaltitle is not None:
            self.__data['originaltitle'] = originaltitle

    @property
    def sorttitle(self) -> typing.Optional[str]:
        """Get sortable title of movie."""
        return self.__data.get('sorttitle', self.title)

    @sorttitle.setter
    def sorttitle(self, sorttitle: str):
        if sorttitle is not None:
            self.__data['sorttitle'] = sorttitle

    @property
    def ratings(self) -> typing.Mapping[str, RatingType]:
        """Get list of all ratings associated to this movie."""
        return self.__data['ratings']

    def rating(self, name: str, values: int, **kwargs: RatingType):
        """Store a new rating for given movie."""
        if values is None:
            return
        if 'ratings' not in self.__data:
            self.__data['ratings'] = {}
        self.__data['ratings'][name] = {
            'values': values,
            'votes': kwargs.get('votes', None),
            'max': kwargs.get('max', 10),
            'default': kwargs.get('default', False)
        }

    @property
    def plot(self) -> typing.Optional[str]:
        """Get movie plot."""
        return self.__data.get('plot', None)

    @plot.setter
    def plot(self, plot):
        if plot is not None:
            self.__data['plot'] = plot

    @property
    def tagline(self):
        """Get movie tagline."""
        return self.__data.get('tagline', None)

    @tagline.setter
    def tagline(self, tagline):
        if tagline is not None:
            self.__data['tagline'] = tagline

    @property
    def runtime(self) -> typing.Optional[int]:
        """Get runtime in minutes."""
        return self.__data.get('runtime', None)

    @runtime.setter
    def runtime(self, minutes: typing.Optional[int]):
        if minutes is not None:
            self.__data['runtime'] = minutes

    @property
    def poster(self) -> typing.Optional[str]:
        """Get movie poster URL."""
        return self.__data.get('thumb', {'poster': None}).get('poster', None)

    @poster.setter
    def poster(self, poster: typing.Optional[str]):
        if poster is None:
            return

        if 'thumb' not in self.__data:
            self.__data['thumb'] = {}

        self.__data['thumb']['poster'] = poster

    @property
    def uniqueid(self) -> typing.Optional[typing.Mapping[str, str]]:
        """Retrieve list of unique ids."""
        return self.__data.get('uniqueid', None)

    def add_uniqueid(self, idtype: str, value: str):
        """Add a new uniqueid in movie."""
        if value is None:
            return

        if 'uniqueid' not in self.__data:
            self.__data['uniqueid'] = {}

        self.__data['uniqueid'][idtype] = value

    @property
    def genre(self) -> typing.Iterable[str]:
        """Get list of genres."""
        return self.__data.get('genre', {})

    def add_genre(self, genre: str):
        """Add new genre to movie."""
        if genre is not None:
            if 'genre' not in self.__data:
                self.__data['genre'] = {genre}
            else:
                self.__data['genre'].add(genre)

    @property
    def country(self) -> typing.Iterable[str]:
        """Get list of production countries."""
        return self.__data.get('country', {})

    def add_country(self, country: str):
        """Add new production country."""
        if country is not None:
            if 'country' not in self.__data:
                self.__data['country'] = {country}
            else:
                self.__data['country'].add(country)

    @property
    def writter(self) -> typing.Iterable[str]:
        """Get list of writter."""
        return self.__data.get('credits', {})

    def add_writter(self, writter: str):
        """Add new writter."""
        if writter is not None:
            if 'credits' not in self.__data:
                self.__data['credits'] = {writter}
            else:
                self.__data['credits'].add(writter)

    @property
    def director(self) -> typing.Iterable[str]:
        """Get list of director."""
        return self.__data.get('director', {})

    def add_director(self, director: str):
        """Add new director."""
        if director is not None:
            if 'director' not in self.__data:
                self.__data['director'] = {director}
            else:
                self.__data['director'].add(director)

    @property
    def premiered(self) -> typing.Optional[str]:
        """Get premierered date."""
        return self.__data.get('premiered', None)

    @premiered.setter
    def premiered(self, release_date: typing.Optional[str]):
        if release_date is not None:
            self.__data['premiered'] = release_date

    @property
    def studio(self) -> typing.Sequence[str]:
        """Get production studios."""
        return self.__data.get('studio', {})

    def add_studio(self, studio: typing.Optional[str]):
        """Add new studio."""
        if studio is not None:
            if 'studio' in self.__data:
                self.__data['studio'].add(studio)
            else:
                self.__data['studio'] = {studio}

    @property
    def actors(self) -> typing.Sequence[ActorType]:
        """Get actors."""
        return self.__data.get('actor', [])

    def add_actor(self, name: str, role: str, order: int, thumb: str):
        """Add new actor."""
        if 'actor' not in self.__data:
            self.__data['actor'] = []

        self.__data['actor'].append({
            'name': name,
            'role': role,
            'order': order,
            'thumb': thumb
        })

    @property
    def technical(self):
        """Retrieve technical information on movie file."""
        return self.__data.get('fileinfo', {})

    def set_technical(self, video: Video):
        """Add technical information."""
        self.__data['fileinfo'] = {
            'streamdetails': {
                'video': video.video,
                'audio': video.audio,
                'subtitle': video.subtitle
            }
        }

    def nfo(self, filehandle):
        """Write NFO data in stream-compatible object."""
        root = etree.Element('movie')
        dispatch = {
            'ratings': self._handle_ratings,
            'thumb': self._handle_thumb,
            'uniqueid': self._handle_uniqueid,
            'actor': self._handle_actors,
            'fileinfo': self._handle_fileinfo,
        }
        for key, value in self.__data.items():
            if key in dispatch:
                dispatch[key](root, value)
                continue
            if isinstance(value, (str, int)):
                element = etree.SubElement(root, key)
                element.text = value
                continue
            if isinstance(value, (list, set)):
                for item in value:
                    element = etree.SubElement(root, key)
                    element.text = item
                continue

        filehandle.write(etree.tounicode(root, pretty_print=True))

    @staticmethod
    def _handle_ratings(movie, values):
        element = etree.SubElement(movie, 'ratings')
        for name, content in values.items():
            rating = etree.SubElement(element, 'rating')
            rating.attrib['name'] = name
            if 'max' in content:
                rating.attrib['max'] = str(content['max'])
            if 'default' in content:
                rating.attrib['default'] = str(content['default'])
            value = etree.SubElement(rating, 'value')
            value.text = str(content['values'])
            if 'votes' in content:
                votes = etree.SubElement(rating, 'votes')
                votes.text = str(content['votes'])

    @staticmethod
    def _handle_thumb(movie, values):
        for aspect, url in values.items():
            thumb = etree.SubElement(movie, 'thumb', aspect=aspect)
            thumb.text = url

    @staticmethod
    def _handle_uniqueid(movie, values):
        default = True
        for idtype, value in values.items():
            uniqueid = etree.SubElement(
                movie, 'uniqueid', type=idtype, default=str(default)
            )
            uniqueid.text = value
            default = False

    @staticmethod
    def _handle_actors(movie, values):
        for actor in values:
            element = etree.SubElement(movie, 'actor')
            for item in ('name', 'role', 'order', 'thumb'):
                subelement = etree.SubElement(element, item)
                subelement.text = str(actor[item])

    @staticmethod
    def _handle_fileinfo(movie, values):
        fileinfo = etree.SubElement(movie, 'fileinfo')
        streamdetails = etree.SubElement(fileinfo, 'streamdetails')
        for video in values['streamdetails']['video']:
            stream = etree.SubElement(streamdetails, 'video')
            for item in (
                    'codec', 'aspect', 'width', 'height', 'durationinseconds'
            ):
                etree.SubElement(stream, item).text = str(video[item])
        for audio in values['streamdetails']['audio']:
            stream = etree.SubElement(streamdetails, 'audio')
            for item in (
                    'codec', 'language', 'channels'
            ):
                etree.SubElement(stream, item).text = str(audio[item])
        for sub in values['streamdetails']['subtitle']:
            stream = etree.SubElement(streamdetails, 'subtitle')
            for item in ('language',):
                etree.SubElement(stream, item).text = str(sub[item])