Commit d9839d19 authored by Thiago's avatar Thiago 💬 Committed by Victor Toso

lua-factory: Remove metrolyrics plugin

Remove the metrolyrics plugin
parent 3e9e8d54
Pipeline #33224 passed with stage
in 3 minutes and 48 seconds
--[[
* Copyright (C) 2014 Victor Toso.
*
* Contact: Victor Toso <me@victortoso.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; version 2.1 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*
--]]
---------------------------
-- Source initialization --
---------------------------
source = {
id = "grl-metrolyrics",
name = "Metrolyrics",
description = "a source for lyrics",
supported_keys = { "lyrics" },
supported_media = { 'audio', 'video' },
resolve_keys = {
["type"] = "audio",
required = { "artist", "title" },
},
tags = { 'music', 'net:internet', 'net:plaintext' },
}
netopts = {
user_agent = "Grilo Source Metrolyrics/0.2.8",
}
------------------
-- Source utils --
------------------
METROLYRICS_INVALID_URL_CHARS = "[" .. "%(%)%[%]%$%&%%" .. "]"
METROLYRICS_DEFAULT_QUERY = "http://www.metrolyrics.com/%s-lyrics-%s.html"
---------------------------------
-- Handlers of Grilo functions --
---------------------------------
function grl_source_resolve()
local url, req
local artist, title
req = grl.get_media_keys()
if not req or not req.artist or not req.title
or #req.artist == 0 or #req.title == 0 then
grl.callback()
return
end
-- Prepare artist and title strings to the url
artist = req.artist:gsub(METROLYRICS_INVALID_URL_CHARS, "")
artist = artist:gsub("%s+", "-")
artist = grl.encode(artist)
title = req.title:gsub(METROLYRICS_INVALID_URL_CHARS, "")
title = title:gsub("%s+", "-")
title = grl.encode(title)
url = string.format(METROLYRICS_DEFAULT_QUERY, title, artist)
grl.fetch(url, netopts, fetch_page_cb)
end
---------------
-- Utilities --
---------------
function fetch_page_cb(feed)
local media = nil
if feed and not feed:find("notfound") then
media = metrolyrics_get_lyrics(feed)
end
grl.callback(media, 0)
end
function metrolyrics_get_lyrics(feed)
local media = {}
local lyrics_body = '<div id="lyrics%-body%-text".->(.-)<p class="writers"'
local noise_array = {
{ noise = '<div id="mid%-song%-discussion".->.+</div>\n<p', sub = "<p" },
{ noise = '</div>', sub = "" },
{ noise = "</p>", sub = "\n\n" },
{ noise = "<p class='verse'><p class='verse'>", sub = "\n\n" },
{ noise = "<p class='verse'>", sub = "" },
{ noise = "<br/>", sub = "" },
{ noise = "<br>", sub = "" },
}
-- remove html noise
feed = feed:match(lyrics_body)
if not feed then
grl.debug ("This Lyrics do not match our parser")
return nil
end
for _, it in ipairs (noise_array) do
feed = feed:gsub(it.noise, it.sub)
end
-- strip the lyrics
feed = feed:gsub("^[%s%W]*(.-)[%s%W]*$", "%1")
-- switch table to string
media.lyrics = feed
return media
end
......@@ -12,7 +12,6 @@ sources = [
'grl-guardianvideos',
'grl-itunes-podcast',
'grl-lastfm-cover',
'grl-metrolyrics',
'grl-musicbrainz',
'grl-pocket',
'grl-radiofrance',
......
......@@ -9,12 +9,7 @@ acoustid_resources = gnome.compile_resources('acoustidresources',
'test_lua_acoustid.gresource.xml',
c_name: '_grl_lua_acoustid')
metrolyrics_resources = gnome.compile_resources('metrolyricsresources',
'test_lua_metrolyrics.gresource.xml',
c_name: '_grl_lua_metrolyrics')
source_tests = [
# 'test_lua_metrolyrics',
]
if chromaprint_enabled
......@@ -28,7 +23,7 @@ source_common = [
'test_lua_factory_utils.h',
]
resources = acoustid_resources + metrolyrics_resources
resources = acoustid_resources
foreach t: source_tests
exe = executable(t, [t + '.c'] + source_common + resources,
......
/*
* Copyright (C) 2015. All rights reserved.
*
* Author: Victor Toso <me@victortoso.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; version 2.1 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*
*/
#include "test_lua_factory_utils.h"
#define METROLYRICS_ID "grl-metrolyrics"
#define METROLYRICS_OPS GRL_OP_RESOLVE
#define GRESOURCE_PREFIX "resource:///org/gnome/grilo/plugins/test/metrolyrics/data/"
#define LYRICS_RING_OF_FIRE GRESOURCE_PREFIX "lyrics_ring_of_fire.txt"
#define LYRICS_BACK_IT_UP GRESOURCE_PREFIX "lyrics_back_it_up.txt"
#define LYRICS_BOHEMIAN_RHAPSODY GRESOURCE_PREFIX "lyrics_bohemian_rhapsody.txt"
#define LYRICS_NOBODYS_PERFECT GRESOURCE_PREFIX "lyrics_nobodys_perfect.txt"
static gchar *
get_lyrics (GrlSource *source,
const gchar *artist,
const gchar *title)
{
GList *keys;
GrlMedia *audio;
GrlOperationOptions *options;
GError *error = NULL;
gchar *lyrics;
audio = grl_media_audio_new ();
grl_media_set_artist (audio, artist);
grl_media_set_title (audio, title);
keys = grl_metadata_key_list_new (GRL_METADATA_KEY_LYRICS, NULL);
options = grl_operation_options_new (NULL);
grl_operation_options_set_resolution_flags (options, GRL_RESOLVE_NORMAL);
grl_source_resolve_sync (source,
GRL_MEDIA (audio),
keys,
options,
&error);
g_assert_no_error (error);
lyrics = g_strdup (grl_media_get_lyrics (audio));
g_list_free (keys);
g_object_unref (options);
g_object_unref (audio);
return lyrics;
}
static void
test_resolve_metrolyrics (void)
{
GrlSource *source;
guint i;
struct {
gchar *title;
gchar *artist;
gchar *lyrics_file;
} audios[] = {
{ "ring of fire", "johnny cash", LYRICS_RING_OF_FIRE },
{ "back it up", "caro emerald", LYRICS_BACK_IT_UP },
{ "bohemian rhapsody", "queen", LYRICS_BOHEMIAN_RHAPSODY },
{ "nobodys perfect", "jessie j", LYRICS_NOBODYS_PERFECT },
{ "100% pure love", "crystal waters", NULL },
};
source = test_lua_factory_get_source (METROLYRICS_ID, METROLYRICS_OPS);
for (i = 0; i < G_N_ELEMENTS (audios); i++) {
gchar *lyrics, *data;
GFile *file;
gsize size;
GError *error = NULL;
lyrics = get_lyrics (source, audios[i].artist, audios[i].title);
if (audios[i].lyrics_file == NULL) {
/* We are not interested in comparing this lyrics */
g_clear_pointer (&lyrics, g_free);
continue;
}
g_assert_nonnull (lyrics);
file = g_file_new_for_uri (audios[i].lyrics_file);
g_file_load_contents (file, NULL, &data, &size, NULL, &error);
g_assert_no_error (error);
g_clear_pointer (&file, g_object_unref);
if (g_ascii_strncasecmp (lyrics, data, size - 1) != 0) {
char *filename;
int fd;
fd = g_file_open_tmp ("metrolyrics-test-XXXXXX", &filename, NULL);
g_assert (fd > 0);
g_assert (g_file_set_contents (filename, lyrics, -1, NULL));
close (fd);
g_warning ("Lyrics of '%s' from '%s' changed. Check if metrolyrics.com changed. New lyrics saved in '%s'",
audios[i].title, audios[i].artist, filename);
g_free (filename);
}
g_clear_pointer (&lyrics, g_free);
g_clear_pointer (&data, g_free);
}
}
static void
test_resolve_metrolyrics_bad_request (void)
{
GrlSource *source;
guint i;
struct {
gchar *title;
gchar *artist;
gchar *lyrics_file;
} audios[] = {
{ "GNOME", "grilo framework", NULL },
};
source = test_lua_factory_get_source (METROLYRICS_ID, METROLYRICS_OPS);
for (i = 0; i < G_N_ELEMENTS (audios); i++) {
gchar *lyrics;
g_test_expect_message("Grilo", G_LOG_LEVEL_WARNING, "*Can't fetch element*");
lyrics = get_lyrics (source, audios[i].artist, audios[i].title);
g_assert_null (lyrics);
}
}
gint
main (gint argc, gchar **argv)
{
test_lua_factory_init (&argc, &argv, FALSE);
test_lua_factory_setup (NULL);
g_test_add_func ("/lua_factory/sources/metrolyrics", test_resolve_metrolyrics);
g_test_add_func ("/lua_factory/sources/metrolyrics/bad-request", test_resolve_metrolyrics_bad_request);
gint result = g_test_run ();
test_lua_factory_shutdown ();
test_lua_factory_deinit ();
return result;
}
<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/org/gnome/grilo/plugins/test/metrolyrics">
<file compressed="false">data/lyrics_back_it_up.txt</file>
<file compressed="false">data/lyrics_bohemian_rhapsody.txt</file>
<file compressed="false">data/lyrics_nobodys_perfect.txt</file>
<file compressed="false">data/lyrics_ring_of_fire.txt</file>
</gresource>
</gresources>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment