Giter Site home page Giter Site logo

un-def / i3blocks-mpris Goto Github PK

View Code? Open in Web Editor NEW
15.0 1.0 2.0 44 KB

A persistent i3blocks blocklet for the MPRIS D-Bus interface (f.k.a. i3blocks-spotify-persist)

License: MIT License

Python 100.00%
i3blocks blocklet mpris spotify i3blocks-spotify-persist

i3blocks-mpris's Introduction

i3blocks-mpris

A persistent i3blocks blocklet for the MPRIS D-Bus interface.

screenshot

Click the image above to watch a screencast.

This project was previously known as i3blocks-spotify-persist.

Features

  • near-immediate updates thanks to the event-driven model: the blocket is a constantly running process receiving D-Bus signals
  • configurable output
  • configurable mouse click actions (i3blocks version 1.5 or later is required)

Installation

Python version 3.8 or later is required.

The blocket can be installed from PyPI using pip:

python3 -m pip install [--user] i3blocks-mpris

Once the package is installed, there will be a blocket script named i3blocks-mpris somewhere depending on the presence of a --user pip flag (e.g., /usr/local/bin/i3blocks-mpris or ~/.local/bin/i3blocks-mpris).

To avoid dependecy hell, pipx can be used:

pipx install i3blocks-mpris

In this case the blocket script will be placed in ~/.local/bin directory.

Dependencies

Required (installed automatically):

Optional (installed manually):

Usage

Add the following lines to your i3blocks config:

[mpris]
command=/path/to/bin/i3blocks-mpris -c /path/to/config.json
interval=persist

Configuration

The blocket can be configured using a JSON config file and/or command line arguments. The only required parameter is player. It must be specified using either the config or the command line argument. Other config parameters and the config itself are optional.

Config parameters

player

Type: string

Default value: no default value, must be specified

A name of the player, either a full bus name — org.mpris.MediaPlayer2.<player>[.<instance>] — or its <player>[.<instance>] part.

Examples:

  • org.mpris.MediaPlayer2.spotify
  • org.mpris.MediaPlayer2.vlc.instance7389
  • spotify
  • vlc.instance7389

format

Type: string

Default value: {status}: {artist} – {title}

A template string with placeholders. Placeholder formats are {field} and {field:filter}.

Supported fields:

  • status, one of enum values: Playing, Paused, Stopped
  • artist
  • title

Supported filters:

Filter Description Example
upper str.upper “lorem Ipsum DOLor” → “LOREM IPSUM DOLOR”
lower str.lower “lorem Ipsum DOLor” → “lorem ipsum dolor”
capitalize str.capitalize “lorem Ipsum DOLor” → “Lorem ipsum dolor”
title str.title “lorem Ipsum DOLor” → “Lorem Ipsum Dolor”
icon converts a textual status to an icon, see the status_icons option below “Paused” → “⏸”

markup_escape

Type: boolean

Default value: false

This option specifies whether to escape special characters (such as <, >, &) using corresponding XML entities. Set to true if Pango markup is used (markup=pango in your i3blocks config), false otherwise.

status_icons

Type: object

Default value: {"Playing": "\uf04b", "Paused": "\uf04c", "Stopped": "\uf04d"}

This option provides a mapping for the icon filter (see above). The default value uses icons from Font Awesome.

mouse_buttons

Type: object

Default value: {"1": "PlayPause"}

This option provides a mapping of X11 mouse buttons numbers to MPRIS methods. You can use the xev program to determine button numbers.

sanitize_unicode

Type: boolean

Default value: true

If this option is set to true, the blocklet removes some unicode characters (more specifically, characters belonging to Cc, Cs, Co, and Cn general categories). See issue #9 for details.

dedupe

Type: boolean

Default value: true

For some reason, the Spotify app emits several identical signals for one action/event (e.g., it produces four PropertiesChanged signals when a track is played or paused). If this option is set true, the blocklet will compare the updated message with the previous one and print it only if it has changed. There is no reason to turn off deduplication except for debugging.

Config example

{
    "player": "spotify",
    "format": "<span font_family='monospace' color='#ffa651' weight='bold'>{status:icon} {status:upper}</span> <span color='#72bf44' weight='bold'>{artist}</span><span color='#ffa651'>᛫</span><span color='#b2d235'>{title}</span>",
    "markup_escape": true,
    "status_icons": {
        "Playing": "|>",
        "Paused": "||",
        "Stopped": "[]"
    },
    "mouse_buttons": {
        "1": "PlayPause",
        "9": "Previous",
        "8": "Next"
    }
}

Command line arguments

  • -h, --help — show all command line arguments and exit
  • -c, --config — a path to the config file (see above)

The following arguments override corresponding config options or defaults (that is, command line arguments have the highest precedence):

  • -p, --player
  • -f, --format
  • --markup-escape / --no-markup-escape
  • --sanitize-unicode / --no-sanitize-unicode
  • --dedupe / --no-dedupe

Changelog

See CHANGELOG.md.

License

The MIT License.

i3blocks-mpris's People

Contributors

paulfariello-syn avatar un-def avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

i3blocks-mpris's Issues

Process exits on Real-time signals

Hello! First, thanks for i3blocks-mpris, it is pretty cool!

I have come across a bug that I was unable to figure out. My volume blockets rely on real time signals to get "manually" notified on volume changes (via pkill -RTMIN+10 i3blocks). This is rather common in i3blocks and is explicitely supported.

The bug I came across is that sending any SIGRTMIN+N causes i3blocks-mpris to exit without any error message, the only trace in standard error is the message Real-time signal N, and the exit code is always of the form162+N, where N is the same as in SIGRTMIN+N.

I tried to debug as far as I could but my knowledge on dbus is close to zero.

Non-printable garbage in some fields

For example, this track: https://open.spotify.com/track/3NUKBSs5ZrPlxE3KsY5ySG (“We're There (feat. Chunky)” by Zed Bias).

image

The same two garbage symbols with or without Pango markup escape.

On the surface, it seems like a bug in the blocklet:

  • playerctl displays the title without any garbage:
    $ playerctl -p spotify metadata title
    We're There (feat. Chunky)
    
  • Strangely enough, i3blocks-mpris running in the console also works fine (somehow):
    $ python -m i3blocks_mpris -p spotify -f '{title}' --no-markup-escape
    We're There (feat. Chunky)
    
  • The Spotify web page displays the title correctly:
    image

But if we dig a bit deeper, we'll find the root of the problem:

  • The Spotify web page:

    <title>We&#x27;��re There (feat. Chunky) - song and lyrics by Zed Bias, Chunky | Spotify</title>
    {
      // embedded application/ld+json document; most fields are omitted for brevity
      "@context":"http://schema.googleapis.com/",
      "@type":"MusicRecording",
      "@id":"https://open.spotify.com/track/3NUKBSs5ZrPlxE3KsY5ySG",
      "url":"https://open.spotify.com/track/3NUKBSs5ZrPlxE3KsY5ySG",
      "name":"We'\u0080\u0099re There (feat. Chunky)",
      "datePublished":"2013-11-25"
    }
  • playerctl | hexdump:

    57 65 27 c2 80 c2 99 72 65 20 54 68 65 72 65 20 28 66 65 61 74 2e 20 43 68 75 6e 6b 79 29 0a
    
  • i3blocks-mpris | hexdump:

    $ python -m i3blocks_mpris -p spotify -f '{title}' --no-markup-escape | stdbuf -o0 hexdump -ve '/1 "%02x "'
    57 65 27 c2 80 c2 99 72 65 20 54 68 65 72 65 20 28 66 65 61 74 2e 20 43 68 75 6e 6b 79 29 0a
    

Here is a byte-by-byte comparison of hex representations and chars (I marked “invisible” chars as ×):

57 65 27 c2 80 c2 99 72 65 20 54 68 65 72 65 20 28 66 65 61 74 2e 20 43 68 75 6e 6b 79 29 0a
W  e  '  ×  ×  ×  ×  r  e     T  h  e  r  e     (  f  e  a  t  .     C  h  u  n  k  y  )

There are definitely some non-printable codepoints: �� / \u0080\u0099 / c2 80 c2 99!

Okay, we need to go a bit more deeper.

Let's assume that there was not the regular ASCII U+0027 ' APOSTROPHEbut the fancy pseudo-typographic one, a.k.a. U+2019 ’ RIGHT SINGLE QUOTATION MARK (see https://en.wikipedia.org/wiki/Apostrophe#Unicode), and try to mess with encodings:

rsqm = "’"
unicodedata.name(rsqm)   # → 'RIGHT SINGLE QUOTATION MARK'
binascii.hexlify(rsqm.encode(), ' ').decode()  # → 'e2 80 99'

Bingo! The same 0x80 0x99.

Let's check again: https://www.fileformat.info/info/unicode/char/2019/index.htm (“Unicode Character 'RIGHT SINGLE QUOTATION MARK' (U+2019)”)

Encodings Encoded
UTF-8 (hex) 0xE2 0x80 0x99 (e28099)

A questsion on Stack Overflow on the same topic: https://stackoverflow.com/questions/2477452/%C3%A2%E2%82%AC-showing-on-page-instead-of

BTW,

rsqm = "’"
unicodedata.name(rsqm)   # → 'RIGHT SINGLE QUOTATION MARK'
rsqm.encode('utf-8').decode('windows-1252')  # → '’'

Yep, exactly the same character sequence as in the aforementioned SO question.

Crashing when sending signal to i3blocks

As soon as you send a signal to i3blocks, your script crashes with the error:

[1] 182634 unknown signal i3blocks-spotify-persist

even tho i3blocks is not configured to send any signal to that script. Really annoying because I use signals to update my blockets (for example when adjusting volume).

pic-selected-200801-1505-06

Gio GLiB import error, missing dependancy?

The script has recently stopped working with latest python / arch linux updates?

Any clues as to what dependencies are missing / what the issue could be?

Error log:

Traceback (most recent call last):
File "/home/viking/.local/bin/i3blocks-mpris", line 5, in
from i3blocks_mpris import _main
File "/home/viking/.local/lib/python3.10/site-packages/i3blocks_mpris.py", line 11, in
from gi.repository import Gio, GLib
File "/home/viking/.local/lib/python3.10/site-packages/gi/init.py", line 42, in
from . import _gi
ImportError: /home/viking/.local/lib/python3.10/site-packages/gi/_gi.cpython-310-x86_64-linux-gnu.so: undefined symbol: _PyUnicode_AsStringAndSize

Click not working with persist interval

Not sure if I'm missing some part of config since I see this was implemented, but when I have the next, prev, play/pause buttons set with persistent interval they don't work. Setting them to interval works, but is obviously not ideal.

i3blocks version 1.4-4
Ubuntu 18.04

Python 3.10 errors when installing

Hello. Sorry, this is my first Github error report ever so I might miss some etiquette there and there. I tried to install the package today with pip according to the instructions, however it threw bunch of errors. They all seem to be related to Python 3.10, which isn't specified in "setup.cfg". I am on Arch Linux.

Other way to get this working is to put the "i3blocks_mpris.py" to directory you want. In i3blocks configuration file, I had to put "python3" first in the "command="-line or the program wouldn't run at all. Works perfectly well now, thanks.

[mpris]
command=python3 /directory/to/i3blocks_mpris.py -c /directory/to/config.json
interval=persist
markup=pango

Escape Pango special characters

First, thank you for the great i3block.

At my system, song titles which contain a &, <, or > were not displayed. According to this Github Issue this are special characters in Pango which must be escaped.

I modified the code:

if not only_if_changed or self._prev_info != info:
    info = info.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
    print(info, flush=True)
    self._prev_info = info

Maybe this helps somebody else :)

Very high CPU usage

I noticed last night that the process was pegging my CPU and had to kill it.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.