| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
import sys |
| 4 |
import argparse |
| 5 |
|
| 6 |
try: |
| 7 |
from pydbus import SessionBus |
| 8 |
except ImportError as e: |
| 9 |
print("Cannot find pydbus module. Please install first") |
| 10 |
sys.exit(1) |
| 11 |
|
| 12 |
attributes = { |
| 13 |
'album': 'xesam:album', |
| 14 |
'albumArtist': 'xesam:albumArtist', |
| 15 |
'artist': 'xesam:artist', |
| 16 |
'discNumber': 'xesam:discNumber', |
| 17 |
'genre': 'xesam:genre', |
| 18 |
'rating': 'xesam:userRating', |
| 19 |
'title': 'xesam:title', |
| 20 |
'trackNumber': 'xesam:trackNumber', |
| 21 |
|
| 22 |
'artUrl': 'mpris:artUrl', |
| 23 |
'length': 'mpris:length', |
| 24 |
|
| 25 |
'bitrate': 'sayonara:bitrate', |
| 26 |
'filesize': 'sayonara:filesize', |
| 27 |
'year': 'sayonara:year', |
| 28 |
|
| 29 |
'length-sec': 'length-sec', |
| 30 |
'length-string': 'length-string', |
| 31 |
'pos': 'pos', |
| 32 |
'pos-sec': 'pos-sec', |
| 33 |
'pos-string': 'pos-string', |
| 34 |
'progress': 'progress', |
| 35 |
'rating-stars': 'rating-stars', |
| 36 |
'status': 'status', |
| 37 |
'volume': 'volume' |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
def print_help_and_exit(): |
| 42 |
parser.print_help() |
| 43 |
sys.exit(1) |
| 44 |
|
| 45 |
|
| 46 |
def apply_to_format_string( resultList, formatString ): |
| 47 |
result = (', ').join(resultList) |
| 48 |
if not formatString is None: |
| 49 |
if len(formatString) > 0: |
| 50 |
result = formatString |
| 51 |
for r in resultList: |
| 52 |
result = result.replace('%', r, 1) |
| 53 |
|
| 54 |
return result |
| 55 |
|
| 56 |
def convert_sec_to_string(sec): |
| 57 |
|
| 58 |
hours = 0 |
| 59 |
minutes = 0 |
| 60 |
if sec >= 3600: |
| 61 |
hours = int(sec / 3600) |
| 62 |
sec = sec % 3600 |
| 63 |
if sec >= 60: |
| 64 |
minutes = int(sec / 60) |
| 65 |
sec = sec % 60 |
| 66 |
|
| 67 |
ret = '' |
| 68 |
if hours > 0: |
| 69 |
ret = str(hours) + ':' |
| 70 |
|
| 71 |
ret = ret + str(minutes).rjust(2, '0') + ':' + str(sec).rjust(2, '0') |
| 72 |
|
| 73 |
return ret |
| 74 |
|
| 75 |
|
| 76 |
def main(): |
| 77 |
bus = SessionBus() |
| 78 |
|
| 79 |
try: |
| 80 |
sayonara = bus.get( |
| 81 |
'org.mpris.MediaPlayer2.sayonara', # Bus name |
| 82 |
'/org/mpris/MediaPlayer2' # Object path |
| 83 |
) |
| 84 |
except: |
| 85 |
print('Sayonara not running') |
| 86 |
sys.exit(1) |
| 87 |
|
| 88 |
global parser |
| 89 |
parser = argparse.ArgumentParser() |
| 90 |
parser.add_argument('attributes', |
| 91 |
help='a comma separated list of attributes. Valid attributes: ' + |
| 92 |
(', ').join(attributes.keys()) |
| 93 |
) |
| 94 |
|
| 95 |
parser.add_argument('--format', type=str, required=False, |
| 96 |
help='a format string. Every %% is replaced by the value of an attribute. ' |
| 97 |
'e.g. ' |
| 98 |
'\'title,album,albumArtist --format \"%% on %% by %%\"\'' |
| 99 |
) |
| 100 |
|
| 101 |
try: |
| 102 |
parsedArgs = parser.parse_args() |
| 103 |
except: |
| 104 |
print_help_and_exit() |
| 105 |
|
| 106 |
requestedAttributes = parsedArgs.attributes.split(',') |
| 107 |
|
| 108 |
resultList = [] |
| 109 |
|
| 110 |
for attribute in requestedAttributes: |
| 111 |
if not attribute in attributes: |
| 112 |
print('Invalid attribute: ' + attribute) |
| 113 |
print_help_and_exit() |
| 114 |
|
| 115 |
metadata = sayonara.Get('org.mpris.MediaPlayer2.Player', 'Metadata') |
| 116 |
position = sayonara.Get('org.mpris.MediaPlayer2.Player', 'Position') |
| 117 |
status = sayonara.Get('org.mpris.MediaPlayer2.Player', 'PlaybackStatus') |
| 118 |
volume = sayonara.Get('org.mpris.MediaPlayer2.Player', 'Volume') |
| 119 |
length = metadata[attributes['length']] |
| 120 |
|
| 121 |
metadata['pos'] = position |
| 122 |
metadata['pos-sec'] = int(position / 1000000.0) |
| 123 |
metadata['pos-string'] = convert_sec_to_string(metadata['pos-sec']) |
| 124 |
metadata['length-sec'] = int(length / 1000000.0) |
| 125 |
metadata['length-string'] = convert_sec_to_string(metadata['length-sec']) |
| 126 |
|
| 127 |
try: |
| 128 |
metadata['progress'] = position / length |
| 129 |
except: |
| 130 |
metadata['progress'] = 0; |
| 131 |
|
| 132 |
metadata['volume'] = volume |
| 133 |
|
| 134 |
rating = int(metadata['xesam:userRating'] * 5) |
| 135 |
metadata['xesam:userRating'] = rating |
| 136 |
|
| 137 |
metadata['rating-stars'] = "*" * rating |
| 138 |
|
| 139 |
specifier = attributes[attribute] |
| 140 |
|
| 141 |
resultList.append(str(metadata[specifier])) |
| 142 |
|
| 143 |
formatString = parsedArgs.format |
| 144 |
result = apply_to_format_string(resultList, formatString) |
| 145 |
|
| 146 |
print(result) |
| 147 |
|
| 148 |
|
| 149 |
if __name__ == '__main__': main() |
| 150 |
|