def setItem(self, url, content_id, start_point=0.0, content_type='', total_time=0, paused=False): self.current_item = url self.current_content_id = content_id self.start_from = start_point self.current_video_type = content_type self.total_time = int(total_time) self.start_paused = paused Logger.log_write("Setting item (%s - %s) Duration (%d/%d): %s Paused=%s" % (content_id, content_type, self.start_from, self.total_time, url, str(paused)), mode=Logger.LOG_PLAYER)
def __remove_folder_database_by_path_id(self, path_id): Logger.kodi_log("Removing path id: %d" % (path_id)) db_path = self.__get_database_path() conn = sqlite3.connect(db_path) cursor = conn.cursor() cursor.execute("DELETE FROM path WHERE idPath = ?", [path_id]) conn.commit() cursor.close() conn.close()
def send_keep_alive(self): ka_resp = utils.call_service("keep_alive", {"contentId": self.current_content_id}) if ka_resp != None: Logger.log_write("Keep Alive OK!", Logger.LOG_PLAYER) self.keep_alive_limit = int(ka_resp["resultObj"]["keepAlive"]) self.keep_alive_token = ka_resp["resultObj"]["token"] return True Logger.log_write("Keep Alive FAILED!", Logger.LOG_PLAYER) return False
def onCorrectTimeLoaded(self): if self.start_paused: while not self.is_paused: self.pause() xbmc.sleep(1000) while abs(self.getTime()-self.start_from) > 10: Logger.log_write("Trying to resume: %f" % (self.start_from), Logger.LOG_PLAYER) self.seekTime(self.start_from) xbmc.sleep(100)
def load_pickle(pickle_file, default=None): folder = get_data_folder() path = url_join(folder, pickle_file) try: content = default with open(path, "r") as file_stream: content = pickle.load(file_stream) file_stream.close() return content except: Logger.kodi_log("Error while loading %s" % (pickle_file)) return default
def __update_tvshows_library(self): tvshows = utils.call_service("load_all_contents", { "begin": 0, "category": "Serie" }) if tvshows == None or len(tvshows) == 0: return self.empty_tvshows_library() base_series_folder = os.path.join(self.library_folder, self.tvshows_folder) items = TimVisionObjects.parse_collection(tvshows) for tvshow in items: title_normalized = self.__normalize_path(tvshow.title) if len(title_normalized) == 0: # TODO: FIX ME continue #skip shows with unicode characters/empty title Logger.kodi_log("Library (TV): %s" % (title_normalized)) normalized_show_name = "%s (%d)" % (title_normalized, tvshow.year) show_folder = os.path.join(base_series_folder, normalized_show_name) xbmcvfs.mkdir(show_folder) seasons_json = utils.call_service( "get_show_content", { "contentId": tvshow.content_id, "contentType": TimVisionAPI.TVSHOW_CONTENT_TYPE_SEASONS }) seasons = TimVisionObjects.parse_collection(seasons_json) for season in seasons: episodes_json = utils.call_service( "get_show_content", { "contentId": season.content_id, "contentType": TimVisionAPI.TVSHOW_CONTENT_TYPE_EPISODES }) episodes = TimVisionObjects.parse_collection(episodes_json) for episode in episodes: filename = "%s S%02dE%02d" % ( normalized_show_name, episode.season, episode.episode) url = "plugin://plugin.video.timvision/?action=play_item&contentId=%s&videoType=%s&has_hd=%s&startPoint=%s&duration=%s" % ( str(episode.content_id), episode.mediatype, str(episode.is_hd_available), str( episode.bookmark), str(episode.duration)) self.write_strm( os.path.join(show_folder, filename + ".strm"), filename, url)
def onPlayBackStarted(self): if self.current_item != None and self.isPlaying(): playing_file = self.getPlayingFile() self.listen = self.current_item == playing_file if not self.listen: return if self.start_from >= 10: self.seekTime(float(self.start_from)) Logger.log_write("Listening ("+self.current_content_id+"): "+str(self.listen), mode=Logger.LOG_PLAYER) Logger.log_write("Started ("+self.current_content_id+")", Logger.LOG_PLAYER) self.send_keep_alive() self.playback_thread_stop_event = threading.Event() check_thread = threading.Thread(target=self.check_time) check_thread.start() threshold_thread = threading.Thread(target=self.threshold_calculation) threshold_thread.start() if self.start_paused: self.start_paused = False self.pause()
def start_webserver(): Logger.kodi_log("Starting TIMVISION addon") # pick & store a port for the internal TimVision HTTP proxy service tv_port = select_unused_port() set_setting('timvision_service_port', str(tv_port)) # server defaults SocketServer.TCPServer.allow_reuse_address = True # configure the TimVision Data Server from resources.lib.TimVisionHttpRequestHandler import TimVisionHttpRequestHandler tv_server = SocketServer.TCPServer(('127.0.0.1', tv_port), TimVisionHttpRequestHandler) tv_server.server_activate() tv_server.timeout = 1 tv_thread = threading.Thread(target=tv_server.serve_forever) tv_thread.daemon = True tv_thread.start() return tv_server
def check_time(self): time_elapsed = 0 self.current_time = int(self.getTime()) while self.current_time > self.total_time or self.current_time < 0: xbmc.sleep(200) self.onCorrectTimeLoaded() while not self.playback_thread_stop_event.isSet(): # keep live check self.current_time = int(self.getTime()) self.playback_thread_stop_event.wait(2) time_elapsed += 2 if time_elapsed >= self.keep_alive_limit and self.send_keep_alive(): time_elapsed = 0 # out of while complete_percentage = self.current_time * 100.0 / self.total_time Logger.log_write("Stopping (%s) - %.3f%%" % (self.current_content_id, complete_percentage), Logger.LOG_PLAYER) if complete_percentage >= 97.5: utils.call_service("set_content_seen", {"contentId":self.current_content_id, "duration": int(self.total_time)}) elif self.current_time > 10: utils.call_service("stop_content", {"contentId":str(self.current_content_id), "time":int(self.current_time), "threshold": int(self.threshold)}) self.reset_player()
def send_request(self, url, base_url, method="GET", data={}): if not url.startswith("https://"): url = utils.url_join(base_url, url) url = self.__compile_url(url) Logger.log_write("Sending " + method + " request to " + url, Logger.LOG_TIMVISION) response = self.api_endpoint.get( url, params=data) if method == "GET" else self.api_endpoint.post( url, data=data) Logger.log_write("Status Code: " + str(response.status_code), Logger.LOG_TIMVISION) if response.status_code == 200: data = response.json() Logger.log_write("Response: " + response.text, Logger.LOG_TIMVISION) if isinstance(data, list): Logger.log_write("JSON result is an array", Logger.LOG_TIMVISION) data = data[0] if data["resultCode"] == "OK": return data return None
def call_service(method, params={}, try_time=1): try: params.update({"method": method}) url_values = urllib.urlencode(params) full_url = get_service_url() + '?' + url_values Logger.log_write(full_url, Logger.LOG_API) data = urllib2.urlopen(full_url).read() parsed_json = json.loads(data) result = parsed_json.get('result', None) return result except urllib2.URLError as error: Logger.log_write("webserver error: %s" % (str(error.reason)), Logger.LOG_API) if try_time == 5: Logger.log_write("TryTime limit reach. Returning None", Logger.LOG_API) return None start_webserver() call_service(method, params, try_time + 1)
def update(self, force=False): is_updating = utils.get_setting("lib_export_updating") if not utils.get_setting("lib_export_enabled") or is_updating: return utils.set_setting("lib_export_updating", "true") update_kodi_library = False time_now = int(time.time()) last_update_movies = int( utils.get_setting("lib_export_last_update_movies")) if utils.get_setting("lib_export_movies") and ( force or time_now - self.TIME_BETWEEN_UPDATE > last_update_movies): Logger.kodi_log("Updating movies library") utils.set_setting("lib_export_last_update_movies", str(time_now)) self.__update_movies_library() self.__add_folder_to_sources(self.movies_folder) self.__insert_folder_database(self.movies_folder) update_kodi_library = True last_update_tvshows = int( utils.get_setting("lib_export_last_update_tvshows")) if utils.get_setting("lib_export_tvshows") and ( force or time_now - self.TIME_BETWEEN_UPDATE > last_update_tvshows): Logger.kodi_log("Updating tvshows library") utils.set_setting("lib_export_last_update_tvshows", str(time_now)) self.__update_tvshows_library() self.__add_folder_to_sources(self.tvshows_folder) self.__insert_folder_database(self.tvshows_folder) update_kodi_library = True if update_kodi_library: #xbmc.executebuiltin('Action(reloadsources)') self.__run_update_library() Logger.kodi_log("Libreria in aggiornamento") utils.set_setting("lib_export_updating", "false")
def onPlayBackResumed(self): if not self.listen: return Logger.log_write("Resumed ("+self.current_content_id+")", mode=Logger.LOG_PLAYER) self.is_paused = False
def onPlayBackStopped(self): if not self.listen: return Logger.log_write("Stopped ("+self.current_content_id+")", mode=Logger.LOG_PLAYER) self.playback_thread_stop_event.set()
def onPlayBackPaused(self): if not self.listen: return Logger.log_write("Paused ("+self.current_content_id+")", mode=Logger.LOG_PLAYER) utils.call_service("pause_content", {"contentId":self.current_content_id, "time":int(self.current_time), "threshold":int(self.threshold)}) self.is_paused = True