Skip to content

Commit

Permalink
cosmetic
Browse files Browse the repository at this point in the history
  • Loading branch information
it-is-wednesday committed Nov 8, 2019
1 parent 9b608a5 commit 781ad2b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 29 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,6 @@ _build.prev/
node_modules/

# End of https://www.gitignore.io/api/ocaml,emacs

# binary produces by build
pitzulit
2 changes: 1 addition & 1 deletion dune
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
(env
(dev
(flags (:standard -w @A-48-44))))
(flags (:standard -w @A-48-44-45))))
29 changes: 29 additions & 0 deletions src/desc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type stamp_line = {
timestamp_sec: int
}


let parse_line (raw_line: string) : stamp_line option =
let parse_timestamp_string time : int option =
(* "0:02:01" -> 121 *)
Expand Down Expand Up @@ -48,6 +49,34 @@ let parse_line (raw_line: string) : stamp_line option =
timestamp_sec = timestamp}


(* given a video description, returns the tracklist in it (if any) *)
let parse_tracks_from_desc (desc: string): Track.t list =
(* gather all lines in given video description to hold a track title and
timestamp. for example:
2:30 bruh song
3:22 second bruh song *)
let stamp_lines = List.filter_map parse_line (String.split ~by:"\\n" desc) in

(* figure out track's actual time ranges out of the timestamps. we
take into account the surrounding lines to calculate it. for example,
given the previous example, we can understand that "bruh song" starts at
2:30 and ends at 3:22, because the timestamp in the following line is 3:22. *)
let num_of_lines = List.length stamp_lines in
stamp_lines |> List.mapi (fun line_num {title; timestamp_sec} ->
let time = match line_num with
(* last track *)
| x when x = (num_of_lines - 1) -> Track.End timestamp_sec
(* either the first track or a track in the middle *)
| _ ->
(* timestamp at next line *)
let next_stamp = (List.get_at_idx_exn (line_num + 1) stamp_lines).timestamp_sec in
match line_num with
| 0 -> Track.Beginning next_stamp
| _ -> Track.Middle (timestamp_sec, next_stamp)
in
Track.{title; time})


let extract_title_data video_title =
let s = String.split_on_char '-' video_title in
List.nth s 0, List.nth s 1
29 changes: 1 addition & 28 deletions src/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,6 @@ let download url =
| 0 -> ()
| error_code -> Printf.eprintf "youtube-dl failed with error code %d\n" error_code; exit 1

(* given a video description, returns the tracklist in it (if any) *)
let parse_tracks_from_desc (desc: string): Track.t list =
(* gather all lines in given video description to hold a track title and
timestamp. for example:
2:30 bruh song
3:22 second bruh song *)
let stamp_lines = List.filter_map Desc.parse_line (String.split ~by:"\\n" desc) in

(* figure out track's actual time ranges out of the timestamps. we
take into account the surrounding lines to calculate it. for example,
given the previous example, we can understand that "bruh song" starts at
2:30 and ends at 3:22, because the timestamp in the following line is 3:22. *)
let num_of_lines = List.length stamp_lines in
stamp_lines |> List.mapi (fun line_num Desc.{title; timestamp_sec} ->
let time = match line_num with
(* last track *)
| x when x = (num_of_lines - 1) -> Track.End timestamp_sec
(* either the first track or a track in the middle *)
| _ ->
(* timestamp at next line *)
let next_stamp = Desc.((List.get_at_idx_exn (line_num + 1) stamp_lines).timestamp_sec) in
match line_num with
| 0 -> Track.Beginning next_stamp
| _ -> Track.Middle (timestamp_sec, next_stamp)
in
Track.{title; time})

let main url no_download dir =
Sys.chdir dir;

Expand All @@ -55,7 +28,7 @@ let main url no_download dir =
Yojson.Basic.from_file "album.mp3.info.json"
|> Yojson.Basic.Util.member "description"
|> Yojson.Basic.to_string
|> parse_tracks_from_desc
|> Desc.parse_tracks_from_desc
|> List.iter (fun track ->
Track.extract "album.mp3" track |> ignore)

Expand Down

0 comments on commit 781ad2b

Please sign in to comment.