Created
November 19, 2013 20:51
-
-
Save fmoralesc/7552334 to your computer and use it in GitHub Desktop.
This file contains 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 python2 | |
import sys | |
import re | |
import shlex | |
import getopt | |
import mpd | |
from subprocess import Popen, PIPE | |
def get(): | |
try: | |
c = mpd.MPDClient() | |
c.connect("localhost", 6600) | |
cursong = c.currentsong()["file"] | |
out, err = Popen(shlex.split('mpc sticker "' + cursong + '" get rating'), stdout=PIPE, stderr=PIPE).communicate() | |
return out | |
except: | |
pass | |
def score(rating): | |
try: | |
c = mpd.MPDClient() | |
c.connect("localhost", 6600) | |
cursong = c.currentsong()["file"] | |
Popen(shlex.split('mpc sticker "' + cursong + '" set rating ' + str(rating)), stdout=PIPE, stderr=PIPE) | |
except: | |
pass | |
def remove(): | |
try: | |
c = mpd.MPDClient() | |
c.connect("localhost", 6600) | |
cursong = c.currentsong()["file"] | |
Popen(shlex.split('mpc sticker "' + cursong + '" delete rating'), stdout=PIPE, stderr=PIPE) | |
except: | |
pass | |
def get_list(rating_filter=None): | |
out, err = Popen(shlex.split('mpc sticker "" find rating'), stdout=PIPE).communicate() | |
data = filter(lambda i: i != "", out.split("\n")) | |
songdata = [] | |
for item in data: | |
item_data = re.search("(?P<file>.*(?=:)).\srating=(?P<rating>\d*)", item) | |
if item_data: | |
songdata.append((item_data.group("file"), int(item_data.group("rating")))) | |
if rating_filter: | |
filter_data = re.search("(?P<comp>[<>=]{,2})(?P<value>\d+)", rating_filter) | |
if filter_data: | |
if filter_data.group("comp") == ">": | |
songdata = filter(lambda i: i[1] > int(filter_data.group("value")), songdata) | |
elif filter_data.group("comp") == "<": | |
songdata = filter(lambda i: i[1] < int(filter_data.group("value")), songdata) | |
elif filter_data.group("comp") == "=": | |
songdata = filter(lambda i: i[1] == int(filter_data.group("value")), songdata) | |
elif filter_data.group("comp") == "<=": | |
songdata = filter(lambda i: i[1] <= int(filter_data.group("value")), songdata) | |
elif filter_data.group("comp") == ">=": | |
songdata = filter(lambda i: i[1] >= int(filter_data.group("value")), songdata) | |
return songdata | |
if __name__ == "__main__": | |
optlist, args = getopt.getopt(sys.argv[1:], "grs:l:") | |
for opt, val in optlist: | |
if opt == "-g": | |
rating = get() | |
if rating: | |
print(rating) | |
break | |
elif opt == "-r": | |
remove() | |
break | |
elif opt == "-s": | |
score(val) | |
break | |
elif opt == "-l": | |
for i in get_list(val): | |
print(i[0]) | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment