File release/contrib/lualibs/mympd.lua

Functions

mympd.api (method, params) Calls the myMPD jsonrpc api for current partition
mympd.api_partition (partition, method, params) Calls the myMPD jsonrpc api
mympd.cache_cover_write (src, uri, mimetype) Write a file for the cover cache
mympd.cache_lyrics_write (str, uri) Write a string to a file in the lyrics cache
mympd.cache_misc_write (src, name, mimetype) Write a file for the misc cache
mympd.cache_thumbs_write (src, tagvalue, mimetype) Write a file for the thumbs cache
mympd.check_arguments (tocheck) Checks arguments from the mympd_arguments global variable.
mympd.dialog (title, data, callback) Returns an Jsonrpc response for a script dialog.
mympd.hash_md5 (str) Returns the MD5 hash of string.
mympd.hash_sha1 (str) Returns the SHA1 hash of string.
mympd.hash_sha256 (str) Returns the SHA256 hash of string.
mympd.htmlencode (str) Simple HTML encoding
mympd.http_client (method, uri, extra_headers, payload, cache) Simple HTTP client
mympd.http_download (uri, extra_headers, out, cache) Download a file over http
mympd.http_header_get (header, name) Gets the named header value from a header lua table
mympd.http_jsonrpc_error (method, msg) Sends a JSONRPC 2.0 error.
mympd.http_jsonrpc_response (obj, result) Sends a JSONRPC 2.0 response.
mympd.http_jsonrpc_warn (method, msg) Sends a JSONRPC 2.0 warning.
mympd.http_redirect (location) Sends a HTTP temporary redirect (302).
mympd.http_reply (status, header, body) Sends a HTTP reply.
mympd.http_serve_file (file) Serves a file from the filesystem.
mympd.http_serve_file_from_cache (file) Serves a file from the http client cache.
mympd.http_serve_file_rm (file) Serves a file from the filesystem and removes it afterwards.
mympd.init () Populate the global mympd_state lua table
mympd.isnilorempty (str) Checks for empty string and nil
mympd.jsonrpc_error (msg) Creates a JSONRPC 2.0 error.
mympd.jsonrpc_notification (severity, msg) Creates a JSONRPC 2.0 notification.
mympd.jsonrpc_warn (msg) Creates a JSONRPC 2.0 warning.
mympd.log (loglevel, message) Logs to the myMPD log.
mympd.notify_client (severity, message) Sends a notification to the client that started this script.
mympd.notify_partition (severity, message) Sends a notification to all clients in the current partition.
mympd.os_capture (cmd) Execute a system command and capture its output
mympd.read_file (path) Read an ascii file
mympd.remove_file (path) Wrapper for os.remove that logs the error on failure
mympd.sleep (ms) Sleep ms
mympd.splitlines (str) Split string by newline characters and trims the lines
mympd.tmp_file () Creates a temporary file for the misc cache
mympd.tmpvar_delete (key) Deletes a temporary variable
mympd.tmpvar_get (key) Gets a temporary variable
mympd.tmpvar_list () Lists all temporary variable
mympd.tmpvar_set (key, value, lifetime) Sets a temporary variable
mympd.trim (str) Trims a string
mympd.update_mtime (filename) Updates the timestamp of a file
mympd.urldecode (str, form) URL decoding
mympd.urlencode (str) URL encoding


Functions

mympd.api (method, params)
Calls the myMPD jsonrpc api for current partition

Parameters

  • method: myMPD API method
  • params: API parameters as lua table

Return values:

  1. 0 for success, else 1
  2. jsonrpc result for success, else error
mympd.api_partition (partition, method, params)
Calls the myMPD jsonrpc api

Parameters

  • partition: MPD Partition
  • method: myMPD API method
  • params: API parameters as lua table

Return values:

  1. 0 for success, else 1
  2. jsonrpc result for success, else error
mympd.cache_cover_write (src, uri, mimetype)
Write a file for the cover cache

Parameters

  • src: File to rename
  • uri: URI to create the cover cache file for
  • mimetype: Mime Type or nil to sniff by magic bytes

Return values:

  1. 0 on success, else 1
  2. written name or error message on error
mympd.cache_lyrics_write (str, uri)
Write a string to a file in the lyrics cache

Parameters

  • str: String to save (it must be a valid lyrics json string)
  • uri: URI to create the lyrics cache file for

Return values:

  1. 0 on success, else 1
  2. written name or error message on error
mympd.cache_misc_write (src, name, mimetype)
Write a file for the misc cache

Parameters

  • src: File to rename
  • name: Value to create the misc cache file for
  • mimetype: Mime Type or nil to sniff by magic bytes

Return values:

  1. 0 on success, else 1
  2. written name or error message on error
mympd.cache_thumbs_write (src, tagvalue, mimetype)
Write a file for the thumbs cache

Parameters

  • src: File to rename
  • tagvalue: Tag value to create the thumbs cache file for
  • mimetype: Mime Type or nil to sniff by magic bytes

Return values:

  1. 0 on success, else 1
  2. written name or error message on error
mympd.check_arguments (tocheck)
Checks arguments from the mympd_arguments global variable.

Parameters

  • tocheck: Table of arguments with options to check Available options: notempty, required, number

Return values:

  1. true on success, else nil
  2. Error message on failure
mympd.dialog (title, data, callback)
Returns an Jsonrpc response for a script dialog.

Parameters

  • title: Dialog title
  • data: Dialog definition
  • callback: Script to call for the submit button

Return value:

Jsonrpc response
mympd.hash_md5 (str)
Returns the MD5 hash of string.

Parameters

  • str: String to hash

Return value:

MD5 hash of string
mympd.hash_sha1 (str)
Returns the SHA1 hash of string.

Parameters

  • str: String to hash

Return value:

SHA1 hash of string
mympd.hash_sha256 (str)
Returns the SHA256 hash of string.

Parameters

  • str: String to hash

Return value:

SHA256 hash of string
mympd.htmlencode (str)
Simple HTML encoding

Parameters

  • str: String to HTML encode

Return value:

Encoded string
mympd.http_client (method, uri, extra_headers, payload, cache)
Simple HTTP client

Parameters

  • method: HTTP method, e.g. GET, POST
  • uri: The uri to access
  • extra_headers: Additional headers terminated by "\r\n"
  • payload: Payload to send
  • cache: Cache the response

Return values:

  1. rc 0 for success, else 1
  2. HTTP status code
  3. HTTP Headers as Lua table
  4. HTTP Body
mympd.http_download (uri, extra_headers, out, cache)
Download a file over http

Parameters

  • uri: The uri to access
  • extra_headers: Additional headers terminated by "\r\n"
  • out: Filename to write the response body
  • cache: Cache the response

Return values:

  1. 0 for success, else 1
  2. HTTP status code
  3. HTTP Headers as Lua table
  4. Output filepath
mympd.http_header_get (header, name)
Gets the named header value from a header lua table

Parameters

  • header: Lua table of http headers and its values
  • name: Name of the header to return (case insensitive)

Return value:

HTTP header value or nil
mympd.http_jsonrpc_error (method, msg)
Sends a JSONRPC 2.0 error.

Parameters

  • method: API method
  • msg: Error message

Return value:

HTTP response
mympd.http_jsonrpc_response (obj, result)
Sends a JSONRPC 2.0 response.

Parameters

  • obj:
  • result: jsonrpc result object

Return value:

HTTP response
mympd.http_jsonrpc_warn (method, msg)
Sends a JSONRPC 2.0 warning.

Parameters

  • method: API method
  • msg: Error message

Return value:

HTTP response
mympd.http_redirect (location)
Sends a HTTP temporary redirect (302). Can be only used in the "http" event

Parameters

  • location: Location to redirect

Return value:

HTTP response
mympd.http_reply (status, header, body)
Sends a HTTP reply. Can be only used in the "http" event

Parameters

  • status: HTTP status code
  • header: Additional headers terminated by "\r\n"
  • body: HTTP Body to send

Return value:

HTTP response
mympd.http_serve_file (file)
Serves a file from the filesystem. Only files from the diskcache are allowed.

Parameters

  • file: File to serve

Return value:

HTTP response
mympd.http_serve_file_from_cache (file)
Serves a file from the http client cache.

Parameters

  • file: File to serve

Return value:

HTTP response
mympd.http_serve_file_rm (file)
Serves a file from the filesystem and removes it afterwards. Only files from the diskcache are allowed.

Parameters

  • file: File to serve

Return value:

HTTP response
mympd.init ()
Populate the global mympd_state lua table

Return values:

  1. 0 for success, else 1
  2. jsonrpc result for success, else error
mympd.isnilorempty (str)
Checks for empty string and nil

Parameters

  • str: String to check

Return value:

true if string is empty or nil, else false
mympd.jsonrpc_error (msg)
Creates a JSONRPC 2.0 error.

Parameters

  • msg: Error message

Return value:

String with jsonrpc message
mympd.jsonrpc_notification (severity, msg)
Creates a JSONRPC 2.0 notification.

Parameters

  • severity: Severity 0 = SEVERITY_EMERG = Emergency 1 = SEVERITY_ALERT = Alert 2 = SEVERITY_CRIT = Critical 3 = SEVERITY_ERR = Error 4 = SEVERITY_WARNING = Warning 5 = SEVERITY_NOTICE = Notice 6 = SEVERITY_INFO = Info 7 = SEVERITY_DEBUG = Debug
  • msg: Error message

Return value:

HTTP response
mympd.jsonrpc_warn (msg)
Creates a JSONRPC 2.0 warning.

Parameters

  • msg: Error message

Return value:

String with jsonrpc message
mympd.log (loglevel, message)
Logs to the myMPD log.

Parameters

  • loglevel: Syslog loglevel 0 = LOG_EMERG = Emergency 1 = LOG_ALERT = Alert 2 = LOG_CRIT = Critical 3 = LOG_ERR = Error 4 = LOG_WARNING = Warning 5 = LOG_NOTICE = Notice 6 = LOG_INFO = Info 7 = LOG_DEBUG = Debug
  • message:
mympd.notify_client (severity, message)
Sends a notification to the client that started this script.

Parameters

  • severity: Severity 0 = SEVERITY_EMERG = Emergency 1 = SEVERITY_ALERT = Alert 2 = SEVERITY_CRIT = Critical 3 = SEVERITY_ERR = Error 4 = SEVERITY_WARNING = Warning 5 = SEVERITY_NOTICE = Notice 6 = SEVERITY_INFO = Info 7 = SEVERITY_DEBUG = Debug
  • message: Jsonrpc message to send
mympd.notify_partition (severity, message)
Sends a notification to all clients in the current partition.

Parameters

  • severity: Severity 0 = SEVERITY_EMERG = Emergency 1 = SEVERITY_ALERT = Alert 2 = SEVERITY_CRIT = Critical 3 = SEVERITY_ERR = Error 4 = SEVERITY_WARNING = Warning 5 = SEVERITY_NOTICE = Notice 6 = SEVERITY_INFO = Info 7 = SEVERITY_DEBUG = Debug
  • message: Jsonrpc message to send
mympd.os_capture (cmd)
Execute a system command and capture its output

Parameters

  • cmd: Command to execute

Return value:

Captured output of cmd
mympd.read_file (path)
Read an ascii file

Parameters

  • path: Filename

Return value:

File content or nil on error
mympd.remove_file (path)
Wrapper for os.remove that logs the error on failure

Parameters

  • path: File to remove

Return values:

  1. true on success, else nil
  2. Error message on failure
mympd.sleep (ms)
Sleep ms

Parameters

  • ms: Milliseconds to sleep
mympd.splitlines (str)
Split string by newline characters and trims the lines

Parameters

  • str: String to split

Return value:

tables of lines
mympd.tmp_file ()
Creates a temporary file for the misc cache

Return value:

filename or nil on error
mympd.tmpvar_delete (key)
Deletes a temporary variable

Parameters

  • key: Variable name

Return values:

  1. 0 for success, else 1
  2. jsonrpc result for success, else error
mympd.tmpvar_get (key)
Gets a temporary variable

Parameters

  • key: Variable name

Return values:

  1. Variable value
  2. Unix timestamp of variable expiration
mympd.tmpvar_list ()
Lists all temporary variable

Return value:

Table of variables or nil on error
mympd.tmpvar_set (key, value, lifetime)
Sets a temporary variable

Parameters

  • key: Variable name
  • value: Variable value
  • lifetime: Lifetime of the variable

Return values:

  1. 0 for success, else 1
  2. jsonrpc result for success, else error
mympd.trim (str)
Trims a string

Parameters

  • str: String to trim

Return value:

Trimed string
mympd.update_mtime (filename)
Updates the timestamp of a file

Parameters

  • filename: File to update the timestamp

Return value:

0 on success, else 1
mympd.urldecode (str, form)
URL decoding

Parameters

  • str: String to URL decode
  • form: true = decode a url form encoded string

Return value:

Decoded string
mympd.urlencode (str)
URL encoding

Parameters

  • str: String to URL encode

Return value:

Encoded string

Valid XHTML 1.0!