Last active
October 31, 2025 16:45
-
-
Save YtvwlD/06cc690a360a8d7d29b067b800ac8998 to your computer and use it in GitHub Desktop.
mpris2mqtt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| # DEBUG | |
| import os | |
| DEBUG = "DEBUG" in os.environ | |
| print("Debugging: {} {}".format(DEBUG, "(You might want to set DEBUG.)" if not DEBUG else "")) | |
| # DBUS (pip3 install dbus-python) | |
| from dbus.mainloop.glib import DBusGMainLoop | |
| # GLib (apt install libglib2.0-dev libgirepository1.0-dev, pip3 install PyGObject) | |
| from gi.repository.GLib import MainLoop | |
| # MPRIS (pip3 install mpris2) | |
| from mpris2 import get_players_uri, Player | |
| from platform import node | |
| def handle_change(current_player, info, signature): | |
| handle_status(info["Metadata"]) | |
| def handle_status(metadata): | |
| title = metadata["xesam:title"] | |
| artist = ", ".join(metadata["xesam:artist"]) | |
| album = metadata["xesam:album"] | |
| if not artist and not album and title: | |
| if " - " in title: | |
| artist = title.split(" - ")[0] | |
| title = title.split(" - ")[1] | |
| if DEBUG: | |
| print("------------------------") | |
| print("Currently playing: {}".format(metadata)) | |
| print("Title: {}".format(title)) | |
| print("Artist: {}".format(artist)) | |
| print("Album: {}".format(album)) | |
| print("Source: {}".format(hostname)) | |
| mqtt.publish("music/title", title) | |
| mqtt.publish("music/artist", artist) | |
| mqtt.publish("music/album", album) | |
| mqtt.publish("music/source", hostname) | |
| def quit(): | |
| print("Exiting...") | |
| mqtt.loop_stop(force=False) | |
| loop.quit() | |
| hostname = node() | |
| # connect to mqtt (pip3 install paho-mqtt) | |
| import paho.mqtt.client | |
| from paho.mqtt.client import CallbackAPIVersion as MqttCallbackAPIVersion | |
| mqtt = paho.mqtt.client.Client(MqttCallbackAPIVersion.VERSION2) | |
| mqtt.connect("mqttserver.chaosdorf.space") | |
| mqtt.loop_start() | |
| print("Connected to mqtt.") | |
| print("Connecting to DBUS...") | |
| DBusGMainLoop(set_as_default=True) | |
| loop = MainLoop() | |
| print("Searching for media players...") | |
| players = list(get_players_uri()) | |
| print("Found: {}".format(players)) | |
| if not players: | |
| print("ERROR: No media players found.") | |
| exit() | |
| print("Assuming first.") | |
| print("Connecting to player...") | |
| player = Player(dbus_interface_info={'dbus_uri': players[0]}) | |
| player.PropertiesChanged = handle_change | |
| handle_status(player.Metadata) | |
| # SIGTERM | |
| import signal | |
| signal.signal(signal.SIGTERM, quit) | |
| print("Looping...") | |
| try: | |
| while True: | |
| loop.run() | |
| except KeyboardInterrupt: | |
| quit() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [[source]] | |
| url = "https://pypi.org/simple" | |
| verify_ssl = true | |
| name = "pypi" | |
| [packages] | |
| mpris2 = "*" | |
| paho-mqtt = "*" | |
| dbus-python = "*" | |
| pygobject = "*" | |
| [dev-packages] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "_meta": { | |
| "hash": { | |
| "sha256": "b63dc22a8fb98e74d47bc638f307bd653e2a07a0bfe5cb04b55d9cb4b66fd1e2" | |
| }, | |
| "pipfile-spec": 6, | |
| "requires": {}, | |
| "sources": [ | |
| { | |
| "name": "pypi", | |
| "url": "https://pypi.org/simple", | |
| "verify_ssl": true | |
| } | |
| ] | |
| }, | |
| "default": { | |
| "dbus-python": { | |
| "hashes": [ | |
| "sha256:ad67819308618b5069537be237f8e68ca1c7fcc95ee4a121fe6845b1418248f8" | |
| ], | |
| "index": "pypi", | |
| "markers": "python_version >= '3.7'", | |
| "version": "==1.3.2" | |
| }, | |
| "mpris2": { | |
| "hashes": [ | |
| "sha256:2663be03f07559055746ef28e6fe68792165eb3dadc84880eca8841a2a7eb185" | |
| ], | |
| "index": "pypi", | |
| "version": "==1.0.2" | |
| }, | |
| "paho-mqtt": { | |
| "hashes": [ | |
| "sha256:13b205f29251e4f2c66a6c923c31fc4fd780561e03b2d775cff8e4f2915cf947", | |
| "sha256:2ef745073dfc9aa68bfec30d0b9b6f0304ea75182bae85a7c77a80cefce1eff5" | |
| ], | |
| "index": "pypi", | |
| "markers": "python_version >= '3.7'", | |
| "version": "==2.0.0" | |
| }, | |
| "pycairo": { | |
| "hashes": [ | |
| "sha256:1d54e28170a5e790269d9db4c195cca5152ff018ba7e330d0ed05d86ccc2ea7d", | |
| "sha256:20a31af89d92ffd5fc60c08e65ff649f16e18621a14a40dbdb049fc74942d7a9", | |
| "sha256:2dddd0a874fbddb21e14acd9b955881ee1dc6e63b9c549a192d613a907f9cbeb", | |
| "sha256:3e4e18ea03122e60abe3eb611e2849859cc950083ff85d8369328eadf3df63f5", | |
| "sha256:5986b8da3e7de7ab931d7ad527938df38f75d3a3bdea2b515c786c5ca2c5093c", | |
| "sha256:675578bc6d62d15ff8669f264783efc9c8c73e3a6f564b294a70fb45a2f78667", | |
| "sha256:696ba8024d2827e66e088a6e05a3b0aea30d289476bcb2ca47c9670d40900a50", | |
| "sha256:8616408ae93de4824a3777ec532ea75643e4bf74e49d601062c0b1788180c962", | |
| "sha256:9fa51168010e2dfb45499df071fca2d921893f724646f3454951000a7ad0cabb", | |
| "sha256:a611e4d82ad8470138bb46d465d47e8db826d9d80b6a520ccd83ee007f2073e4", | |
| "sha256:a8f3b567ba2ad55624a809823ccf75aff8d768c20216cb5888365f6fc695c1d2", | |
| "sha256:aac447b423b33b64119ecdd1ffebf9163b07f5401c5da50c707197efdd1c918a", | |
| "sha256:b6690a00fb225c19f42d76660e676aba7ae7cb18f3632cb02bce7f0d9b9c3800", | |
| "sha256:d374d9ec6d2f791bf57105d87a9028db1ef2b687848f64a524e447033eae7229", | |
| "sha256:d63929ab5a2f890a333f2f2f51de9f1c9fe20d1bddc982c2ca577b737448d72f" | |
| ], | |
| "markers": "python_version >= '3.8'", | |
| "version": "==1.26.0" | |
| }, | |
| "pygobject": { | |
| "hashes": [ | |
| "sha256:2c4cd7c805ee9e727904172423a7519338017fd7e122b62218ac211627436875" | |
| ], | |
| "index": "pypi", | |
| "markers": "python_version >= '3.8' and python_version < '4'", | |
| "version": "==3.48.1" | |
| } | |
| }, | |
| "develop": {} | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| dbus-python==1.3.2 | |
| mpris2==1.0.2 | |
| paho-mqtt==2.0.0 | |
| pygobject==3.48.1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /usr/bin/env python3 | |
| # partly taken from https://github.com/pypa/sampleproject/blob/02130aeda025ca86975258f953b5d2531d74e94c/setup.py | |
| # Always prefer setuptools over distutils | |
| from setuptools import setup, find_packages | |
| from os import path | |
| here = path.abspath(path.dirname(__file__)) | |
| # taken from https://stackoverflow.com/a/23265673/2192464 | |
| try: | |
| from pypandoc import convert_file | |
| read_md = lambda f: convert_file(f, 'rst') | |
| except ImportError: | |
| print("warning: pypandoc module not found, could not convert Markdown to RST") | |
| read_md = lambda f: open(f, 'r').read() | |
| setup( | |
| name='mpris2mqtt', | |
| # Versions should comply with PEP440. | |
| version="0.1.0", | |
| description='music', | |
| long_description=read_md(path.join(here, 'README.md')), | |
| # The project's main homepage. | |
| url='https://gist.github.com/YtvwlD/06cc690a360a8d7d29b067b800ac8998', | |
| # Author details | |
| author='Niklas Sombert', | |
| author_email='niklas@ytvwld.de', | |
| # Choose your license | |
| license='GPLv3', | |
| # See https://pypi.python.org/pypi?%3Aaction=list_classifiers | |
| classifiers=[ | |
| # How mature is this project? Common values are | |
| # 3 - Alpha | |
| # 4 - Beta | |
| # 5 - Production/Stable | |
| 'Development Status :: 3 - Alpha', | |
| # Where does it run? | |
| 'Environment :: Console', | |
| # Indicate who your project is intended for | |
| 'Intended Audience :: Developers', | |
| 'Intended Audience :: End Users/Desktop', | |
| # Language | |
| 'Natural Language :: English', | |
| # Topic | |
| 'Topic :: Utilities', | |
| # Specify the Python versions you support here. | |
| 'Programming Language :: Python :: 3', | |
| 'Programming Language :: Python :: 3.3', | |
| 'Programming Language :: Python :: 3.4', | |
| 'Programming Language :: Python :: 3.5', | |
| 'Programming Language :: Python :: 3.6', | |
| ], | |
| # What does your project relate to? | |
| keywords=[], | |
| # You can just specify the packages manually here if your project is | |
| # simple. Or you can use find_packages(). | |
| scripts=["mpris2mqtt.py"], | |
| # List run-time dependencies here. These will be installed by pip when | |
| # your project is installed. | |
| install_requires=["dbus-python==1.3.2", "mpris2==1.0.2", "paho-mqtt==2.0.0", "pygobject==3.48.1"], | |
| # require (at least) Python 3.3 | |
| python_requires="~=3.3", | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment