def write_line(self, data): """ Write a line *data* to socket. It appends a **newline** at the end of the *data* before sending it. The string MUST NOT contain **newline** otherwise an AssertionError will raise. Parameters: **data** String containing the data to be sent. """ assert ('\n' not in data) self.write_lock.acquire() try: try: data = data.encode('utf-8') except AttributeError: pass if self._debug_socket: _log.debug("<:%d: %s", len(data), data.decode('utf-8')[:130]) self._wbuffer += data + b'\n' sbytes = 0 while self._wbuffer: try: sbytes = self._sck.send(self._wbuffer) except IOError: _log.debug("Read socket error: IOError (timeout: %r)", self._sck.gettimeout()) _log.debug(traceback.format_exc(0)) return 0 except socket.error: _log.debug("Read socket error: socket.error (timeout: %r)", self._sck.gettimeout()) _log.debug(traceback.format_exc(0)) return 0 except: raise if sbytes == 0: break self._wbuffer = self._wbuffer[sbytes:] if self._wbuffer: _log.warning("%d bytes left in write buffer", len(self._wbuffer)) return len(self._wbuffer) finally: self.write_lock.release()
def write_thread(self): abort = False while not abort: self.write_thread_semaphore.acquire() try: item = self.write_thread_queue.pop(0) except IndexError: # pop from empty list? _log.warning("write queue was empty??") continue abort = item.get("abort", False) event = item.get("event") write_data = item.get("write_data") if write_data: item["result"] = self.write_now(write_data) if event: event.set() if self._debug_socket: _log.debug("Writing thread finished.")
def AddonFailure(self, addonId): if ADDON.getSetting("provider_disable_failing") == u"false": return 0 if addonId in self._failures: self._failures[addonId] += 1 else: self._failures[addonId] = 1 log.warning("Recorded failure %d for %s" % (self._failures[addonId], addonId)) if self._failures[addonId] > int(ADDON.getSetting("provider_allowed_failures")): try: time.sleep(10) notify(getLocalizedString(30111)) urllib_request.urlopen("%s/provider/%s/disable" % (da_inc_HOST, addonId)) except: notify(getLocalizedString(30112)) return 0 return self._failures[addonId]
def close(self): """ Close the connection and the socket. """ if self.connection_status == "closed": return item = {'abort': True, 'event': threading.Event()} self.write_thread_queue.append(item) self.write_thread_semaphore.release() # notify new item. item['event'].wait(1) if not item['event'].isSet(): _log.warning("write thread doesn't process our abort command") try: self.handler._shutdown() except Exception: _log.error("Error when shutting down the handler: %s", traceback.format_exc()) try: self._sck.shutdown(socket.SHUT_RDWR) except socket.error: pass self._sck.close() self.connection_status = "closed"
def get_da_inc_binary(): global binary_platform binary_platform = get_platform() binary = "da_inc" + (binary_platform["os"] == "windows" and ".exe" or "") binary_dir = os.path.join(ADDON_PATH, "resources", "bin", "%(os)s_%(arch)s" % binary_platform) if binary_platform["os"] == "android": log.info("Detected binary folder: %s" % binary_dir) binary_dir_legacy = binary_dir.replace("/storage/emulated/0", "/storage/emulated/legacy") if os.path.exists(binary_dir_legacy): binary_dir = binary_dir_legacy log.info("Using changed binary folder for Android: %s" % binary_dir) app_id = android_get_current_appid() xbmc_data_path = os.path.join("/data", "data", app_id) if not os.path.exists(xbmc_data_path): log.info( "%s path does not exist, so using %s as xbmc_data_path" % (xbmc_data_path, xbmc.translatePath("special://xbmcbin/"))) xbmc_data_path = xbmc.translatePath("special://xbmcbin/") if not os.path.exists(xbmc_data_path): log.info("%s path does not exist, so using %s as xbmc_data_path" % (xbmc_data_path, xbmc.translatePath("special://masterprofile/"))) xbmc_data_path = xbmc.translatePath("special://masterprofile/") dest_binary_dir = os.path.join(xbmc_data_path, "files", ADDON_ID, "bin", "%(os)s_%(arch)s" % binary_platform) else: dest_binary_dir = os.path.join( xbmc.translatePath(ADDON.getAddonInfo("profile")), "bin", "%(os)s_%(arch)s" % binary_platform) binary_path = os.path.join(binary_dir, binary) dest_binary_path = os.path.join(dest_binary_dir, binary) log.info("Binary detection. Source: %s, Destination: %s" % (binary_path, dest_binary_path)) if not os.path.exists(binary_path): # notify((getLocalizedString(30103) + " %(os)s_%(arch)s" % PLATFORM), time=7000) dialog_ok("LOCALIZE[30347];;" + "%(os)s_%(arch)s" % binary_platform) system_information() try: log.info("Source directory (%s):\n%s" % (binary_dir, os.listdir(os.path.join(binary_dir, "..")))) log.info("Destination directory (%s):\n%s" % (dest_binary_dir, os.listdir(os.path.join(dest_binary_dir, "..")))) except Exception: pass return False, False if os.path.isdir(dest_binary_path): log.warning( "Destination path is a directory, expected previous binary file, removing..." ) try: shutil.rmtree(dest_binary_path) except Exception as e: log.error("Unable to remove destination path for update: %s" % e) system_information() return False, False if not os.path.exists(dest_binary_path) or not os.path.exists( binary_path) or get_da_inc_checksum( dest_binary_path) != get_da_inc_checksum( binary_path) or not filecmp.cmp( dest_binary_path, binary_path, shallow=True): log.info("Updating da_inc daemon...") try: os.makedirs(dest_binary_dir) except OSError: pass try: shutil.rmtree(dest_binary_dir) except Exception as e: log.error("Unable to remove destination path for update: %s" % e) system_information() pass try: shutil.copytree(binary_dir, dest_binary_dir) except Exception as e: log.error("Unable to copy to destination path for update: %s" % e) system_information() return False, False # Clean stale files in the directory, as this can cause headaches on # Android when they are unreachable dest_files = set(os.listdir(dest_binary_dir)) orig_files = set(os.listdir(binary_dir)) log.info("Deleting stale files %s" % (dest_files - orig_files)) for file_ in (dest_files - orig_files): path = os.path.join(dest_binary_dir, file_) if os.path.isdir(path): shutil.rmtree(path) else: os.remove(path) log.info("Binary detection: [ Source: %s, Destination: %s ]" % (binary_path, dest_binary_path)) return dest_binary_dir, ensure_exec_perms(dest_binary_path)
def start_da_inc(**kwargs): jsonrpc_failures = 0 while jsonrpc_enabled() is False: jsonrpc_failures += 1 log.warning( "Unable to connect to Kodi's JSON-RPC service, retrying...") if jsonrpc_failures > 1: time.sleep(5) if not jsonrpc_enabled(notify=True): log.error( "Unable to reach Kodi's JSON-RPC service, aborting...") return False else: break time.sleep(3) da_inc_dir, da_inc_binary = get_da_inc_binary() log.info("Binary dir: %s, item: %s " % (da_inc_dir, da_inc_binary)) if da_inc_dir is False or da_inc_binary is False: return False lockfile = os.path.join(ADDON_PATH, ".lockfile") if os.path.exists(lockfile): log.warning("Existing process found from lockfile, killing...") try: with open(lockfile) as lf: pid = int(lf.read().rstrip(" \t\r\n\0")) os.kill(pid, 9) except OSError as e: if e.errno != 3: # Ignore: OSError: [Errno 3] No such process log.error(repr(e)) except Exception as e: log.error(repr(e)) if binary_platform["os"] == "windows": try: library_lockfile = os.path.join( xbmc.translatePath( ADDON.getAddonInfo("profile")).decode('utf-8'), "library.db.lock") log.warning("Removing library.db.lock file at %s ..." % library_lockfile) os.remove(library_lockfile) except Exception as e: log.error(repr(e)) SW_HIDE = 0 STARTF_USESHOWWINDOW = 1 args = [da_inc_binary] kwargs["cwd"] = da_inc_dir if binary_platform["os"] == "windows": args[0] = getWindowsShortPath(da_inc_binary) kwargs["cwd"] = getWindowsShortPath(da_inc_dir) si = subprocess.STARTUPINFO() si.dwFlags = STARTF_USESHOWWINDOW si.wShowWindow = SW_HIDE clear_fd_inherit_flags() kwargs["startupinfo"] = si else: env = os.environ.copy() env["LD_LIBRARY_PATH"] = "%s:%s" % (da_inc_dir, env.get("LD_LIBRARY_PATH", "")) kwargs["env"] = env kwargs["close_fds"] = True wait_counter = 1 log.debug("Checking for visible") while xbmc.getCondVisibility( 'Window.IsVisible(10140)') or xbmc.getCondVisibility( 'Window.IsActive(10140)'): if wait_counter == 1: log.info( 'Add-on settings currently opened, waiting before starting...') if wait_counter > 300: break time.sleep(1) wait_counter += 1 log.info("da_inc: start args: %s, kw: %s" % (args, kwargs)) if hasSubprocess: return subprocess.Popen(args, **kwargs) return False
def run(url_suffix="", retry=0): try: buffer_timeout = int(ADDON.getSetting("buffer_timeout")) if buffer_timeout < 60: buffer_timeout = 60 except: buffer_timeout = 60 buffer_timeout = buffer_timeout * 2 try: preload_timeout = int(ADDON.getSetting("preload_timeout")) if preload_timeout < 1: preload_timeout = 1 except: preload_timeout = 1 socket.setdefaulttimeout(buffer_timeout) urllib_request.install_opener( urllib_request.build_opener(NoRedirectHandler())) # Pause currently playing da_inc file to avoid doubling requests try: if xbmc.Player().isPlaying() and ADDON_ID in xbmc.Player( ).getPlayingFile(): xbmc.Player().pause() except: pass url = sys.argv[0].replace("plugin://%s" % ADDON_ID, da_inc_HOST + url_suffix) + sys.argv[2] query_add = "" if len(sys.argv) > 3: app = sys.argv[3].replace(":", "=") # Replacing resume=false with resume=true if item is launched from main window title = xbmc.getInfoLabel('ListItem.Title') label = xbmc.getInfoLabel('ListItem.Label') # if "resume" in app and not title and not label: # app = app.replace("resume=false", "resume=true") # if "resume" not in app and not title and not label: if not title and not label: if app: app = app.replace("resume=false", "") app += "&" if app and "resume" not in app: app += "resume=true" query_add = app elif "play" in url or "links" in url: query_add = "resume=true" if query_add and "resume" not in url: query_add = query_add.replace("resume=", "doresume=") if "?" in url: url += "&" + query_add else: url += "?" + query_add log.debug("Requesting %s from %s" % (url, repr(sys.argv))) try: data = _json(url) except PlayerException as e: redirect_url = e.__str__() log.debug("Launching player with %s" % (redirect_url)) xbmcplugin.endOfDirectory(HANDLE, succeeded=True) xbmc.sleep(500) xbmc.executeJSONRPC( '{"jsonrpc":"2.0","method":"Player.Open","params":{"item":{"file":"%s"}},"id":"1"}' % (redirect_url)) return except RedirectException as e: redirect_url = e.__str__() log.debug("Redirecting Kodi with %s" % (redirect_url)) xbmcplugin.endOfDirectory(HANDLE, succeeded=True) xbmc.sleep(500) if "keyboard=1" in sys.argv[0]: xbmc.executebuiltin('Container.Update(%s,replace)' % (redirect_url)) else: xbmc.executebuiltin('Container.Update(%s)' % (redirect_url)) return except urllib_error.URLError as e: # We can retry the request if connection is refused. # For example when plugin has not yet started but is requested by someone. if retry <= 2: time.sleep(preload_timeout) return run(retry=retry + 1) if isinstance(e.reason, IOError) or isinstance( e.reason, OSError) or 'Connection refused' in e.reason: notify(getLocalizedString(30116), time=7000) else: import traceback map(log.error, traceback.format_exc().split("\n")) notify(e.reason, time=7000) return except Exception as e: import traceback map(log.error, traceback.format_exc().split("\n")) try: msg = six.ensure_text(e.__str__(), errors='ignore') except: try: msg = six.ensure_binary(e.__str__(), errors='ignore') except: msg = repr(e) notify(getLocalizedLabel(msg), time=7000) return if not data: return if data["content_type"]: content_type = data["content_type"] if data["content_type"].startswith("menus"): content_type = data["content_type"].split("_")[1] xbmcplugin.addSortMethod(HANDLE, xbmcplugin.SORT_METHOD_UNSORTED) if content_type != "tvshows": xbmcplugin.addSortMethod(HANDLE, xbmcplugin.SORT_METHOD_LABEL_IGNORE_THE) else: xbmcplugin.addSortMethod(HANDLE, xbmcplugin.SORT_METHOD_TITLE_IGNORE_THE) xbmcplugin.addSortMethod(HANDLE, xbmcplugin.SORT_METHOD_DATE) xbmcplugin.addSortMethod(HANDLE, xbmcplugin.SORT_METHOD_GENRE) xbmcplugin.setContent(HANDLE, content_type) listitems = list(range(len(data["items"]))) for i, item in enumerate(data["items"]): # Translate labels if item["label"][0:8] == "LOCALIZE": item["label"] = getLocalizedLabel(item["label"]) if isinstance(item["label"], str): item["label"] = six.ensure_text(item["label"], 'utf-8') if item["label2"][0:8] == "LOCALIZE": item["label2"] = getLocalizedLabel(item["label2"]) listItem = xbmcgui.ListItem(label=item["label"], label2=item["label2"], iconImage=item["icon"], thumbnailImage=item["thumbnail"]) if item.get("info"): listItem.setInfo("video", item["info"]) if item.get("stream_info"): for type_, values in item["stream_info"].items(): listItem.addStreamInfo(type_, values) if item.get("art"): listItem.setArt(item["art"]) elif ADDON.getSetting('default_fanart' ) == 'true' and item["label"] != six.ensure_text( getLocalizedString(30218), 'utf-8'): fanart = os.path.join(ADDON_PATH, "fanart.png") listItem.setArt({'fanart': fanart}) if item.get("context_menu"): # Translate context menus for m, menu in enumerate(item["context_menu"]): if menu[0][0:8] == "LOCALIZE": menu[0] = getLocalizedLabel(menu[0]) listItem.addContextMenuItems(item["context_menu"]) listItem.setProperty("isPlayable", item["is_playable"] and "true" or "false") if item.get("properties"): for k, v in item["properties"].items(): listItem.setProperty(k, v) listitems[i] = (item["path"], listItem, not item["is_playable"]) xbmcplugin.addDirectoryItems(HANDLE, listitems, totalItems=len(listitems)) # Set ViewMode if data["content_type"]: viewMode = ADDON.getSetting("viewmode_%s" % data["content_type"]) if viewMode: try: xbmc.executebuiltin('Container.SetViewMode(%s)' % viewMode) except Exception as e: log.warning("Unable to SetViewMode(%s): %s" % (viewMode, repr(e))) xbmcplugin.endOfDirectory(HANDLE, succeeded=True, updateListing=False, cacheToDisc=False)