INTERACT FORUM

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1]   Go Down

Author Topic: pyMCWS: A Python API Wrapper for MCWS  (Read 5533 times)

DrKNo

  • World Citizen
  • ***
  • Posts: 201
pyMCWS: A Python API Wrapper for MCWS
« on: December 16, 2019, 10:39:40 am »

I started pyMCWS as a project to wrap most of the MCWS API in a pythonian manner and make it more readily available to scripting users. I currently use it in my digital jukebox program and implement mostly what I need, but I am open to suggestions (and of course contributions). The code is hosted on GitHub, and releases are pushed to PyPi regularly.

GitHub: https://github.com/kenomaerz/pymcws
PyPi: https://pypi.org/project/pymcws/

pyMCWS is now on Version 1.0.0 after a pretty big rewrite. Not all methods are implemented, but the API should stay relatively stable from here on.

v1.0.0
  • Major rewrite! Usage should be much more intuitive now.
  • Editing and saving metadata is now supported.
  • Several additional functions added.
  • Added unit tests to feel safer about letting pyMCWS interact with my precious library.

v0.2.0
  • Added automatic field resolution. Fields are automatically converted to and from their corresponding python types by the API, sparing you the conversion.
  • this includes conversion to and from the JRiver date format
  • Because of this, the required version of MC is now 26. Earlier version support is possible but needs to be requested.
  • Automatic zone resolution and handling

v0.1.0
  • Connect to media server using access key, or by manually setting IP and port.
  • Automatically find best connection strategy to media server.
  • Send playback commands, including volume, shuffle, repeat to zones.
  • Search files on server and retrieve them as dictionaries.
  • Play files to zones.
  • Load images from server, either by returning a link, or directly as PIL image.
  • Automatic query escaping to help with building JRiver queries.
  • Recipes bundle common tasks into simpler methods (e.g. play_album).
  • ...and several other endpoints of the MCWS API.

Usage
Install via your favorite package manager:
Code: [Select]
pip install pymcws
First order of action is to import pymcws. You can just import the package and use it as a one-stop-shop-all:
Code: [Select]
import pymcws as mcws
using this method, all functions and recipes are imported and available via the mcws object. You can then initialize a server and start using commands:

Code: [Select]
# Create the server using access key and credentials
office = mcws.get_media_server("AccessKey", "readonly", "supersecretpassword")

# Alternatively, just use the keyword 'localhost' if the instance runs on your machine,
office = mcws.get_media_server("localhost", "readonly", "supersecretpassword")

# Query files via query recipe
files = mcws.recipes.query_album(office, "Ludovico Einaudi", "I Giorni")

# files are dictionaries of tags. Tags are automatically converted to the correct python type.
print(files[0]["Date"])
print(files[0]["Name"])
print(files[0]["Duration"])

# Editing tags is easy! Just edit the dict, and save the file afterwards
files[0]["Name"] = "Test2,3,4"
files[0]["Genre"] = ["Test Genre 1", "Test Genre 2", "Test Genre 3"]
files[0]["Date"] += timedelta(years=10)

office.file.set_info(files[0])


# Play an album using a play recipe.
print("Playing an Album")
mcws.recipes.play_album(office, "Ludovico Einaudi", "I Giorni")
time.sleep(3)
print("Pausing playback")
mcws.playback.playpause(office)

# Find and print the available Zones on the server
zones = mcws.playback.zones(office)
print("Zones available at server:")
for zone in zones:
    print("    ", zone.index, zone.id, zone.name, zone.guid, zone.is_dlna)

For a full set of examples, please see examples.py.

Contributing
Contributions are very welcome. Please create pull requests at your leisure. If you are not of the coding kind, you can also leave a request for a specific functionality in the issue tracker. Feedback is very welcome, as the API is yet subject to change.

And thanks go to...
... the whole JRiver team and community for all the help I got here, and especially Hendrik and Matt that were incredible in helping me with hints and feature implementations.
Logged

JimH

  • Administrator
  • Citizen of the Universe
  • *****
  • Posts: 72436
  • Where did I put my teeth?
Re: pyMCWS: A Python API Wrapper for MCWS
« Reply #1 on: December 16, 2019, 10:43:03 am »

Thank you.
Logged

Castius

  • Citizen of the Universe
  • *****
  • Posts: 562
Re: pyMCWS: A Python API Wrapper for MCWS
« Reply #2 on: December 18, 2019, 03:38:34 am »

Thank you!
Logged

DrKNo

  • World Citizen
  • ***
  • Posts: 201
Re: pyMCWS: A Python API Wrapper for MCWS
« Reply #3 on: March 30, 2020, 10:27:28 am »

I released version 0.2.0! pyMCWS is now able to handle most file querying and playback seamlessly.
Logged

samwin241

  • Recent member
  • *
  • Posts: 13
Re: pyMCWS: A Python API Wrapper for MCWS
« Reply #4 on: May 16, 2020, 11:48:37 pm »

hi DrKNo, just starting python so not sure how to get this running and how to run the examples.py or intialise a server.
Have installed python in windows, ran and got pipenv installed.

Installing pymcws…
Adding pymcws to Pipfile's [packages]…
Installation Succeeded
Installing dependencies from Pipfile.lock (6f0e79)…
  ================================ 7/7 - 00:00:04
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.

Looking into the folder I just ran this from I only have Pipfile and Pipfile.lock.

So the next step to import pymcws as mcws where exactly do i enter this, this isn't a command so assume needs to go into a script?

Would be possible to get a simple example to run against so i can dig further into what this can do. E.g something like start play command
Eventually was hoping this method can communicate directly with MC via a custom remote web page, i dont want to stream anything but just look at running a html web page with a button to send a command to pymcws, so it plays or does something locally on the unit via the MCWS / mcc commands.

Tried the other remotes on jriver not exactly what I'm looking for, and not a programmer but looking here - https://wiki.jriver.com/index.php/Web_Service_Connection this is exactly what i want in simple terms (looking at the code for pyMCWS on git here media_server.py you seem to have the working method and the comments there, just can't make this work to dig in further). Assume this is possible with the pyMCWS or should I be looking at something simpler?
Was trying to connect to the REST API via Jquery etc, not really understanding backend dev, can do frontend though.

Any help much appreciated.
Logged

mattkhan

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 4225
Re: pyMCWS: A Python API Wrapper for MCWS
« Reply #5 on: May 17, 2020, 03:09:02 am »

This is a library for calling mcws from python. If you are calling it from JavaScript then you just need to call mcws directly. There are a couple of js based apps mentioned inhttps://wiki.jriver.com/index.php/Getting_Started#Write_my_own_Apps_that_can_control_Media_Center which you can look at to get an idea.
Logged

samwin241

  • Recent member
  • *
  • Posts: 13
Re: pyMCWS: A Python API Wrapper for MCWS
« Reply #6 on: May 17, 2020, 05:01:00 am »

thanks mattkhan. this one looks promising but is over 7-8yrs old - https://github.com/rlebrette/jrmc-ws-server from one of the links on the page you sent. the gizmo one is not valid, seem this one makes a nodejs server which i can get running after fixing some values like rooms in the code which maybe specific for zones, deprecated express etc have manage to start and connect to MCWS.

> node ./server.js

7:45:31 PM - Detecting IP address
7:45:31 PM - Ethernet 10.211.55.3 (auto)
7:45:31 PM - Loopback Pseudo-Interface 1 127.0.0.1
7:45:31 PM - Pinging http://127.0.0.1:52199/MCWS/v1/, waiting for response.
7:45:31 PM - MC WebSocket Server v1.0 started
7:45:31 PM - Listen Websocket on : 1337 Listen web client on : 8080
7:45:31 PM - Authentification done, got token 7Iv4papS
7:45:31 PM - Communicating with : http://127.0.0.1:52199/MCWS/v1/


hitting 8080 on a browser will crash it straight away though.

SyntaxError: Unexpected identifier in \jrmcws\views\index.ejs while compiling ejs

If the above error is not helpful, you may want to try EJS-Lint:
https://github.com/RyanZim/EJS-Lint
Or, if you meant to create an async function, pass `async: true` as an option.
    at new Function (<anonymous>)
 
well it's supposedly connecting to MCWS, but craps out routing or compiling the client views. SO close.
have to try more debugging....
Logged

mattkhan

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 4225
Re: pyMCWS: A Python API Wrapper for MCWS
« Reply #7 on: May 17, 2020, 05:19:20 am »

this one is a pure js client talking to an MC server - https://github.com/3ll3d00d/ezmote/blob/master/src/services/jriver/index.js - which is all you need to do if you are just trying to talk to an existing MC server (and which is currently working against MC26 without issues)

you should start a new thread describing what you're trying to do though as this is OT for this thread
Logged

DrKNo

  • World Citizen
  • ***
  • Posts: 201
Re: pyMCWS: A Python API Wrapper for MCWS
« Reply #8 on: August 25, 2020, 05:30:14 pm »

After a major rewrite, this is the first version that I dare call 1.0.0.

Special thanks go to Matt and Hendrik that have been putting up with my constant posts and even implemented requests that made things like the automatic field conversion possible. You rock!
Logged

difool

  • Regular Member
  • Recent member
  • *
  • Posts: 33
  • nothing more to say...
Re: pyMCWS: A Python API Wrapper for MCWS
« Reply #9 on: January 10, 2022, 01:53:20 pm »

I got it installed on Python 3.8.8 via pip, no errors.

But when I try to import it into my project I get:

>>> import pymcws as mcws
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    import pymcws as mcws
  File "C:\Users\privat\AppData\Local\Programs\Python\Python38\lib\site-packages\pymcws\__init__.py", line 1, in <module>
    from pymcws.media_server import MediaServer, ApiMediaServer
  File "C:\Users\privat\AppData\Local\Programs\Python\Python38\lib\site-packages\pymcws\media_server.py", line 9, in <module>
    from pymcws.server_mixins import Library, Playback, File, Files, Recipes
  File "C:\Users\privat\AppData\Local\Programs\Python\Python38\lib\site-packages\pymcws\server_mixins.py", line 18, in <module>
    class Files(MediaServerDummy):
  File "C:\Users\privat\AppData\Local\Programs\Python\Python38\lib\site-packages\pymcws\server_mixins.py", line 19, in Files
    from pymcws.api.files import get_image, search, transform_mpl_response
  File "C:\Users\privat\AppData\Local\Programs\Python\Python38\lib\site-packages\pymcws\api\files.py", line 74, in <module>
    fields: list[str] = None,
TypeError: 'type' object is not subscriptable


Any ideas?
Logged

DrKNo

  • World Citizen
  • ***
  • Posts: 201
Re: pyMCWS: A Python API Wrapper for MCWS
« Reply #10 on: January 20, 2022, 07:20:35 am »

I updated the python version to 3.9 in the latest release (see history). You are running python 3.8. You can either upgrade python, or use the 1.0.0 version of pymcws.
Logged
Pages: [1]   Go Up