Exemplo n.º 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()
Exemplo n.º 2
0
    def internal_start_master(self):
        from Webserver.Controllers.AuthController import AuthController
        from Webserver.Controllers.AccessController import AccessController
        from Webserver.Controllers.MediaPlayer.MovieController import MovieController
        from Webserver.Controllers.MediaPlayer.ShowController import ShowController
        from Webserver.Controllers.MediaPlayer.PlayController import PlayController
        from Webserver.Controllers.MediaPlayer.HDController import HDController
        from Webserver.Controllers.MediaPlayer.RadioController import RadioController
        from Webserver.Controllers.MediaPlayer.TorrentController import TorrentController
        from Webserver.Controllers.MediaPlayer.YouTubeController import YouTubeController
        from Webserver.Controllers.DataController import DataController
        from Webserver.Controllers.Home.TradfriController import TradfriController
        from Webserver.Controllers.Home.ToonController import ToonController
        from Webserver.Controllers.UtilController import UtilController
        from Webserver.Controllers.Home.RuleController import RuleController
        from Webserver.Controllers.Websocket2.UIWebsocketController import UIWebsocketController
        from Webserver.Controllers.Websocket2.SlaveWebsocketController import SlaveWebsocketController

        self.ui_websocket_controller = UIWebsocketController("/UI")
        self.slave_websocket_controller = SlaveWebsocketController("/Slave")
        UIWebsocketController.init()
        YouTubeController.init()

        socketio.on_namespace(self.ui_websocket_controller)
        socketio.on_namespace(self.slave_websocket_controller)

        APIController.slaves.add_slave(
            SlaveClient(1, Settings.get_string("name"), None))

        socketio.run(app,
                     host='0.0.0.0',
                     port=int(Settings.get_string("api_port")),
                     log_output=True)
Exemplo n.º 3
0
    def __init__(self, peer, uri):
        super().__init__(peer, "connection")

        self.peer = peer
        self.uri = uri
        self.to_send_bytes = bytearray()
        self.last_send = 0
        self.connected_on = 0
        self._last_communication = 0
        self._peer_timeout = Settings.get_int("peer_timeout")
        self._connection_timeout = Settings.get_int("connection_timeout") / 1000

        self.connection = TcpClient(uri.hostname, uri.port, self._connection_timeout)
        self.buffer = bytearray()

        self.buffer_in_size = 0
        self.buffer_out_size = 0

        self._next_message_length = 1
        self._buffer_position = 0
        self._receive_state = ReceiveState.ReceiveLength
        self._receive_buffer_size = 32768

        self.in_thread = None
        self.reading_handshake = True
Exemplo n.º 4
0
 def __init__(self, host, port):
     self.host = host
     self.port = port
     self.last_announce = 0
     self.transaction_id = 0
     self.connection_id = 0
     self.connection_id_retrieved = 0
     self.connection = UdpClient(host, port, Settings.get_int("connection_timeout") / 1000)
     self.tracker_peer_request_amount = Settings.get_int("tracker_peer_request_amount")
Exemplo n.º 5
0
 def __init__(self):
     self.git_repo = "https://github.com/jkorf/MediaPi.git"
     self.git_branch = "NewUI"
     self.base_folder = Settings.get_string("base_folder")
     self.update_folder = Settings.get_string("base_folder") + "Updates/"
     self.ignore_directories = ("/Solution", "/UI/homebase", "/UI/Web")
     self.copied_files = 0
     self.last_version = Database().get_stat_string("CurrentGitVersion")
     self.last_update = Database().get_stat("LastUpdate")
     self.update_state = UpdateState(self.last_version, self.last_update)
Exemplo n.º 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)
Exemplo n.º 7
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()
Exemplo n.º 8
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()
Exemplo n.º 9
0
    def __init__(self, torrent):
        super().__init__(torrent, "download")

        self.torrent = torrent

        self.init = False
        self.prio = False
        self.last_get_result = 0, 0
        self.last_get_time = 0

        self.queue = []
        self.slow_peer_piece_offset = 0
        self._ticks = 0

        self.start_piece = 0
        self.end_piece = 0
        self.stream_end_buffer_pieces = 0
        self.stream_play_buffer_high_priority = 0
        self.max_chunk_size = Settings.get_int("max_chunk_size")

        self.download_mode = DownloadMode.Full
        self.peers_per_piece = [(100, 2, 5), (95, 1, 2), (0, 1, 1)]

        self._event_id_stopped = EventManager.register_event(
            EventType.TorrentStopped, self.unregister)
        self._event_id_torrent_state = EventManager.register_event(
            EventType.TorrentStateChange, self.init_queue)

        self.queue_log = ""
Exemplo n.º 10
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
Exemplo n.º 11
0
    def __init__(self):
        self.subtitle_sources = [
            SubtitlesOpenSubtitles(),
        ]

        self.sub_file_directory = Settings.get_string("base_folder") + "/subs/"
        self.sub_files = []

        self.file_size = 0
        self.file_length = 0
        self.file_name = None
        self.first_64k = None
        self.last_64k = None

        # create subtitles directory
        if not os.path.exists(self.sub_file_directory):
            os.makedirs(self.sub_file_directory)

        # remove old subtitles files
        file_list = glob.glob(self.sub_file_directory + "*.srt")
        for f in file_list:
            os.remove(f)

        EventManager.register_event(EventType.SearchSubtitles,
                                    self.search_subtitles)
Exemplo n.º 12
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)
Exemplo n.º 13
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
Exemplo n.º 14
0
    def connect():
        SlaveClientController.running = True
        Logger().write(LogVerbosity.Debug, "Connecting to master")
        SlaveClientController.socket = SocketIO(Settings.get_string("master_ip"), port=int(Settings.get_string("api_port")))
        SlaveClientController.slave_ns = SlaveClientController.socket.define(Handler, "/Slave")

        while SlaveClientController.running:
            SlaveClientController.socket.wait(1)
Exemplo n.º 15
0
    def __init__(self, peer):
        super().__init__(peer, "meta")
        self.peer = peer

        self.handshake_send = False
        self.handshake_successful = False
        self.extension_handshake_send = False
        self.bitfield_done = False
        self.metadata_requested = False
        self.pause_handled = False
        self.port_send = False
        self.last_peer_exchange = current_time()

        self._low_peer_max_speed = Settings.get_int(
            "low_peer_max_download_buffer") / 3
        self._medium_peer_max_speed = Settings.get_int(
            "medium_peer_max_download_buffer") / 3
Exemplo n.º 16
0
 def __init__(self):
     self.state_data = StateData()
     self.monitoring = sys.platform == "linux" or sys.platform == "linux2"
     self.state_data.name = Settings.get_string("name")
     self.watch_thread = CustomThread(self.update_state, "State observer")
     self.watch_thread.start()
     self.memory_thread = CustomThread(self.check_memory, "Memory observer")
     self.memory_thread.start()
Exemplo n.º 17
0
 def get_log_files():
     list_of_files = glob.glob(
         Settings.get_string("base_folder") + "/Logs/*/*.txt")
     latest_files = sorted(list_of_files,
                           key=os.path.getctime,
                           reverse=True)
     return [(os.path.basename(x), x, os.path.getsize(x))
             for x in latest_files]
Exemplo n.º 18
0
 def __init__(self, torrent):
     super().__init__(torrent, "cache")
     self.open_cache_files = []
     self.piece_length = 0
     self.total_length = 0
     self.media_start_byte = 0
     self.media_start_piece_offset = 0
     self.path = Settings.get_string("base_folder") + "Solution/cache/"
     self.bytes_written = 0
Exemplo n.º 19
0
    def get_instance_parameters():
        params = [
            "--verbose=" + str(Settings.get_int("vlc_log_level")),
            "--network-caching=" + str(Settings.get_int("network_caching")),
            "--ipv4-timeout=500", "--image-duration=-1"
        ]

        if sys.platform == "linux" or sys.platform == "linux2":
            log_path = Settings.get_string(
                "base_folder") + "/Logs/" + datetime.datetime.now().strftime(
                    '%Y-%m-%d %H-%M-%S')
            params.append(
                "--logfile=" + log_path + '/vlc_' +
                datetime.datetime.now().strftime('%Y-%m-%d %H-%M-%S') + ".txt")
            params.append("--file-logging")
            params.append("--file-caching=5000")

        return params
Exemplo n.º 20
0
 def cleanup_logging(self):
     try:
         base_path = Settings.get_string("base_folder") + "/Logs/"
         for f in os.listdir(base_path):
             file_path = base_path + f
             creation_time = os.stat(file_path).st_mtime
             if (current_time() / 1000 - creation_time) >= 60 * 60 * 24 * 7:
                 shutil.rmtree(file_path, True)
     except Exception as e:
         self.write_error(e, "Log cleaning failed")
Exemplo n.º 21
0
    def __init__(self, peer):
        super().__init__(peer, "download")

        self.peer = peer
        self.stopped = False
        self.downloading = []

        self._block_size = Settings.get_int("block_size")
        self._low_peer_max_blocks = Settings.get_int(
            "low_peer_max_download_buffer") // self._block_size
        self._medium_peer_max_blocks = Settings.get_int(
            "medium_peer_max_download_buffer") // self._block_size
        self._fast_peer_max_blocks = Settings.get_int(
            "fast_peer_max_download_buffer") // self._block_size

        self.timed_out_blocks = 0

        # Logging props
        self.downloading_log = ""
Exemplo n.º 22
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()
Exemplo n.º 23
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()
Exemplo n.º 24
0
    def __init__(self, base_path, file_name, print_to_output):
        self.base_path = base_path
        self.base_file_name = file_name
        self.print_to_output = print_to_output
        self.file = None
        self.file_size = 0
        self.file_number = 1
        self.max_log_file_size = Settings.get_int("max_log_file_size")

        self.running = True

        self.process_thread = None
        self.queue = []
        self.queue_event = threading.Event()
Exemplo n.º 25
0
    def __init__(self, torrent):
        super().__init__(torrent, "peers")

        self.torrent = torrent
        self.potential_peers = []
        self.connecting_peers = []
        self.connected_peers = []
        self.disconnected_peers = []
        self.cant_connect_peers = []
        self.complete_peer_list = []
        self.max_peers_connected = Settings.get_int("max_peers_connected")
        self.max_peers_connecting = Settings.get_int("max_peers_connecting")
        self._peer_request_interval = Settings.get_int("peer_request_interval")
        self._peer_request_interval_no_potential = Settings.get_int(
            "peer_request_interval_no_potential")
        self.random = Random()
        self.download_start = 0
        self.start_time = current_time()
        self.last_peer_request = 0
        self.checked_no_peers = False

        self._event_id_stopped = EventManager.register_event(
            EventType.TorrentStopped, self.unregister)
        self._event_id_torrent_change = EventManager.register_event(
            EventType.TorrentStateChange, self.torrent_state_change)
        self._event_id_peers_found = EventManager.register_event(
            EventType.PeersFound, self.add_potential_peers)

        self.high_speed_peers = 0
        self.medium_speed_peers = 0

        # Log properties
        self.potential_peers_log = 0
        self.connecting_peers_log = 0
        self.connected_peers_log = 0
        self.disconnected_peers_log = 0
        self.cant_connect_peers_log = 0
Exemplo n.º 26
0
    def init_queue(self, old_state, new_state):
        if not self.init and new_state == TorrentState.Downloading and self.torrent.media_file is not None:
            self.init = True

            self.start_piece = self.torrent.media_file.start_piece(
                self.torrent.piece_length)
            self.end_piece = self.torrent.media_file.end_piece(
                self.torrent.piece_length)
            self.stream_end_buffer_pieces = self.torrent.data_manager.get_piece_by_offset(
                self.torrent.media_file.end_byte -
                Settings.get_int("stream_end_buffer_tolerance")).index
            self.stream_play_buffer_high_priority = max(
                1500000 // self.torrent.piece_length, 2)  # TODO setting

            self.requeue(0)
Exemplo n.º 27
0
    def __init__(self, name, port, arg=None):
        super().__init__(arg, name)

        self.name = name
        self.torrent = arg

        self.port = port
        self.thread = None
        self.chunk_length = Settings.get_int("max_chunk_size")
        self.server = StreamServer(self.name, port, self.handle_request)

        self.sockets_writing_data = []

        self.running = False
        self.bytes_send = 0
        self.id = 0
Exemplo n.º 28
0
    def __init__(self, torrent):
        super().__init__(torrent, "data")
        self.torrent = torrent
        self._pieces = dict()
        self.init_done = False
        self.total_pieces = 0
        self.piece_length = 0
        self.bitfield = None
        self.hashes = []

        self.block_size = Settings.get_int("block_size")
        self.broadcasted_hash_data = False
        self._event_id_stopped = EventManager.register_event(
            EventType.TorrentStopped, self.unregister)
        self.blocks_done_length = 0
        self.last_piece_index = 0
Exemplo n.º 29
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)
Exemplo n.º 30
0
    def init():
        if Settings.get_int("log_level") == 0:
            import logging
            logging.getLogger('requests').setLevel(logging.WARNING)
            logging.basicConfig(level=logging.DEBUG)

        from MediaPlayer.MediaManager import MediaManager
        from MediaPlayer.Player.VLCPlayer import VLCPlayer
        from Updater import Updater
        from Shared.State import StateManager
        from Shared.Stats import Stats

        StateManager().state_data.register_callback(lambda old, new: SlaveClientController.broadcast("state", new))
        VLCPlayer().player_state.register_callback(lambda old, new: SlaveClientController.broadcast("player", new))
        MediaManager().media_data.register_callback(lambda old, new: SlaveClientController.broadcast("media", new))
        MediaManager().torrent_data.register_callback(lambda old, new: SlaveClientController.broadcast("torrent", new))
        Stats().cache.register_callback(lambda old, new: SlaveClientController.broadcast("stats", new))
        Updater().update_state.register_callback(lambda old, new: SlaveClientController.broadcast("update", new))