Skip to content

Commit

Permalink
Merge pull request #13 from chachmu/descriptive_upnp_error
Browse files Browse the repository at this point in the history
Add more descriptive upnp error
  • Loading branch information
chachmu committed Apr 10, 2024
2 parents 81d05a1 + f199a93 commit 33a5c24
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 11 deletions.
51 changes: 44 additions & 7 deletions main.js
@@ -1,4 +1,4 @@
// mpvDLNA 3.3.1
// mpvDLNA 3.4.1

"use strict";

Expand Down Expand Up @@ -91,37 +91,74 @@ var DLNA_Browser = function(options) {
// Determine how to call python
this.python = null;
var versions = ["python", "python3"];
var working_python = []
var upnp_errored = []

// If the .conf file specifies an option, test it first
if (options.python_version) {
versions.unshift(options.python_version);
}

// Test each option
// Test --version for each python call option
for (var i = 0; i < versions.length; i++) {
var result = mp.command_native({
name: "subprocess",
playback_only: false,
capture_stdout: true,
capture_stderr: true,
args : [versions[i], mp.get_script_directory()+"/mpvDLNA.py", "-v"]
args : [versions[i], "--version"]
});


if (result.status != 0) {
mp.msg.debug("calling python as " + versions[i] + " errored with: " + result.stderr);
mp.msg.debug("calling python --version as " + versions[i] + " errored with: " + result.stderr);
continue;
} else {
this.python = versions[i];
working_python.push(versions[i]);
}
}

// test mpvDLNA.py for each working python call
for (var i = 0; i < working_python.length; i++) {
var result = mp.command_native({
name: "subprocess",
playback_only: false,
capture_stdout: true,
capture_stderr: true,
args : [working_python[i], mp.get_script_directory()+"/mpvDLNA.py", "-v"]
});

if (result.status != 0) {
mp.msg.debug("calling mpvDLNA.py using " + working_python[i] + " errored with: " + result.stderr);
} else if (result.stdout.search("upnp import failed") != -1) {
upnp_errored.push(working_python[i]);
mp.msg.debug("python call: " + working_python[i] + " does not have upnp installed correctly ");
} else {
this.python = working_python[i];
mp.msg.debug("selecting '" + working_python[i] + "' for python call")
break;
}
}

// None of the options worked, throw an error
if (this.python == null) {
if (working_python.length == 0) {
throw new Error("Unable to find a correctly configured python call: \n \
in the following options: " + versions +
"\n Please add the name of your python install to the .conf file \n \
using the format: python_version=python \n \
or run mpv with the --msg-level=mpvDLNA=trace argument to see the errors");

// Some of the options worked but couldn't run mpvDLNA.py
} else if (this.python == null){
// upnpClient is not installed correctly
if (upnp_errored.length > 0) {
throw new Error("The following python calls exist but do not have upnpClient installed properly: " + versions);

// Some other error occured
} else {
throw new Error("The following python calls exist but failed to run mpvDLNA: " + versions +
"\n Please run mpv with the --msg-level=mpvDLNA=trace argument to see the errors");
}
}

// How long to spend searching for DLNA servers
Expand Down Expand Up @@ -971,7 +1008,7 @@ DLNA_Browser.prototype.command_wake = function(args) {

if (sp[0] == "packet sent") {
this.typing_output = "Packet Sent";
} else if (sp[0] == "import failed"){
} else if (sp[0] == "wakeonlan import failed"){
this.typing_output = Ass.color("FF0000", true) + "wakeonlan python package not installed";
} else {
this.typing_output = Ass.color("FF0000", true) + "unspecified error";
Expand Down
15 changes: 11 additions & 4 deletions mpvDLNA.py
@@ -1,7 +1,11 @@
import sys
import upnpclient

from lxml import etree
# Try to import upnp
upnp = True
try:
import upnpclient
except ImportError as error:
upnp = False

# Try to import wake on lan
wol = True
Expand All @@ -10,6 +14,7 @@
except ImportError as error:
wol = False

from lxml import etree
import logging
# important information is passed through stdout so we need to supress
# the output of the upnp client module
Expand All @@ -26,7 +31,7 @@ def wake(mac):
except:
print("send failed")
else:
print("import failed")
print("wakeonlan import failed")

def info(url, id, count):
device = upnpclient.Device(url)
Expand Down Expand Up @@ -104,7 +109,9 @@ def help():

if len(sys.argv) == 2:
if sys.argv[1] == "-v" or sys.argv[1] == "--version":
print("mpvDLNA.py Plugin Version 2.0.0")
print("mpvDLNA.py Plugin Version 2.1.0")
if not upnp:
print("upnp import failed")
else:
help()
elif len(sys.argv) == 3:
Expand Down

0 comments on commit 33a5c24

Please sign in to comment.