def sync_playback_movies(self, last=0):
        last_at = TraktAPI.utc2timestamp(last)
        playback = self.get_playback_movies({})
        debug('paused at {}'.format(len(playback)))
        sync = {}
        tr = []
        for m in playback:
            paused_at = TraktAPI.utc2timestamp(m['paused_at'])
            if paused_at is None or last_at is None or paused_at >= last_at:
                sync.update({m['movie']['ids']['trakt']: m})
                tr.append(m['movie']['ids']['trakt'])
        if len(sync):
            res = Sc.post('/Ftrakt2sc', data={'trakt': dumps(tr), 't': 1})
            debug('sync {} / {}     {}'.format(len(sync), len(res), res))
            max = len(res)
            dialog = dprogressgb()
            dialog.create('sync_playback_movies Trakt.Movies')
            for pos, scid in enumerate(res):
                if self.monitor.abortRequested():
                    return

                debug('trakt {}%'.format(int(pos / max * 100)))
                dialog.update(int(pos / max * 100))
                itm = res.get(scid)
                item = SCKODIItem(scid, trakt=itm['trakt'])
                tr = sync.get(int(itm['trakt']))
                progress = float(sync.get(int(itm['trakt']))['progress'])
                try:
                    watched = int(
                        float(itm['duration']) * float(progress / 100))
                except:
                    watched = 0
                debug('itm {} {} {} {} {} {}'.format(scid, itm,
                                                     itm['duration'], watched,
                                                     progress, tr))
                item.set_watched(watched)
                d = datetime.fromtimestamp(
                    self.utc2timestamp(tr.get('paused_at')))
                item.set_last_played(d.strftime('%Y-%m-%d %H:%M:%S'))
            dialog.close()
        pass
Beispiel #2
0
class SCPlayer(Player):
    def __init__(self):
        self.settings = settings
        self.win = home_win
        self.current_time = 0
        self.item = None
        self.is_my_plugin = False
        self.movie = None
        self.my_id = None
        self.total_time = 0
        self.ids = {}
        self.watched = False
        self.up_next = False

    def onPlayBackStarted(self):
        self.onAVStarted()

    def set_item(self, item=None):
        self.up_next = False
        # self.item = item
        if not self.win.getProperty('SC.play_item'):
            return
        item_data = loads(self.win.getProperty('SC.play_item'))
        self.win.clearProperty('SC.play_item')
        self.item = item_data.get('info')
        debug('ITEM: {}'.format(self.item.get('info', {}).get('unique_ids')))
        linfo = item_data.get('strms').get('linfo')
        ids = self.win.getProperty('{}.ids'.format(ADDON_ID))
        self.win.clearProperty('{}.ids'.format(ADDON_ID))
        self.ids = loads(ids) if ids else {}
        self.my_id = self.ids.get('sc') if self.ids.get('sc') else None
        debug('my ids: {}'.format(self.ids))
        if self.my_id is not None:
            self.win.setProperty('{}.play'.format(ADDON_ID), '1')
            debug('je to moj plugin')
            self.is_my_plugin = True
            series = self.item['info'].get('season')
            episode = self.item['info'].get('episode')
            self.movie = SCKODIItem(self.my_id,
                                    series=series,
                                    episode=episode,
                                    trakt=self.ids.get('trakt'))
            self.movie.scrobble(self.percent_played(),
                                SCKODIItem.SCROBBLE_START)
            audio = self.getAvailableAudioStreams()
            if len(audio) == 1:
                debug('Nemame na vyber, mame len jednu audio stopu')
                return

            if linfo:
                audio = linfo
            debug('AvailableAudioStreams {}'.format(len(audio)))
            lang1 = settings.get_setting('stream.lang1').lower()
            lang2 = settings.get_setting('stream.lang2').lower()
            if Sc.parental_control_is_active():
                lang1 = settings.get_setting('parental.control.lang1').lower()
                lang2 = settings.get_setting('parental.control.lang2').lower()

            plf = Storage(SC.ITEM_PREFERRED_LANG)
            plf.load(True)
            debug('PREF LANGS: {} / {}'.format(self.my_id, plf.data))
            force_lang = plf.get(self.my_id)
            force = False
            if force_lang is not None:
                lang = force_lang.lower()
                debug('mame force lang {}'.format(force_lang))
                force = self.try_audio(lang, audio)

            if force is False and self.try_audio(lang1, audio) is False:
                self.try_audio(lang2, audio)

    def try_audio(self, lang, streams):
        if lang == 'sk':
            language_list = ['slo', 'sk', 'slk', 'SK']
        elif lang == 'cz':
            language_list = ['cze', 'cz', 'ces', 'CZ']
        elif lang == 'en':
            language_list = ['eng', 'en', 'EN']
        else:
            debug("iny jazyk {}".format(lang))
            language_list = [lang.lower(), lang.upper()]

        for i in language_list:
            if i in streams:
                debug("mame audio: {} pre jazyk {}".format(i, lang))
                stream_number = streams.index(i)
                self.setAudioStream(stream_number)
                # dnotify(lang, '', time=1000, sound=False)
                return True
        return False

    def onAVStarted(self):
        debug('player onAVStarted')
        for i in range(0, 500):
            if self.isPlayback():
                break
            else:
                debug('not playing')
                sleep(1000)
        self.set_item()

    def onAVChange(self):
        debug('player onAVChange')
        if self.is_my_plugin is True:
            debug('moj plugin')

    def onPlayBackEnded(self):
        debug('player onPlayBackEnded')
        self.end_playback()

    def onPlayBackStopped(self):
        debug('player onPlayBackStopped')
        self.end_playback()

    def onPlayBackError(self):
        debug('player onPlayBackError')
        self.end_playback()

    def onPlayBackPaused(self):
        debug('player onPlayBackPaused')
        self.end_playback()

    def onPlayBackResumed(self):
        debug('player onPlayBackResumed')
        if self.movie is not None:
            self.movie.scrobble(self.percent_played(),
                                SCKODIItem.SCROBBLE_START)

    def onQueueNextItem(self):
        debug('player onQueueNextItem')

    def onPlayBackSpeedChanged(self, speed):
        debug('player onPlayBackSpeedChanged {}'.format(speed))

    def onPlayBackSeek(self, time, seekOffset):
        debug('player onPlayBackSeek {} {}'.format(time, seekOffset))

    def onPlayBackSeekChapter(self, chapter):
        debug('player onPlayBackSeekChapter {}'.format(chapter))

    def clean(self):
        debug('player SCPlayer Clean')
        #
        self.win.clearProperty('{}.play'.format(ADDON_ID))
        self.current_time = 0
        self.ids = {}
        self.is_my_plugin = False
        self.item = None
        self.movie = None
        self.my_id = None
        self.up_next = False
        self.total_time = 0
        self.watched = False

    def end_playback(self):
        self.set_watched()
        self.clean()

    def percent_played(self):
        try:
            return self.current_time / self.total_time * 100
        except:
            return 0

    def set_watched(self):
        if self.is_my_plugin:
            self.win.setProperty('{}.stop'.format(ADDON_ID), '1')
            percent_played = self.percent_played()

            self.movie.scrobble(percent_played, SCKODIItem.SCROBBLE_STOP)

            if percent_played > 80:
                play_count = self.movie.get_play_count()
                play_count = int(
                    play_count) + 1 if play_count is not None else 1
                debug('playcount {}'.format(play_count))
                d = datetime.fromtimestamp(time())
                self.movie.set_play_count(play_count, True)
                self.movie.set_last_played(d.strftime('%Y-%m-%d %H:%M:%S'))
            if 3 < percent_played < 80:
                debug('watched {}'.format(self.current_time))
                self.movie.set_watched(self.current_time)

    def check_up_next(self, percent_played):
        return gcv('System.hasAddon(service.upnext)'
                   ) and self.item and self.item['info'].get(
                       'episode') is not None and self.up_next is False

    def send_up_next(self):
        try:
            series = self.item['info'].get('season')
            episode = self.item['info'].get('episode')
            url = '/upNext/{}/{}/{}'.format(self.my_id, series, episode)
            debug('upnext call {}'.format(url))
            data = Sc.get(url)
            if 'info' in data:
                debug('Mame next item: {}'.format(data))
                d = SCUpNext(data)
                # sleep(3 * 1000)
                upnext_signal(ADDON_ID, d.get())
        except:
            debug('send_up_next ERR {}'.format(traceback.format_exc()))

    def run(self):
        debug('START player bg service')
        m = Monitor()
        while not m.abortRequested():
            sleep(1000)
            try:
                self.periodical_check()
            except:
                debug('player bg service ERR {}'.format(
                    traceback.format_exc()))
        debug('END player bg service')

    def getTime1(self):  # type: () -> float
        try:
            return self.getTime()
        except:
            return 0

    def isPlayingVideo1(self):  # type: () -> bool
        try:
            return self.isPlayingVideo()
        except:
            return False

    def periodical_check(self):
        if not self.isPlayback() or self.is_my_plugin is False:
            return

        self.current_time = self.getTime()
        self.total_time = self.getTotalTime()
        try:
            percent_played = self.current_time / self.total_time * 100
        except:
            percent_played = 0
        # debug('self.watched: {} {}'.format(self.watched, percent_played))
        if percent_played >= 80 and not self.watched:
            self.set_watched()
            self.watched = True
        if self.check_up_next(percent_played):
            debug('Mame upnext nainstalovane')
            try:
                self.send_up_next()
            except:
                pass
            self.up_next = True

    def isPlayback(self):  # type: () -> bool
        return self.isPlaying() and self.isPlayingVideo(
        ) and self.getTime() >= 0