mpd mpc查询语言
mpd mpc query language
我不知道如何使用 mpc
正确查询 mpd
。
例如:我知道如何列出所有专辑
mpc list album
但我想得到的不仅仅是名字。
如何查询 mpd
专辑、路径、艺术家、曲目编号、持续时间等?最好在一个查询中,但多个查询也可以。
我试图阅读 mpc
手册页和 mpd
的官方文档,但无法理解。
import audioscrobbler
import mpd
import random
import time
lastsong = {}
def timer_control():
get_similar()
time.sleep(10)
timer_control()
def get_similar():
audioscrobbler
client = mpd.MPDClient()
client.connect("localhost", 6600)
mpdstatus = client.status()
prevsonginfo = client.currentsong()
global lastsong
if mpdstatus['state'] == "stop": return
if prevsonginfo == lastsong: return
lastsong = prevsonginfo
similarartists = ""
song = prevsonginfo
#if not song: break #No song, do nothing
prevartist = song['artist']
# Is the info already cached?
similar_cache = {}
if similar_cache.has_key(prevartist):
similarartists = similar_cache[prevartist]
else:
#Not cached so fetch from Audioscrobbler
try:
similarartists = [artist.name for artist in audioscrobbler.AudioScrobblerQuery(artist=prevartist).similar()]
# Cache search results and save some time next search
similar_cache[prevartist] = similarartists
except audioscrobbler.AudioScrobblerError:
similar_cache[prevartist] = None # Empty cache
return # Do nothing!
if not similarartists: return # Empty list
# Split list in half and sort upper half
# this means good matches will have priority
# but makes sure artist A does not always result in artist B
half_idx = len(similarartists)/2
upperhalf = similarartists[:half_idx]
lowerhalf = similarartists[half_idx:]
random.shuffle(upperhalf)
artistlist = upperhalf
artistlist.extend(lowerhalf)
# Try each artist in order
for artist in artistlist:
try:
print "Trying:",artist
songs = client.search("artist", artist)
if not songs: continue
selected_song = random.sample(songs, 1)[0]
client.add(selected_song['file'])
print "Added", selected_song['title'],"by",selected_song['artist']
# Delete old song from playlist?
break
except mpd.MPDError, e:
print "MPDError", e.message
continue
except ValueError, e:
print "ValueError:",e.message
continue
timer_control()
关注这篇文章了解更多信息
https://bbs.archlinux.org/viewtopic.php?id=49765
http://manpages.ubuntu.com/manpages/intrepid/man1/mpc.1.html
mpc
(在撰写本文时,v0.27-1)没有任何命令可以为您提供由 uri 标识的歌曲的所有元数据。没想到,居然没有。
然而,MPD communication protocol 确实 return 关于每首歌曲的完整信息。
如果我发出 mpc current
我只会得到这样的回应:
Afro Celt Sound System - Release
但是,如果我发送 MPD(而非 MPC)命令 currentsong
,那么我会从 MPD 收到此响应:
file: gavin/Compilations/Volume 2 _ Release/01 Release.m4a
Last-Modified: 2005-03-02T14:16:51Z
Artist: Afro Celt Sound System
Album: Volume 2 : Release
Title: Release
Track: 1/11
Genre:World
Date: 1999
Composer: Simon Emerson, James McNally, Iarla O Lionaird, Martin Russell & Sinead O'Connor/Sinéad O'Connor
Disc: 1/1
Time: 456
Pos: 0
Id: 53616
如果我发送 MPD 命令 playlistid 49312
我会收到这样的回复:
file: gavin/ATB/Future Memories/16 Missing 1.m4a
Last-Modified: 2013-11-17T21:14:43Z
Artist: ATB
Album: Future Memories
Title: Missing
Track: 16/29
Genre: Dance
Date: 2009-05-01T07:00:00Z
Composer: Tracey Thorn & Ben Watt
Disc: 1/1
AlbumArtist: ATB
Time: 256
Pos: 10
Id: 49312
如果我发送命令 search file surfing
我会得到这个响应(不同的歌曲由新的 file:
分隔):
file: doza/Air/06 Surfing On a Rocket.m4a
Last-Modified: 2015-11-20T15:56:00Z
Time: 223
Artist: Air
Album: Talkie Walkie
Title: Surfing On a Rocket
Track: 6/10
Genre: Electronic
Date: 2004-01-26T08:00:00Z
Disc: 1/1
AlbumArtist: Air
file: gavin/Air/Surfing On a Rocket EP/06 Surfing on a rocket (remixed by Joakim).m4a
Last-Modified: 2015-01-13T15:31:39Z
Time: 393
Artist: Air
Album: Surfing On a Rocket EP
Title: Surfing on a rocket (remixed by Joakim)
Track: 6/7
Genre: Dance
Date: 2004-09-10T07:00:00Z
Composer: Jean-Benoit Dunckel & Nicolas Godin
Disc: 1/1
AlbumArtist: Air
file: gavin/The Beach Boys/Greatest Surfing Songs!/02 Little Deuce Coupe.mp3
Last-Modified: 2009-09-10T04:32:49Z
Time: 111
Artist: The Beach Boys
Title: Little Deuce Coupe
Album: Greatest Surfing Songs!
Track: 2
Genre: Sunshine Pop
如果我发送 MPD 命令 listplaylistinfo Thump
,我会得到这样的响应:
file: gavin/Muse/The 2nd Law (Deluxe Version)/02 Madness.m4a
Last-Modified: 2013-11-17T22:30:54Z
Artist: Muse
Album: The 2nd Law (Deluxe Version)
Title: Madness
Track: 2/13
Genre: Alternative
Date: 2012-10-01T07:00:00Z
Disc: 1/1
AlbumArtist: Muse
Time: 280
file: gavin/U2/The Best Of 1990-2000/15 Numb (New Mix).mp3
Last-Modified: 2003-09-24T14:31:05Z
Artist: U2
Title: Numb (New Mix)
Album: The Best Of 1990-2000
Track: 15/16
Date: 2002
Genre: Rock
Composer: U2
Time: 264
file: gavin/Massive Attack/Mezzanine/03 Teardrop.m4a
Last-Modified: 2012-09-05T19:38:14Z
Artist: Massive Attack
Album: Mezzanine
Title: Teardrop
Track: 3/11
Genre: Electronic
Date: 1998-04-20T07:00:00Z
Disc: 1/1
AlbumArtist: Massive Attack
Time: 331
file: gavin/Massive Attack/Mezzanine/01 Angel.m4a
Last-Modified: 2012-09-05T19:38:17Z
Artist: Massive Attack
Album: Mezzanine
Title: Angel
Track: 1/11
Genre: Electronic
Date: 1998-04-20T07:00:00Z
Disc: 1/1
AlbumArtist: Massive Attack
Time: 380
file: gavin/Eels/Shrek 2/07 I Need Some Sleep.mp3
Last-Modified: 2005-01-14T21:24:25Z
Artist: Eels
Title: I Need Some Sleep
Album: Shrek 2
Track: 7/14
Date: 2004
Genre: Soundtrack
Time: 147
我个人使用 ruby-mpd
库以丰富的 full-featured 方式提供与 MPD 通信的便捷接口。
我不知道如何使用 mpc
正确查询 mpd
。
例如:我知道如何列出所有专辑
mpc list album
但我想得到的不仅仅是名字。
如何查询 mpd
专辑、路径、艺术家、曲目编号、持续时间等?最好在一个查询中,但多个查询也可以。
我试图阅读 mpc
手册页和 mpd
的官方文档,但无法理解。
import audioscrobbler
import mpd
import random
import time
lastsong = {}
def timer_control():
get_similar()
time.sleep(10)
timer_control()
def get_similar():
audioscrobbler
client = mpd.MPDClient()
client.connect("localhost", 6600)
mpdstatus = client.status()
prevsonginfo = client.currentsong()
global lastsong
if mpdstatus['state'] == "stop": return
if prevsonginfo == lastsong: return
lastsong = prevsonginfo
similarartists = ""
song = prevsonginfo
#if not song: break #No song, do nothing
prevartist = song['artist']
# Is the info already cached?
similar_cache = {}
if similar_cache.has_key(prevartist):
similarartists = similar_cache[prevartist]
else:
#Not cached so fetch from Audioscrobbler
try:
similarartists = [artist.name for artist in audioscrobbler.AudioScrobblerQuery(artist=prevartist).similar()]
# Cache search results and save some time next search
similar_cache[prevartist] = similarartists
except audioscrobbler.AudioScrobblerError:
similar_cache[prevartist] = None # Empty cache
return # Do nothing!
if not similarartists: return # Empty list
# Split list in half and sort upper half
# this means good matches will have priority
# but makes sure artist A does not always result in artist B
half_idx = len(similarartists)/2
upperhalf = similarartists[:half_idx]
lowerhalf = similarartists[half_idx:]
random.shuffle(upperhalf)
artistlist = upperhalf
artistlist.extend(lowerhalf)
# Try each artist in order
for artist in artistlist:
try:
print "Trying:",artist
songs = client.search("artist", artist)
if not songs: continue
selected_song = random.sample(songs, 1)[0]
client.add(selected_song['file'])
print "Added", selected_song['title'],"by",selected_song['artist']
# Delete old song from playlist?
break
except mpd.MPDError, e:
print "MPDError", e.message
continue
except ValueError, e:
print "ValueError:",e.message
continue
timer_control()
关注这篇文章了解更多信息 https://bbs.archlinux.org/viewtopic.php?id=49765 http://manpages.ubuntu.com/manpages/intrepid/man1/mpc.1.html
mpc
(在撰写本文时,v0.27-1)没有任何命令可以为您提供由 uri 标识的歌曲的所有元数据。没想到,居然没有。
然而,MPD communication protocol 确实 return 关于每首歌曲的完整信息。
如果我发出 mpc current
我只会得到这样的回应:
Afro Celt Sound System - Release
但是,如果我发送 MPD(而非 MPC)命令 currentsong
,那么我会从 MPD 收到此响应:
file: gavin/Compilations/Volume 2 _ Release/01 Release.m4a
Last-Modified: 2005-03-02T14:16:51Z
Artist: Afro Celt Sound System
Album: Volume 2 : Release
Title: Release
Track: 1/11
Genre:World
Date: 1999
Composer: Simon Emerson, James McNally, Iarla O Lionaird, Martin Russell & Sinead O'Connor/Sinéad O'Connor
Disc: 1/1
Time: 456
Pos: 0
Id: 53616
如果我发送 MPD 命令 playlistid 49312
我会收到这样的回复:
file: gavin/ATB/Future Memories/16 Missing 1.m4a
Last-Modified: 2013-11-17T21:14:43Z
Artist: ATB
Album: Future Memories
Title: Missing
Track: 16/29
Genre: Dance
Date: 2009-05-01T07:00:00Z
Composer: Tracey Thorn & Ben Watt
Disc: 1/1
AlbumArtist: ATB
Time: 256
Pos: 10
Id: 49312
如果我发送命令 search file surfing
我会得到这个响应(不同的歌曲由新的 file:
分隔):
file: doza/Air/06 Surfing On a Rocket.m4a
Last-Modified: 2015-11-20T15:56:00Z
Time: 223
Artist: Air
Album: Talkie Walkie
Title: Surfing On a Rocket
Track: 6/10
Genre: Electronic
Date: 2004-01-26T08:00:00Z
Disc: 1/1
AlbumArtist: Air
file: gavin/Air/Surfing On a Rocket EP/06 Surfing on a rocket (remixed by Joakim).m4a
Last-Modified: 2015-01-13T15:31:39Z
Time: 393
Artist: Air
Album: Surfing On a Rocket EP
Title: Surfing on a rocket (remixed by Joakim)
Track: 6/7
Genre: Dance
Date: 2004-09-10T07:00:00Z
Composer: Jean-Benoit Dunckel & Nicolas Godin
Disc: 1/1
AlbumArtist: Air
file: gavin/The Beach Boys/Greatest Surfing Songs!/02 Little Deuce Coupe.mp3
Last-Modified: 2009-09-10T04:32:49Z
Time: 111
Artist: The Beach Boys
Title: Little Deuce Coupe
Album: Greatest Surfing Songs!
Track: 2
Genre: Sunshine Pop
如果我发送 MPD 命令 listplaylistinfo Thump
,我会得到这样的响应:
file: gavin/Muse/The 2nd Law (Deluxe Version)/02 Madness.m4a
Last-Modified: 2013-11-17T22:30:54Z
Artist: Muse
Album: The 2nd Law (Deluxe Version)
Title: Madness
Track: 2/13
Genre: Alternative
Date: 2012-10-01T07:00:00Z
Disc: 1/1
AlbumArtist: Muse
Time: 280
file: gavin/U2/The Best Of 1990-2000/15 Numb (New Mix).mp3
Last-Modified: 2003-09-24T14:31:05Z
Artist: U2
Title: Numb (New Mix)
Album: The Best Of 1990-2000
Track: 15/16
Date: 2002
Genre: Rock
Composer: U2
Time: 264
file: gavin/Massive Attack/Mezzanine/03 Teardrop.m4a
Last-Modified: 2012-09-05T19:38:14Z
Artist: Massive Attack
Album: Mezzanine
Title: Teardrop
Track: 3/11
Genre: Electronic
Date: 1998-04-20T07:00:00Z
Disc: 1/1
AlbumArtist: Massive Attack
Time: 331
file: gavin/Massive Attack/Mezzanine/01 Angel.m4a
Last-Modified: 2012-09-05T19:38:17Z
Artist: Massive Attack
Album: Mezzanine
Title: Angel
Track: 1/11
Genre: Electronic
Date: 1998-04-20T07:00:00Z
Disc: 1/1
AlbumArtist: Massive Attack
Time: 380
file: gavin/Eels/Shrek 2/07 I Need Some Sleep.mp3
Last-Modified: 2005-01-14T21:24:25Z
Artist: Eels
Title: I Need Some Sleep
Album: Shrek 2
Track: 7/14
Date: 2004
Genre: Soundtrack
Time: 147
我个人使用 ruby-mpd
库以丰富的 full-featured 方式提供与 MPD 通信的便捷接口。