From 63ef712076713c3baf5829a0e232b4d637e6c441 Mon Sep 17 00:00:00 2001 From: lainsce Date: Tue, 6 Aug 2024 10:36:54 -0300 Subject: [PATCH] More lyrics pane work --- data/app.gresource.xml | 3 +- .../symbolic/actions/show-lyrics-symbolic.svg | 63 +++++++++++++ data/style.css | 9 ++ data/ui/window.blp | 4 +- src/Utils/lyrics-catch.vala | 37 ++++++-- src/Widgets/lyric-page.vala | 93 ++++++++++--------- src/window.vala | 5 +- 7 files changed, 158 insertions(+), 56 deletions(-) create mode 100644 data/icons/hicolor/symbolic/actions/show-lyrics-symbolic.svg diff --git a/data/app.gresource.xml b/data/app.gresource.xml index bf98187..e6a3989 100644 --- a/data/app.gresource.xml +++ b/data/app.gresource.xml @@ -7,8 +7,9 @@ style-dark.css + icons/hicolor/symbolic/actions/show-lyrics-symbolic.svg icons/hicolor/symbolic/actions/audio-x-generic-filled-symbolic.svg icons/hicolor/symbolic/actions/media-optical-cd-audio-filled-symbolic.svg icons/hicolor/symbolic/actions/system-users-filled-symbolic.svg - \ No newline at end of file + diff --git a/data/icons/hicolor/symbolic/actions/show-lyrics-symbolic.svg b/data/icons/hicolor/symbolic/actions/show-lyrics-symbolic.svg new file mode 100644 index 0000000..512c545 --- /dev/null +++ b/data/icons/hicolor/symbolic/actions/show-lyrics-symbolic.svg @@ -0,0 +1,63 @@ + + diff --git a/data/style.css b/data/style.css index 86b6735..eec2db8 100644 --- a/data/style.css +++ b/data/style.css @@ -71,3 +71,12 @@ -gtk-icon-size: 24px; color: @view_bg_color; } + +.view-lyric text { + background: none; + margin: 6px; + padding: 24px; +} +.view-lyric { + border-radius: 12px 12px 0 0; +} diff --git a/data/ui/window.blp b/data/ui/window.blp index 0df9941..996ff2b 100644 --- a/data/ui/window.blp +++ b/data/ui/window.blp @@ -174,8 +174,8 @@ template VictrolaMainWindow : He.ApplicationWindow { [titlebar-toggle] ToggleButton lyrics_btn { - icon-name: "edit-find-symbolic"; - tooltip-text: _("See Lyrics"); + icon-name: "show-lyrics-symbolic"; + tooltip-text: _("Show Lyrics"); } } diff --git a/src/Utils/lyrics-catch.vala b/src/Utils/lyrics-catch.vala index 103b8e9..26682d9 100644 --- a/src/Utils/lyrics-catch.vala +++ b/src/Utils/lyrics-catch.vala @@ -19,22 +19,18 @@ */ namespace Victrola { - public class LyricsAPI : GLib.Object { - public LyricsAPI () { - - } - + public LyricsAPI () {} } public class LyricsFetcher : GLib.Object { - private string[] lyrics_apis = {}; public LyricsFetcher () { lyrics_apis += "music_163"; lyrics_apis += "letras_mus"; lyrics_apis += "lyrics_wikia"; + lyrics_apis += "random"; } private Lyric? get_music_163(string title, string artist){ @@ -126,6 +122,33 @@ namespace Victrola { return null; } + private Lyric? get_random_lyric (string artist, string title){ + var seeds_url = "https://www.azlyrics.com/lyrics/"; + var session = new Soup.Session (); + session.timeout = 5; + var url = seeds_url + artist.replace(" ", "").down() + "/" + title.replace(" ", "").down() + ".html"; + var message = new Soup.Message ("GET", url); + + /* send a sync request */ + session.send_message (message); + + // parse html + var html_cntx = new Html.ParserCtxt(); + html_cntx.use_options(Html.ParserOption.NOERROR + Html.ParserOption.NOWARNING); + var result_string = (string) message.response_body.flatten ().data; + + var doc = html_cntx.read_doc(result_string.replace("
", "\n"), ""); + var lyricbox = getValue(doc, "//div[contains(@class, 'row')]"); + + if(lyricbox == null){ + return null; + } + var lyric = new Lyric(); + lyric.lyric = lyricbox; + lyric.current_url = url; + return lyric; + } + private Lyric? get_lyrics_wikia(string title, string artist){ var seeds_url = "http://lyrics.wikia.com/wiki/"; var session = new Soup.Session (); @@ -220,6 +243,8 @@ namespace Victrola { r = get_lyrics_wikia(n_title, n_artist); }else if(s_api == "letras_mus"){ r = get_letras_mus(n_title, n_artist); + }else if(s_api == "random"){ + r = get_random_lyric(n_title, n_artist); }else{ return null; } diff --git a/src/Widgets/lyric-page.vala b/src/Widgets/lyric-page.vala index 419323b..844a1b3 100644 --- a/src/Widgets/lyric-page.vala +++ b/src/Widgets/lyric-page.vala @@ -27,12 +27,11 @@ namespace Victrola { public string last_artist; private int64 last_position; private bool was_paused; + public Song? cur_song { get; set; } public LyricPage (Gtk.Window window) { Object ( - margin_start: 18, - margin_end: 18, - window: window + window : window ); last_title = ""; last_artist = ""; @@ -45,68 +44,66 @@ namespace Victrola { fetcher = new LyricsFetcher (); - view = new Gtk.TextView (); - view.editable = false; - view.set_wrap_mode (Gtk.WrapMode.WORD); - view.vexpand = true; + view = new Gtk.TextView () { + editable = false, + wrap_mode = Gtk.WrapMode.WORD, + vexpand = true, + cursor_visible = false, + top_margin = 12, + left_margin = 12, + right_margin = 12 + }; view.add_css_class ("view-lyric"); + view.remove_css_class ("view"); scrolled = new Gtk.ScrolledWindow (); - scrolled.margin_bottom = 18; scrolled.set_child (view); + var get_lyrics_button = new He.Button ("", "Fetch Lyrics") { + halign = Gtk.Align.CENTER, + is_pill = true, + margin_bottom = 18 + }; + var main_box = new Gtk.Box (Gtk.Orientation.VERTICAL, 12); main_box.vexpand = main_box.hexpand = true; - main_box.margin_top = 18; main_box.append (scrolled); + main_box.append (get_lyrics_button); this.child = main_box; this.vexpand = this.hexpand = true; - // update_lyric.begin (app); + get_lyrics_button.clicked.connect (() => { + update_lyric.begin (); + }); } - private async void update_lyric (Application app) { + private async void update_lyric () { ThreadFunc run = () => { - var sub = ""; - var title = ""; - var url = ""; var lyric = ""; - try { - bool error = false; - var r = fetcher.get_lyric (app.current_song.title, app.current_song.artist); - if (r != null) { - lyric = r.lyric; - url = r.current_url; - title = r.title; - sub = r.lyric_sync; - if (title != last_title) { - return null; - } - Idle.add (() => { - clean_text_buffer (); - return false; - }); - - if (lyric == "" || lyric == null) { - error = true; - } - current_lyric = r; - } else { + bool error = false; + var r = fetcher.get_lyric (cur_song.title, cur_song.artist); + + if (r != null) { + lyric = r.lyric; + if (lyric == "") { error = true; } + current_lyric = r; + } else { + error = true; + } - if (error == true) { - scrolled.hide (); - } else { - Idle.add (() => { - insert_text (lyric); - show_lyrics (); - return false; - }); - } - } catch (Error e) { - warning ("Failed to get lyric: %s", e.message); + if (error == true) { + clean_text_buffer (); + insert_text (_("No lyrics found!")); + } else { + Idle.add (() => { + clean_text_buffer (); + insert_text (lyric); + show_lyrics (); + return false; + }); } return null; }; @@ -140,5 +137,9 @@ namespace Victrola { scrolled.get_vadjustment ().set_value (0); scrolled.show (); } + + public void update_cur_song (Song song) { + cur_song = song; + } } } diff --git a/src/window.vala b/src/window.vala index 743a10e..a22d870 100644 --- a/src/window.vala +++ b/src/window.vala @@ -65,6 +65,7 @@ namespace Victrola { private PlayBar play_bar; private PlayBarMobile play_bar_mobile; private InfoPage info_page; + private LyricPage lyric_page; uint num1; uint num2; uint num3; @@ -126,7 +127,7 @@ namespace Victrola { info_box_mobile.append (play_bar_mobile); - var lyric_page = new LyricPage (this); + lyric_page = new LyricPage (this); lyrics_box.append (lyric_page); lyrics_btn.toggled.connect (() => { @@ -266,6 +267,7 @@ namespace Victrola { private void on_song_changed (Song song) { update_song_info (song); + lyric_page.update_cur_song (song); action_set_enabled (ACTION_APP + ACTION_PLAY, true); } @@ -375,6 +377,7 @@ namespace Victrola { private void update_song_info (Song song) { info_page.update (song); play_bar_mobile.update (song); + lyric_page.update_cur_song (song); } public async void accent_set (Gdk.Pixbuf? pixbuf) {