Ejemplo n.º 1
0
    def main(self):
        try:
            vlc = VLCRemote(self.vlc_ip, self.vlc_port)
            self.vlc_connected = True
        except:
            if self.vlc_connected:
                self.log.info('Could not find VLC running at ' +
                              str(self.vlc_ip) + ':' + str(self.vlc_port))
                self.log.debug('Make sure your VLC player is running with ' +
                               '--extraintf=rc --rc-host=' +
                               str(self.vlc_ip) + ':' + str(self.vlc_port) +
                               ' --rc-quiet', exc_info=sys.exc_info())
                self.vlc_connected = False

                # If we were watching a video but we didn't finish it, we
                # have to cancel the watching status
                if self.cache["watching"] > -1 and not self.cache["scrobbled"]:
                    self.trakt_client.cancelWatching(
                        self.cache["video"]["imdbid"],
                        self.get_episode(self.cache["video"]))

                # If there is something in the cache, we can purge the watching
                # and scrobbled information, so if the video is opened again we
                # will consider it's a new watch
                if self.cache['vlc_file_name'] is not None:
                    self.resetCacheView()

            return

        vlcStatus = vlc.is_playing()
        if not vlcStatus:
            vlc.close()
            return

        currentFileLength = vlc.get_length()
        if not int(currentFileLength) > 0:
            self.log.debug("main::File length is 0, can't do anything")
            vlc.close()
            return

        if self.USE_FILENAME:
            currentFileName = vlc.get_filename()
        else:
            currentFileName = vlc.get_title()
        self.vlcTime = int(vlc.get_time())

        # Parse the filename to verify if it comes from a stream
        parsed = urlparse(currentFileName)
        if parsed.netloc:
            # Set the filename using only the basename of the parsed path
            currentFileName = os.path.basename(parsed.path)

            # And use urllib's unquote to bring back special chars
            currentFileName = unquote(currentFileName)
        elif self.USE_FILENAME:
            # Even if it's not from a stream, if it's a filename we're using
            # we need to keep only the basename of the parsed path
            currentFileName = os.path.basename(currentFileName)

        if (currentFileName == self.cache["vlc_file_name"]
                and currentFileLength == self.cache['vlc_file_length']):
            if (self.cache["series_info"] is None
                    and self.cache["movie_info"] is None):
                video = None
            elif self.cache["series_info"] is not None:
                video = self.get_TV(vlc, self.cache["series_info"])
            else:
                video = self.get_Movie(vlc, self.cache["movie_info"])
        else:
            self.log.debug("main::New file: %s (%s)"
                           % (currentFileName, currentFileLength))

            # If we were watching a video but we didn't finish it, we
            # have to cancel the watching status
            if self.cache["watching"] > -1 and not self.cache["scrobbled"]:
                self.trakt_client.cancelWatching(
                    self.cache["video"]["imdbid"],
                    self.get_episode(self.cache["video"]))

            self.resetCache(currentFileName, currentFileLength)
            self.cache['started_watching'] = (time.time(), self.vlcTime)

            video = self.get_TV(vlc)
            if video is None:
                video = self.get_Movie(vlc)

        if video is None:
            self.log.info(
                "No tv show nor movie found for the current playing video")
            vlc.close()
            return

        # We cache the updated video information
        self.cache["video"] = video

        logtitle = video["title"]
        if video["tv"]:
            logtitle += (" - %01dx%02d"
                         % (int(video["season"]), int(video["episode"])))

            # If we changed episode, we have to reset the view status
            if (self.cache['watching'] > -1
                    and self.cache['series_current_ep'] != video['episode']):
                self.resetCacheView(video['episode'])
                self.cache['started_watching'] = (
                    time.time(),
                    self.vlcTime % video['duration'])

        self.log.info(logtitle + " state : " + str(video["percentage"]) + "%")
        self.log.debug("main::Video: %s" % str(video))
        self.log.debug("main::This video is scrobbled : " +
                       str(self.cache["scrobbled"]))

        if (((video['tv'] and self.DO_SCROBBLE_TV)
                or (not video['tv'] and self.DO_SCROBBLE_MOVIE))
                and video["percentage"] >= self.SCROBBLE_PERCENT
                and not self.cache["scrobbled"]
                and self.cache['started_watching'] is not None
                and ((time.time() - self.cache['started_watching'][0])
                     > (float(video['duration']) / 3.0))
                and ((self.vlcTime - self.cache['started_watching'][1])
                     > (float(video['duration']) / 4.0))):

            self.log.info("Scrobbling " + logtitle + " to Trakt...")
            try:
                self.trakt_client.stopWatching(video["imdbid"],
                                               video["percentage"],
                                               self.get_episode(video))

                self.cache["scrobbled"] = True
                self.log.info(logtitle + " scrobbled to Trakt !")
            except TraktClient.TraktError as e:
                self.log.error("An error occurred while trying to scrobble",
                               exc_info=sys.exc_info())

        elif (((video['tv'] and self.DO_WATCHING_TV)
                or (not video['tv'] and self.DO_WATCHING_MOVIE))
                and video["percentage"] < self.SCROBBLE_PERCENT
                and not self.cache["scrobbled"]
                and ((float(video["duration"]) * float(video["percentage"])
                     / 100.0) >= self.START_WATCHING_TIMER)):

            self.log.debug("main::Trying to mark " +
                           logtitle +
                           " watching on Trakt...")

            try:
                self.trakt_client.startWatching(video["imdbid"],
                                                video["percentage"],
                                                self.get_episode(video))

                self.log.info(logtitle + " is currently watching on Trakt...")
                self.cache["watching"] = video["percentage"]
            except TraktClient.TraktError as e:
                self.log.error("An error occurred while trying to mark as " +
                               "watching " + logtitle,
                               exc_info=sys.exc_info())

        vlc.close()
Ejemplo n.º 2
0
    def main(self):
        try:
            vlc = VLCRemote(self.vlc_ip, self.vlc_port)
            self.vlc_connected = True
        except:
            if self.vlc_connected:
                self.log.info('Could not find VLC running at ' +
                              str(self.vlc_ip) + ':' + str(self.vlc_port))
                self.log.debug('Make sure your VLC player is running with ' +
                               '--extraintf=rc --rc-host=' +
                               str(self.vlc_ip) + ':' + str(self.vlc_port) +
                               ' --rc-quiet', exc_info=sys.exc_info())
                self.vlc_connected = False

                # If we were watching a video but we didn't finish it, we
                # have to cancel the watching status
                if self.cache["watching"] > -1 and not self.cache["scrobbled"]:
                    self.trakt_client.cancelWatching(
                        self.cache["video"]["imdbid"],
                        self.get_episode(self.cache["video"]))

                # If there is something in the cache, we can purge the watching
                # and scrobbled information, so if the video is opened again we
                # will consider it's a new watch
                if self.cache['vlc_file_name'] is not None:
                    self.resetCacheView()

            return

        vlcStatus = vlc.is_playing()
        if not vlcStatus:
            vlc.close()
            return

        currentFileLength = vlc.get_length()
        if not int(currentFileLength) > 0:
            self.log.debug("main::File length is 0, can't do anything")
            vlc.close()
            return

        currentFilePath = vlc.get_filename()

        if self.USE_FILENAME:
            currentFileName = currentFilePath.decode('utf-8')
        else:
            currentFileName = vlc.get_title().decode('utf-8')
        self.vlcTime = int(vlc.get_time())

        # Parse the filename to verify if it comes from a stream
        parsed = urlparse(currentFileName)
        if parsed.netloc:
            # Set the filename using only the basename of the parsed path
            currentFileName = os.path.basename(parsed.path)
        elif self.USE_FILENAME:
            # Even if it's not from a stream, if it's a filename we're using
            # we need to keep only the basename of the parsed path
            currentFileName = os.path.basename(currentFileName)

        # Use urllib's unquote to bring back special chars
        currentFileName = unquote(currentFileName)

        if (currentFileName == self.cache["vlc_file_name"]
                and currentFileLength == self.cache['vlc_file_length']):
            if (self.cache["series_info"] is None
                    and self.cache["movie_info"] is None):
                video = None
            elif self.cache["series_info"] is not None:
                video = self.get_TV(vlc, self.cache["series_info"])
            else:
                video = self.get_Movie(vlc, self.cache["movie_info"])
        else:
            self.log.debug("main::New file: %s (length: %s)"
                           % (currentFileName, currentFileLength))

            # If we were watching a video but we didn't finish it, we
            # have to cancel the watching status
            if self.cache["watching"] > -1 and not self.cache["scrobbled"]:
                self.trakt_client.cancelWatching(
                    self.cache["video"]["imdbid"],
                    self.get_episode(self.cache["video"]))

            self.resetCache(currentFilePath, currentFileName,
                            currentFileLength)
            self.cache['started_watching'] = (time.time(), self.vlcTime)

            video = self.get_TV(vlc)
            if video is None:
                video = self.get_Movie(vlc)

        if video is None:
            self.log.info(
                "No tv show nor movie found for the current playing video")
            vlc.close()
            return

        # We cache the updated video information
        self.cache["video"] = video

        logtitle = video["title"]
        if video["tv"]:
            logtitle += (" - %01dx%02d"
                         % (int(video["season"]), int(video["episode"])))

            # If we changed episode, we have to reset the view status
            if (self.cache['watching'] > -1
                    and self.cache['series_current_ep'] != video['episode']):
                self.resetCacheView(video['episode'])
                self.cache['started_watching'] = (
                    time.time(),
                    self.vlcTime % video['duration'])

        self.log.info(logtitle + " state : " + str(video["percentage"]) + "%")
        self.log.debug("main::Video: %s" % str(video))
        self.log.debug("main::This video is scrobbled : " +
                       str(self.cache["scrobbled"]))

        if (((video['tv'] and self.DO_SCROBBLE_TV)
                or (not video['tv'] and self.DO_SCROBBLE_MOVIE))
                and video["percentage"] >= self.SCROBBLE_PERCENT
                and not self.cache["scrobbled"]
                and self.cache['started_watching'] is not None
                and ((time.time() - self.cache['started_watching'][0])
                     > (float(video['duration']) / 3.0))
                and ((self.vlcTime - self.cache['started_watching'][1])
                     > (float(video['duration']) / 4.0))):

            self.log.info("Scrobbling " + logtitle + " to Trakt...")
            try:
                self.trakt_client.stopWatching(video["imdbid"],
                                               video["percentage"],
                                               self.get_episode(video))

                self.cache["scrobbled"] = True
                self.log.info(logtitle + " scrobbled to Trakt !")
            except TraktClient.TraktError as e:
                self.log.error("An error occurred while trying to scrobble",
                               exc_info=sys.exc_info())

        elif (((video['tv'] and self.DO_WATCHING_TV)
                or (not video['tv'] and self.DO_WATCHING_MOVIE))
                and video["percentage"] < self.SCROBBLE_PERCENT
                and not self.cache["scrobbled"]
                and ((float(video["duration"]) * float(video["percentage"])
                     / 100.0) >= self.START_WATCHING_TIMER)):

            self.log.debug("main::Trying to mark " +
                           logtitle +
                           " watching on Trakt...")

            try:
                self.trakt_client.startWatching(video["imdbid"],
                                                video["percentage"],
                                                self.get_episode(video))

                self.log.info(logtitle + " is currently watching on Trakt...")
                self.cache["watching"] = video["percentage"]
            except TraktClient.TraktError as e:
                self.log.error("An error occurred while trying to mark as " +
                               "watching " + logtitle,
                               exc_info=sys.exc_info())

        vlc.close()