Exemple #1
0
    def update_tracking(self, state):
        if self.media_data.type == "Radio":
            return

        if self.history_id == 0 or state.state != PlayerState.Playing or current_time(
        ) - self.last_tracking_update < 5000:
            return

        if state.playing_for > state.length - (
                state.length *
                0.04) or state.length - state.playing_for < 10000:
            if Settings.get_bool("slave"):
                SlaveClientController.notify_master("update_watching_item",
                                                    self.history_id,
                                                    state.length, state.length,
                                                    current_time())
            else:
                Database().update_watching_item(self.history_id, state.length,
                                                state.length, current_time())
        else:
            if Settings.get_bool("slave"):
                SlaveClientController.notify_master("update_watching_item",
                                                    self.history_id,
                                                    state.playing_for,
                                                    state.length,
                                                    current_time())
            else:
                Database().update_watching_item(self.history_id,
                                                state.playing_for,
                                                state.length, current_time())
        self.last_tracking_update = current_time()
Exemple #2
0
    def start_timing(self, name):
        if not Settings.get_bool("state_logging"):
            return

        if name not in self.timings:
            self.timings[name] = TimingData(name)
        self.timings[name].start()
Exemple #3
0
    def media_selection_required(self, files):
        if Settings.get_bool("slave"):
            data, = SlaveClientController.request_master(
                "get_history_for_url", 5, self.torrent.uri)
            if data:
                history = [
                    History(x['id'], x['imdb_id'], x['type'], x['title'],
                            x['image'], x['watched_at'], x['season'],
                            x['episode'], x['url'], x['media_file'],
                            x['played_for'], x['length'])
                    for x in json.loads(data)
                ]
            else:
                history = []
        else:
            history = Database().get_history_for_url(self.torrent.uri)

        for file in files:
            seen = [x for x in history if x.media_file == file.path]
            file.seen = len(seen) > 0
            if file.seen:
                seen = seen[-1]
                file.played_for = seen.played_for
                file.play_length = seen.length

        APIController().ui_request("SelectMediaFile", self.set_media_file,
                                   60 * 30, files)
Exemple #4
0
    def get_by_id_internal(show_id):
        Logger().write(LogVerbosity.Debug, "Get show by id " + show_id)
        response = RequestFactory.make_request(ShowController.shows_api_path +
                                               "show/" + show_id)
        data = json.loads(response.decode('utf-8'))

        seen_episodes = []
        data['favorite'] = False
        if not Settings.get_bool("slave"):
            seen_episodes = Database().get_history_for_id(show_id)
            data['favorite'] = show_id in [
                x.id for x in Database().get_favorites()
            ]
        for episode in data['episodes']:
            seen = [
                x for x in seen_episodes if episode['season'] == x.season
                and episode['episode'] == x.episode
            ]
            episode['seen'] = len(seen) != 0
            if len(seen) == 0:
                continue
            seen = seen[-1]
            episode['seen'] = True
            episode['played_for'] = seen.played_for
            episode['length'] = seen.length
        return data
Exemple #5
0
    def __init__(self):
        self.media_data = MediaData()
        self.torrent_data = TorrentData()

        self.torrent = None
        self.subtitle_provider = SubtitleProvider()
        self.next_episode_manager = NextEpisodeManager()
        self.play_position = 0
        self.play_length = 0

        self.history_id = 0
        self.last_tracking_update = 0

        self.dht_enabled = Settings.get_bool("dht")
        if self.dht_enabled:
            self.dht = DHTEngine()
            self.dht.start()

        EventManager.register_event(EventType.AbortingTorrent,
                                    self.aborting_torrent)
        EventManager.register_event(EventType.TorrentMediaSelectionRequired,
                                    self.media_selection_required)
        EventManager.register_event(EventType.TorrentMediaFileSet,
                                    lambda x: self._start_playing_torrent())
        EventManager.register_event(EventType.TorrentStopped,
                                    lambda: self.on_torrent_stopped())

        VLCPlayer().player_state.register_callback(self.player_state_change)
        self.torrent_observer = CustomThread(self.observe_torrent,
                                             "Torrent observer")
        self.torrent_observer.start()
        self.next_epi_thread = None
Exemple #6
0
 def __init__(self, parent, name):
     self._logging = Settings.get_bool("state_logging")
     if self._logging:
         parent_id = 0
         if parent is not None:
             parent_id = parent.log_tracker.id
         self.log_tracker = LogItemTracker(parent_id, name)
Exemple #7
0
    def start_file(self, url, time):
        actual_url = url
        if Settings.get_bool("slave"):
            actual_url = "http://" + Settings.get_string(
                "master_ip") + ":50015/file/" + urllib.parse.quote(url)

        self.stop_play()
        VLCPlayer().play(actual_url, time)
        if Settings.get_bool("slave"):
            self.history_id, = SlaveClientController.request_master(
                "add_watched_file", 5, url, current_time())
        else:
            self.history_id = Database().add_watched_file(url, current_time())
        self.media_data.start_update()
        self.media_data.type = "File"
        self.media_data.title = os.path.basename(url)
        self.media_data.url = url
        self.media_data.image = None
        self.media_data.stop_update()
        TVManager().switch_input_to_pi()
Exemple #8
0
 def start_url(self, title, url):
     self.stop_play()
     VLCPlayer().play(url, 0)
     if Settings.get_bool("slave"):
         self.history_id, = SlaveClientController.request_master(
             "add_watched_url", 5, url, current_time())
     else:
         self.history_id = Database().add_watched_url(url, current_time())
     self.media_data.start_update()
     self.media_data.type = "Url"
     self.media_data.title = title
     self.media_data.stop_update()
     TVManager().switch_input_to_pi()
Exemple #9
0
    def start(self):
        APIController.slaves = SlaveCollection()

        log_verbosity = Settings.get_int("log_level")
        if log_verbosity > 0:
            flask_logger = logging.getLogger('werkzeug')
            flask_logger.setLevel(logging.INFO)

        if Settings.get_bool("slave"):
            thread = CustomThread(self.internal_start_slave, "API controller",
                                  [])
            thread.start()
        else:
            thread = CustomThread(self.internal_start_master, "API controller",
                                  [])
            thread.start()
Exemple #10
0
 def _start_playing_torrent(self):
     if Settings.get_bool("slave"):
         self.history_id, = SlaveClientController.request_master(
             "add_watched_torrent", 5, self.media_data.type,
             self.media_data.title, self.media_data.id, self.torrent.uri,
             self.torrent.media_file.path, self.media_data.image,
             self.media_data.season, self.media_data.episode,
             current_time())
     else:
         self.history_id = Database().add_watched_torrent(
             self.media_data.type, self.media_data.title,
             self.media_data.id, self.torrent.uri,
             self.torrent.media_file.path, self.media_data.image,
             self.media_data.season, self.media_data.episode,
             current_time())
     VLCPlayer().play("http://localhost:50009/torrent",
                      self.media_data.start_from)
Exemple #11
0
 def update_subtitles(self, new_state):
     media_type = self.media_data.type
     if media_type == "File":
         if Settings.get_bool("slave"):
             SlaveClientController.request_master_cb(
                 "get_file_info", self.process_file_info_for_subtitles, 5,
                 self.media_data.url)
         else:
             size, first_64k, last_64k = get_file_info(self.media_data.url)
             EventManager.throw_event(EventType.SearchSubtitles, [
                 self.media_data.title, size,
                 VLCPlayer().get_length(), first_64k, last_64k
             ])
     elif media_type == "Show" or media_type == "Movie" or media_type == "Torrent":
         EventManager.throw_event(EventType.SearchSubtitles, [
             os.path.basename(self.torrent.media_file.name),
             self.torrent.media_file.length,
             VLCPlayer().get_length(), self.torrent.media_file.first_64k,
             self.torrent.media_file.last_64k
         ])
Exemple #12
0
    def try_find_in_dir(self, media_data):
        season, epi = try_parse_season_episode(media_data.url)
        if season == 0 or epi == 0:
            Logger().write(LogVerbosity.Debug,
                           "No next episode of file, season/epi not parsed")
            return

        dir_name = os.path.dirname(media_data.url)
        if Settings.get_bool("slave"):
            data, = SlaveClientController.request_master(
                "get_directory", 5, dir_name)
            if not data:
                return
            data = json.loads(data)
            file_list = data["file_names"]
        else:
            file_list = FileStructure(dir_name).file_names

        for potential in file_list:
            if not is_media_file(potential):
                continue

            s, e = try_parse_season_episode(potential)
            if s == season and e == epi + 1:
                Logger().write(LogVerbosity.Info,
                               "Found next episode: " + potential)
                self.next_season = s
                self.next_episode = epi + 1
                self.next_type = "File"
                self.next_title = potential
                self.next_path = dir_name + "/" + potential
                return
        Logger().write(
            LogVerbosity.Debug,
            "No next episode of file, no matching next season/epi found in file list"
        )
        return
Exemple #13
0
    def stop_timing(self, name):
        if not Settings.get_bool("state_logging"):
            return

        self.timings[name].stop()
Exemple #14
0
 def __init__(self):
     self.path = Settings.get_string("base_folder") + "Solution/"
     self.db_name = "database.data"
     self.slave = Settings.get_bool("slave")
     self.current_version = 13
Exemple #15
0
 def __init__(self):
     self.slave = Settings.get_bool("slave")
     self.ui_websocket_controller = None
     self.slave_websocket_controller = None
Exemple #16
0
    def start(self):
        if Settings.get_bool("slave"):
            return

        self.running = True
        self.check_thread.start()
Exemple #17
0
    def update(self):
        Logger().write(LogVerbosity.Important, "Starting update")
        if Settings.get_bool("slave"):
            self.check_version()

        start_time = current_time()
        self.update_state.start()
        path = self.update_folder + self.last_version

        if os.path.exists(path):
            Logger().write(LogVerbosity.Info, "Removing old update")
            self.update_state.set_state("Removing previous update")
            shutil.rmtree(path, onerror=self.on_remove_error)

        os.makedirs(path)

        self.update_state.set_state("Cloning git repo")
        success = self.execute_command("Git cloning", [
            "git", "clone", "-b", self.git_branch, "--single-branch",
            self.git_repo
        ],
                                       cwd=path)
        if not success:
            self.update_state.set_complete(error="Failed to clone git repo")
            return

        data_path = path + "/MediaPi/src/"
        ui_path = data_path + "UI/homebase"

        if not Settings.get_bool("slave"):
            # Only need to build UI when we're not slave
            self.update_state.set_state("Restoring UI packages")
            success = self.execute_command("UI package restore",
                                           ["npm", "install"],
                                           cwd=ui_path,
                                           shell=True)
            if not success:
                self.update_state.set_complete(
                    error="UI package restore failed")
                return

            self.update_state.set_state("Building UI")
            success = self.execute_command("UI build", ["npm", "run", "build"],
                                           cwd=ui_path,
                                           shell=True)
            if not success:
                self.update_state.set_complete(error="UI build failed")
                return

        self.copied_files = 0
        self.update_state.set_state("Copying files")
        Logger().write(LogVerbosity.Info, "Starting copying of files")
        start = current_time()
        self.copy_directory(data_path, self.base_folder)
        Logger().write(
            LogVerbosity.Info, "Copied " + str(self.copied_files) +
            " files in " + str(current_time() - start) + "ms")

        if not Settings.get_bool("slave"):
            self.update_state.set_state("Copying UI files")
            Logger().write(LogVerbosity.Info, "Starting copying of UI files")
            self.copied_files = 0
            start = current_time()
            self.copy_directory(ui_path + "/build/",
                                self.base_folder + "UI/homebase/")
            Logger().write(
                LogVerbosity.Info, "Copied " + str(self.copied_files) +
                " files in " + str(current_time() - start) + "ms")

        self.update_state.set_state("Removing temp directory")
        Logger().write(LogVerbosity.Info, "Removing temp directory")
        shutil.rmtree(path, onerror=self.on_remove_error)

        Logger().write(
            LogVerbosity.Important,
            "Update completed in " + str(current_time() - start_time) + "ms")
        Database().update_stat("CurrentGitVersion", self.last_version)
        Database().update_stat("LastUpdate", current_time())

        self.update_state.completed = True
        self.update_state.state = "Restarting"
        self.update_state.changed()
        Logger().write(LogVerbosity.Important, "Restarting to complete update")
        if sys.platform == "linux" or sys.platform == "linux2":
            os.system('sudo reboot')
Exemple #18
0
    def remove(self, name):
        if not Settings.get_bool("state_logging"):
            return

        self.timings[name].remove()
        del self.timings[name]
Exemple #19
0
    def __init__(self):
        Logger().start(Settings.get_int("log_level"))
        sys.excepthook = self.handle_exception

        Logger().write(LogVerbosity.Info, "Starting")
        self.running = True

        self.version = datetime.fromtimestamp(
            self.get_latest_change()).strftime("%Y-%m-%d %H:%M:%S")
        self.is_slave = Settings.get_bool("slave")
        self.pi = sys.platform == "linux" or sys.platform == "linux2"

        Logger().write(
            LogVerbosity.Info, "Python version " + str(sys.version_info[0]) +
            "." + str(sys.version_info[1]) + "." + str(sys.version_info[2]))
        Logger().write(LogVerbosity.Info,
                       "MediaPlayer build [" + self.version + "]")
        Logger().write(LogVerbosity.Info, "Slave: " + str(self.is_slave))
        if self.is_slave:
            Logger().write(
                LogVerbosity.Info,
                "Master ip: " + str(Settings.get_string("master_ip")))
        Logger().write(LogVerbosity.Info, "Pi: " + str(self.pi))
        Logger().write(LogVerbosity.Info,
                       "UI: " + str(Settings.get_bool("UI")))

        Logger().write(LogVerbosity.Debug, "Initializing database")
        Database().init_database()

        Logger().write(LogVerbosity.Debug, "Initializing singletons")
        self.init_singletons()

        Logger().write(LogVerbosity.Debug, "Initializing sounds and folders")
        self.init_sound()
        self.init_folders()

        Logger().write(LogVerbosity.Debug, "Initializing API")
        APIController().start()

        Logger().write(LogVerbosity.Debug, "Initializing WiFi controller")
        WiFiController().check_wifi()

        Logger().write(LogVerbosity.Debug, "Initializing stats")
        Stats().start()
        Stats().set('start_time', current_time())

        Logger().write(LogVerbosity.Debug, "Initializing presence manager")
        PresenceManager().start()

        Logger().write(LogVerbosity.Debug, "Initializing rule manager")
        RuleManager().start()

        Logger().write(LogVerbosity.Debug, "Initializing TV manager")
        TVManager().start()

        if not self.is_slave:
            Logger().write(LogVerbosity.Debug, "Initializing TradeFriManager")
            TradfriManager().init()

            Logger().write(LogVerbosity.Debug,
                           "Initializing master file server")
            self.file_listener = StreamListener("MasterFileServer", 50015)
            self.file_listener.start_listening()

        Logger().write(LogVerbosity.Important, "Started")
        if Settings.get_bool("UI"):
            from UI.TV.GUI import App
            self.gui = App.initialize()
        else:
            while self.running:
                time.sleep(5)