Skip to content

Commit

Permalink
Artist - title example.
Browse files Browse the repository at this point in the history
  • Loading branch information
smimram committed Mar 4, 2024
1 parent bd2126e commit 9e2e6f1
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ all: test id3v2

test:
@dune test
@dune build @citest

id3v2:
@for i in id3v2/*.mp3; do \
Expand Down
2 changes: 2 additions & 0 deletions examples/artist_title
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
dune exec --no-print-directory ./artist_title.exe -- $@
34 changes: 34 additions & 0 deletions examples/artist_title.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
(** Show artist - title of a (list of) files. *)

let () =
let fname = ref [] in
Arg.parse [] (fun f -> fname := f :: !fname) "artist_title files";
if !fname = [] then (
Printf.eprintf "Please enter a filename.\n%!";
exit 1);
let fname =
!fname
|> List.map
(fun f ->
if String.contains f '*' then (
let d = Filename.dirname f in
let f =
Filename.basename f
|> Str.global_replace (Str.regexp "\\*") ".*"
|> Str.regexp
in
let files =
Sys.readdir d |> Array.to_list
|> List.filter (fun s -> Str.string_match f s 0)
in
List.map (fun f -> d ^ "/" ^ f) files)
else [f])
|> List.flatten
in
List.iter
(fun fname ->
let m = Metadata.Any.parse_file fname in
let artist = List.assoc_opt "artist" m |> Option.value ~default:"?" in
let title = List.assoc_opt "title" m |> Option.value ~default:"?" in
Printf.printf "%s - %s\n%!" artist title
) fname
6 changes: 6 additions & 0 deletions examples/dune
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
(modules meta)
(libraries str metadata))

(executable
(name artist_title)
(modules artist_title)
(libraries str metadata))

(executable
(name basic)
(modules basic)
Expand Down Expand Up @@ -40,6 +45,7 @@
(progn
(run ./basic.exe)
(run ./test.exe)
(run ./artist_title.exe %{deps})
(run ./meta.exe %{deps}))))

(rule
Expand Down

0 comments on commit 9e2e6f1

Please sign in to comment.