Skip to content

Commit

Permalink
More lyrics pane work
Browse files Browse the repository at this point in the history
  • Loading branch information
lainsce committed Aug 6, 2024
1 parent 1ac3221 commit 63ef712
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 56 deletions.
3 changes: 2 additions & 1 deletion data/app.gresource.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
<file alias="style-dark.css">style-dark.css</file>
</gresource>
<gresource prefix="/com/fyralabs/Victrola/icons">
<file alias="scalable/actions/show-lyrics-symbolic.svg">icons/hicolor/symbolic/actions/show-lyrics-symbolic.svg</file>
<file alias="scalable/actions/audio-x-generic-filled-symbolic.svg">icons/hicolor/symbolic/actions/audio-x-generic-filled-symbolic.svg</file>
<file alias="scalable/actions/media-optical-cd-audio-filled-symbolic.svg">icons/hicolor/symbolic/actions/media-optical-cd-audio-filled-symbolic.svg</file>
<file alias="scalable/actions/system-users-filled-symbolic.svg">icons/hicolor/symbolic/actions/system-users-filled-symbolic.svg</file>
</gresource>
</gresources>
</gresources>
63 changes: 63 additions & 0 deletions data/icons/hicolor/symbolic/actions/show-lyrics-symbolic.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions data/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
4 changes: 2 additions & 2 deletions data/ui/window.blp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

Expand Down
37 changes: 31 additions & 6 deletions src/Utils/lyrics-catch.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down Expand Up @@ -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("<br />", "\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 ();
Expand Down Expand Up @@ -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;
}
Expand Down
93 changes: 47 additions & 46 deletions src/Widgets/lyric-page.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand All @@ -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<void*> 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;
};
Expand Down Expand Up @@ -140,5 +137,9 @@ namespace Victrola {
scrolled.get_vadjustment ().set_value (0);
scrolled.show ();
}

public void update_cur_song (Song song) {
cur_song = song;
}
}
}
5 changes: 4 additions & 1 deletion src/window.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 (() => {
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 63ef712

Please sign in to comment.