Skip to content

Commit

Permalink
Merge pull request #21 from mamantoha/remove-deprecated-methods
Browse files Browse the repository at this point in the history
Remove deprecated methods
  • Loading branch information
mamantoha authored Jun 22, 2022
2 parents 2e1835a + 501e62a commit 7f9fd46
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 85 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ client = MPD::Client.new("localhost", password: "password")
The client library can be used as follows:

```crystal
puts client.version # print the mpd version
puts client.search("title", "crystal") # print the result of the command 'search title crystal'
client.close # send the close command
client.disconect # disconnect from the server
puts client.version # print the mpd version
client.play(2) # begins playing the playlist at song number 2
puts client.status # print the current status of the player and the volume level
client.close # send the close command
client.disconect # disconnect from the server
```

Check `MPD::Client` [source](https://mamantoha.github.io/crystal_mpd/MPD/Client.html) for supported commands.
Expand Down Expand Up @@ -146,7 +147,7 @@ The `find` commands are case sensitive, which `search` and related commands igno

```crystal
client.search("(any =~ 'crystal')")
client.findaddpl("alt_rock", "(genre == 'Alternative Rock')")
client.searchaddpl("alt_rock", "(genre == 'Alternative Rock')", sort: "-ArtistSort", window: (5..10))
client.list("filename", "((artist == 'Linkin Park') AND (date == '2003'))")
```

Expand Down
9 changes: 2 additions & 7 deletions samples/client.cr
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ puts client.currentsong
# puts client.delete(10..) # 10:
# puts client.delete(10...) # 10:
# puts client.playlistinfo(1)
# puts client.playlistsearch("title", "All Around Me")
# puts client.playlistfind("title", "All Around Me")
# puts client.playlistinfo(0..1) # 0:2
# puts client.playlistinfo(0...1) # 0:1
# puts client.playlistinfo(10..-1) # 10:
Expand All @@ -53,7 +51,6 @@ puts client.currentsong
# puts client.rm("test")
# puts client.listplaylists
# client.playlistclear("test")
# client.searchaddpl("test", "Artist", "Otep")
# puts client.listplaylist("test")
# puts client.playlistdelete("test", 0)
# puts client.listplaylistinfo("test")
Expand All @@ -66,9 +63,7 @@ puts client.currentsong
# puts client.move(10, 0)
# puts client.outputs
# puts client.tagtypes
# puts client.search("title", "crystal")
# puts client.search("(any =~ 'crystal')")
# puts client.find("artist", "Порнофильмы")
# puts client.find("(artist == 'Порнофильмы')")
# puts client.clear
# puts client.findadd("(genre == 'Alternative Rock')")
Expand All @@ -92,9 +87,9 @@ puts client.currentsong
# puts client.seekcur("-10")
# puts client.seekid(13, 45)
# puts client.seek(3, 45)
# puts client.list("album", "Linkin Park")
# puts client.list("album", "(artist == 'Linkin Park')")
# puts client.list("Genre")
# client.searchadd("Artist", "Otep")
# client.searchadd("(artist == 'Otep')")
# puts client.list("Artist")
# puts client.list("filename", "((artist == 'Linkin Park') AND (date == '2003'))")
# puts client.count("((artist == 'Linkin Park') AND (date == '2003'))")
Expand Down
10 changes: 10 additions & 0 deletions samples/count.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require "mime"
require "../src/crystal_mpd"

# MPD::Log.level = :debug
# MPD::Log.backend = ::Log::IOBackend.new

mpd = MPD::Client.new

puts mpd.count("(genre != 'Pop')", group: "artist")
puts mpd.count("(genre == 'Rock')")
12 changes: 8 additions & 4 deletions samples/make_playlist.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ optparse = OptionParser.new do |parser|
parser.banner = "Usage: salute [arguments]"
parser.on("-v", "--verbose", "Show verbose output") { options["verbose"] = true }
parser.on("-h HOST", "--host=NAME", "Specifies the MPD host") { |name| options["host"] = name }
parser.on("-t NAME", "--type=NAME", "Specifies the type") { |name| options["type"] = name }
parser.on("-q NAME", "--query=NAME", "Specifies the query") { |name| options["query"] = name }
parser.on("-t NAME", "--tag=NAME", "Specifies the tag") { |name| options["tag"] = name }
parser.on("-q NAME", "--query=NAME", "Specifies the seach query") { |name| options["query"] = name }
parser.on("-h", "--help", "Show this help") { puts parser }
parser.invalid_option do |flag|
STDERR.puts "ERROR: #{flag} is not a valid option."
Expand All @@ -26,7 +26,7 @@ end

begin
optparse.parse
mandatory = ["type", "query"]
mandatory = ["tag", "query"]
missing = mandatory.select { |param| options[param]?.nil? }
unless missing.empty?
raise OptionParser::MissingOption.new(missing.join(", "))
Expand All @@ -47,7 +47,11 @@ end
client.stop
client.clear

songs = client.search(options["type"].as(String), options["query"].as(String))
tag = options["tag"].as(String)
query = options["query"].as(String)
filter = "(#{tag} == '#{query}')"

songs = client.search(filter)

client.with_command_list do
songs.not_nil!.each do |song|
Expand Down
139 changes: 70 additions & 69 deletions src/crystal_mpd/client.cr
Original file line number Diff line number Diff line change
Expand Up @@ -551,31 +551,23 @@ module MPD
end
end

# Searches for any song that contains `what` in tag `type` and adds them to the playlist named `name`.
#
# If a playlist by that name doesn't exist it is created.
# Search the database for songs matching `filter` and add them to the queue.
#
# Parameters have the same meaning as for `#find`, except that search is not case sensitive.
def searchaddpl(name : String, type : String, query : String, position : Int32 | String | Nil = nil)
synchronize do
write_command("searchaddpl", name, type, query, position)
execute("fetch_nothing")
end
end

# Search the database for songs matching `filter` and add them to the playlist named `name`.
# If a playlist by that `name` doesn't exist it is created.
#
# If a playlist by that name doesn’t exist it is created.
# Parameters have the same meaning as for `search`.
#
# Parameters have the same meaning as for `#search `.
def searchaddpl(name : String, filler : String, *, sort : String? = nil, window : MPD::Range? = nil)
# The `position` parameter specifies where the songs will be inserted.
# It can be relative to the current song as in `addid`.
def searchaddpl(name : String, filter : String, *, sort : String? = nil, window : MPD::Range? = nil, position : Int32 | String | Nil = nil)
synchronize do
hash = {} of String => String

sort.try { hash["sort"] = sort }
window.try { hash["window"] = parse_range(window) }
position.try { hash["position"] = position }

write_command("searchaddpl", name, filter, query, hash)
write_command("searchaddpl", name, filter, hash)
execute("fetch_nothing")
end
end
Expand Down Expand Up @@ -736,26 +728,30 @@ module MPD
fetch_binary(IO::Memory.new, 0, "readpicture", uri)
end

# Count the number of songs and their total playtime in the database
# that `type` is `query`
# Count the number of songs and their total playtime in the database matching `filter`.
#
# The following prints the number of songs whose title matches "Echoes"
# ```
# mpd.count("(genre == 'Rock')")
# => {"songs" => "11", "playtime" => "2496"}
# ```
#
# The `group` keyword may be used to group the results by a tag.
# The first following example prints per-artist counts
# while the next prints the number of songs whose title matches "Echoes" grouped by artist:
#
# ```
# mpd.count("title", "Echoes")
# mpd.count("(genre != 'Pop')", group: "artist")
# => [{"Artist" => "Artist 1", "songs" => "11", "playtime" => "2388"}, {"Artist" => "Artist 2", "songs" => "12", "playtime" => "2762"}]
# ```
def count(type : String, query : String)
def count(filter : String, *, group : String? = nil)
synchronize do
write_command("count", type, query)
execute("fetch_object")
end
end
hash = {} of String => String

# Count the number of songs and their total playtime in the database matching `filter`
def count(filter : String)
synchronize do
write_command("count", filter)
execute("fetch_object")
group.try { hash["group"] = group }

write_command("count", filter, hash)

execute("fetch_counts")
end
end

Expand Down Expand Up @@ -869,31 +865,23 @@ module MPD
end
end

# Finds songs in the db that are exactly `query`.
#
# `type` can be any tag supported by MPD, or one of the two special parameters:
#
# * `file` to search by full path (relative to database root)
# * `any` to match against all available tags.
#
# `query` is what to find.
def find(type : String, query : String)
synchronize do
write_command("find", type, query)
execute("fetch_songs")
end
end

# Search the database for songs matching `filter`.
#
# `sort` sorts the result by the specified tag. The sort is descending if the tag is prefixed with a minus (‘-‘).
# Without `sort`, the order is undefined. Only the first tag value will be used, if multiple of the same type exist.
# To sort by "Artist", "Album" or "AlbumArtist", you should specify "ArtistSort", "AlbumSort" or "AlbumArtistSort"
# instead. These will automatically fall back to the former if "*Sort" doesn’t exist.
# "AlbumArtist" falls back to just “Artist”. The type "Last-Modified" can sort by file modification time.
# `sort` sorts the result by the specified tag.
# The sort is descending if the tag is prefixed with a minus (`-`).
# Without `sort`, the order is undefined.
# Only the first tag value will be used, if multiple of the same type exist.
# To sort by "Artist", “Album” or "AlbumArtist", you should specify "ArtistSort", "AlbumSort" or "AlbumArtistSort" instead.
# These will automatically fall back to the former if "*Sort" doesn't exist.
# "AlbumArtist" falls back to just "Artist".
# The type "Last-Modified" can sort by file modification time.
#
# `window` can be used to query only a portion of the real response. The parameter is two zero-based record numbers;
# a start number and an end number.
# `window` can be used to query only a portion of the real response.
# The parameter is two zero-based record numbers; a start number and an end number.
#
# ```
# mpd.find("(genre != 'Pop')", sort: "-ArtistSort", window: (5..10))
# ```
def find(filter : String, *, sort : String? = nil, window : MPD::Range? = nil)
synchronize do
hash = {} of String => String
Expand All @@ -906,20 +894,6 @@ module MPD
end
end

# Searches for any song that contains `query`.
#
# Parameters have the same meaning as for `#find`, except that search is not case sensitive.
#
# ```
# mpd.search("title", "crystal")
# ```
def search(type : String, query : String)
synchronize do
write_command("search", type, query)
execute("fetch_songs")
end
end

# Search the database for songs matching `filter`.
#
# Parameters have the same meaning as for `#find`, except that search is not case sensitive.
Expand All @@ -941,17 +915,18 @@ module MPD

# Search the database for songs matching `filter` and add them to the queue.
#
# Parameters have the same meaning as for `#find`.
# Parameters have the same meaning as for `#find` and `#searchadd`.
#
# ```
# mpd.findadd("(genre == 'Alternative Rock')")
# ```
def findadd(filter : String, *, sort : String? = nil, window : MPD::Range? = nil)
def findadd(filter : String, *, sort : String? = nil, window : MPD::Range? = nil, position : Int32 | String | Nil = nil)
synchronize do
hash = {} of String => String

sort.try { hash["sort"] = sort }
window.try { hash["window"] = parse_range(window) }
position.try { hash["position"] = position }

write_command("findadd", filter, hash)
execute("fetch_nothing")
Expand All @@ -961,12 +936,16 @@ module MPD
# Search the database for songs matching `filter` and add them to the queue.
#
# Parameters have the same meaning as for `#search`.
def searchadd(filter : String, *, sort : String? = nil, window : MPD::Range? = nil)
#
# The `position` parameter specifies where the songs will be inserted.
# It can be relative to the current song as in `#addid`.
def searchadd(filter : String, *, sort : String? = nil, window : MPD::Range? = nil, position : Int32 | String | Nil = nil)
synchronize do
hash = {} of String => String

sort.try { hash["sort"] = sort }
window.try { hash["window"] = parse_range(window) }
position.try { hash["position"] = position }

write_command("searchadd", filter, hash)
execute("fetch_nothing")
Expand Down Expand Up @@ -1234,6 +1213,28 @@ module MPD
result
end

# :nodoc:
private def fetch_counts : Object | Objects
result = MPD::Objects.new
obj = MPD::Object.new

read_pairs.each do |item|
key = item[0]
value = item[1].chomp

if obj.has_key?(key)
result << obj unless obj.empty?
obj = MPD::Object.new
end

obj[key] = value
end

result << obj unless obj.empty?

result.one? ? result.first : result
end

# :nodoc:
private def fetch_outputs
fetch_objects(["outputid"])
Expand Down

0 comments on commit 7f9fd46

Please sign in to comment.