2011-06-19 14:58:48 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 2011 Birunthan Mohanathas (www.poiru.net)
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program 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 General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
2012-01-23 06:36:15 +00:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2011-06-19 14:58:48 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "StdAfx.h"
|
|
|
|
#include "Player.h"
|
|
|
|
#include "Internet.h"
|
|
|
|
#include "Lyrics.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Download lyrics from various serivces.
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
bool Lyrics::GetFromInternet(const std::wstring& artist, const std::wstring& title, std::wstring& out)
|
2011-06-19 14:58:48 +00:00
|
|
|
{
|
2013-05-31 14:18:52 +00:00
|
|
|
std::wstring encArtist = Internet::EncodeUrl(artist);
|
|
|
|
std::wstring encTitle = Internet::EncodeUrl(title);
|
2011-06-19 14:58:48 +00:00
|
|
|
|
|
|
|
bool found = GetFromWikia(encArtist, encTitle, out) ||
|
|
|
|
GetFromLYRDB(encArtist, encTitle, out) ||
|
|
|
|
GetFromLetras(encArtist, encTitle, out);
|
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Download lyrics from LyricWiki.
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
bool Lyrics::GetFromWikia(const std::wstring& artist, const std::wstring& title, std::wstring& data)
|
2011-06-19 14:58:48 +00:00
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
|
2011-09-08 12:42:03 +00:00
|
|
|
std::wstring url = L"http://lyrics.wikia.com/api.php?func=getSong&fmt=json&artist=" + artist;
|
2011-06-19 14:58:48 +00:00
|
|
|
url += L"&song=";
|
|
|
|
url += title;
|
|
|
|
|
2013-05-31 14:18:52 +00:00
|
|
|
data = Internet::DownloadUrl(url, CP_UTF8);
|
2011-06-19 14:58:48 +00:00
|
|
|
if (!data.empty())
|
|
|
|
{
|
|
|
|
// First we get the URL to the actual wiki page
|
|
|
|
std::wstring::size_type pos = data.find(L"http://");
|
|
|
|
if (pos != std::wstring::npos)
|
|
|
|
{
|
|
|
|
data.erase(0, pos);
|
2011-07-14 10:12:02 +00:00
|
|
|
pos = data.find_first_of(L'\'');
|
|
|
|
url.assign(data, 0, pos);
|
2011-06-19 14:58:48 +00:00
|
|
|
|
|
|
|
// Fetch the wiki page
|
2013-05-31 14:18:52 +00:00
|
|
|
data = Internet::DownloadUrl(url, CP_UTF8);
|
2011-06-19 14:58:48 +00:00
|
|
|
if (!data.empty())
|
|
|
|
{
|
|
|
|
pos = data.find(L"'lyricbox'");
|
|
|
|
pos = data.find(L"&#", pos);
|
|
|
|
if (pos != std::wstring::npos)
|
|
|
|
{
|
|
|
|
// Get and decode lyrics
|
|
|
|
data.erase(0, pos);
|
|
|
|
pos = data.find(L"<!");
|
|
|
|
data.resize(pos);
|
2013-05-31 14:18:52 +00:00
|
|
|
Internet::DecodeReferences(data);
|
2011-06-19 14:58:48 +00:00
|
|
|
|
|
|
|
pos = data.find(L"[...]");
|
|
|
|
if (pos != std::wstring::npos)
|
|
|
|
{
|
|
|
|
// Skip incomplete lyrics
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = data.find(L"<p>");
|
|
|
|
if (pos != std::wstring::npos)
|
|
|
|
{
|
|
|
|
// Skip unavailable lyrics
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((pos = data.find(L"<br />"), pos) != std::wstring::npos)
|
|
|
|
{
|
|
|
|
data.replace(pos, 6, L"\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get rid of all HTML tags
|
|
|
|
std::wstring::size_type len = 0;
|
|
|
|
while ((pos = data.find_first_of(L'<'), pos) != std::wstring::npos)
|
|
|
|
{
|
|
|
|
len = data.find_first_of(L'>', pos);
|
|
|
|
len -= pos;
|
|
|
|
data.erase(pos, ++len);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Download lyrics from LYRDB.
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
bool Lyrics::GetFromLYRDB(const std::wstring& artist, const std::wstring& title, std::wstring& data)
|
2011-06-19 14:58:48 +00:00
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
|
2011-09-08 12:42:03 +00:00
|
|
|
std::wstring query = artist + L"|";
|
2011-06-19 14:58:48 +00:00
|
|
|
query += title;
|
|
|
|
|
|
|
|
// LYRDB doesn't like apostrophes
|
|
|
|
std::wstring::size_type pos = 0;
|
|
|
|
while ((pos = query.find(L"%27", pos)) != std::wstring::npos)
|
|
|
|
{
|
|
|
|
query.erase(pos, 3);
|
|
|
|
}
|
|
|
|
|
2011-09-08 12:42:03 +00:00
|
|
|
std::wstring url = L"http://webservices.lyrdb.com/lookup.php?q=" + query;
|
2011-06-19 14:58:48 +00:00
|
|
|
url += L"&for=match&agent=RainmeterNowPlaying";
|
|
|
|
|
2013-05-31 14:18:52 +00:00
|
|
|
data = Internet::DownloadUrl(url, CP_ACP);
|
2011-06-19 14:58:48 +00:00
|
|
|
if (!data.empty())
|
|
|
|
{
|
|
|
|
pos = data.find(L"\\");
|
|
|
|
if (pos != std::wstring::npos)
|
|
|
|
{
|
|
|
|
// Grab the first match
|
2011-07-14 10:12:02 +00:00
|
|
|
url.assign(data, 0, pos);
|
|
|
|
url.insert(0, L"http://webservices.lyrdb.com/getlyr.php?q=");
|
2011-06-19 14:58:48 +00:00
|
|
|
|
2013-05-31 14:18:52 +00:00
|
|
|
data = Internet::DownloadUrl(url, CP_ACP);
|
2011-06-19 14:58:48 +00:00
|
|
|
if (!data.empty())
|
|
|
|
{
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Download lyrics from Letras.
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
bool Lyrics::GetFromLetras(const std::wstring& artist, const std::wstring& title, std::wstring& data)
|
2011-06-19 14:58:48 +00:00
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
|
2011-09-08 12:42:03 +00:00
|
|
|
std::wstring url = L"http://letras.terra.com.br/winamp.php?musica=" + title;
|
2011-06-19 14:58:48 +00:00
|
|
|
url += L"&artista=";
|
|
|
|
url += artist;
|
2013-05-31 14:18:52 +00:00
|
|
|
data = Internet::DownloadUrl(url, CP_ACP);
|
2011-06-19 14:58:48 +00:00
|
|
|
if (!data.empty())
|
|
|
|
{
|
|
|
|
std::wstring::size_type pos = data.find(L"\"letra\"");
|
|
|
|
pos = data.find(L"<p>", pos);
|
|
|
|
if (pos != std::wstring::npos)
|
|
|
|
{
|
|
|
|
pos += 3;
|
|
|
|
data.erase(0, pos);
|
|
|
|
|
|
|
|
pos = data.find(L"</p>");
|
|
|
|
data.resize(pos);
|
|
|
|
|
2013-05-31 14:18:52 +00:00
|
|
|
Internet::DecodeReferences(data);
|
2011-06-19 14:58:48 +00:00
|
|
|
|
|
|
|
while ((pos = data.find(L"<br/>"), pos) != std::wstring::npos)
|
|
|
|
{
|
|
|
|
data.erase(pos, 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|