|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Thomas Lange <[email protected]> |
| 3 | + * |
| 4 | + * Permission to use, copy, modify, and/or distribute this software for any |
| 5 | + * purpose with or without fee is hereby granted, provided that the above |
| 6 | + * copyright notice and this permission notice appear in all copies. |
| 7 | + * |
| 8 | + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 9 | + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 10 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 11 | + * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
| 12 | + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 13 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 14 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 15 | + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 16 | + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 17 | + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 18 | + * POSSIBILITY OF SUCH DAMAGE. |
| 19 | + */ |
| 20 | + |
| 21 | +#include "lyrics.h" |
| 22 | + |
| 23 | +bool LrcLibProvider::match (LyricsState state) |
| 24 | +{ |
| 25 | + fetch (state); |
| 26 | + return true; |
| 27 | +} |
| 28 | + |
| 29 | +void LrcLibProvider::fetch (LyricsState state) |
| 30 | +{ |
| 31 | + auto handle_result_cb = [=] (const char * uri, const Index<char> & buf) { |
| 32 | + if (! buf.len ()) |
| 33 | + { |
| 34 | + update_lyrics_window_error (str_printf (_("Unable to fetch %s"), uri)); |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + String lyrics; |
| 39 | + if (! try_parse_json (buf, "plainLyrics", lyrics)) |
| 40 | + { |
| 41 | + update_lyrics_window_error (str_printf (_("Unable to parse %s"), uri)); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + LyricsState new_state = g_state; |
| 46 | + new_state.lyrics = lyrics; |
| 47 | + |
| 48 | + if (! lyrics) |
| 49 | + { |
| 50 | + update_lyrics_window_notfound (new_state); |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + new_state.source = LyricsState::Source::LrcLib; |
| 55 | + |
| 56 | + update_lyrics_window (new_state.title, new_state.artist, new_state.lyrics); |
| 57 | + persist_state (new_state); |
| 58 | + }; |
| 59 | + |
| 60 | + auto artist = str_copy (state.artist); |
| 61 | + artist = str_encode_percent (state.artist, -1); |
| 62 | + |
| 63 | + auto title = str_copy (state.title); |
| 64 | + title = str_encode_percent (state.title, -1); |
| 65 | + |
| 66 | + auto fetch_uri = str_concat( |
| 67 | + {m_base_url, "/api/get?artist_name=", artist, "&track_name=", title}); |
| 68 | + |
| 69 | + vfs_async_file_get_contents (fetch_uri, handle_result_cb); |
| 70 | + update_lyrics_window_message (state, _("Looking for lyrics ...")); |
| 71 | +} |
0 commit comments