parent
8deaa0a9e3
commit
ea3778da7c
11 changed files with 260 additions and 46 deletions
@ -1,11 +1,11 @@ |
||||
bin_PROGRAMS = ncmpcpp
|
||||
ncmpcpp_SOURCES = color_parser.cpp helpers.cpp menu.cpp misc.cpp ncmpcpp.cpp \
|
||||
scrollpad.cpp settings.cpp song.cpp status_checker.cpp window.cpp
|
||||
ncmpcpp_SOURCES = color_parser.cpp helpers.cpp lyrics.cpp menu.cpp misc.cpp \
|
||||
ncmpcpp.cpp scrollpad.cpp settings.cpp song.cpp status_checker.cpp window.cpp
|
||||
|
||||
# set the include path found by configure
|
||||
INCLUDES= $(all_includes)
|
||||
|
||||
# the library search path.
|
||||
ncmpcpp_LDFLAGS = $(all_libraries)
|
||||
noinst_HEADERS = helpers.h menu.h scrollpad.h settings.h song.h \
|
||||
noinst_HEADERS = helpers.h lyrics.h menu.h scrollpad.h settings.h song.h \
|
||||
status_checker.h window.h
|
||||
|
||||
@ -0,0 +1,107 @@ |
||||
/***************************************************************************
|
||||
* Copyright (C) 2008 by Andrzej Rybczak * |
||||
* electricityispower@gmail.com * |
||||
* * |
||||
* 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 Foundation, Inc., * |
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
||||
***************************************************************************/ |
||||
|
||||
#include "lyrics.h" |
||||
|
||||
const string lyrics_folder = home_folder + "/" + ".lyrics"; |
||||
const string mkdir_command = "mkdir " + lyrics_folder + " &>/dev/null"; |
||||
|
||||
size_t write_data(char *buffer, size_t size, size_t nmemb, string data) |
||||
{ |
||||
int result = 0; |
||||
if (buffer) |
||||
{ |
||||
data += buffer; |
||||
result = size*nmemb; |
||||
} |
||||
return result; |
||||
} |
||||
|
||||
string GetLyrics(string artist, string song) |
||||
{ |
||||
const string filename = artist + " - " + song + ".txt"; |
||||
const string fullpath = lyrics_folder + "/" + filename; |
||||
system(mkdir_command.c_str()); |
||||
|
||||
string result; |
||||
std::ifstream input(fullpath.c_str()); |
||||
|
||||
if (input.is_open()) |
||||
{ |
||||
string line; |
||||
while (getline(input, line)) |
||||
result += line + "\n"; |
||||
return result; |
||||
} |
||||
|
||||
# ifdef HAVE_CURL_CURL_H |
||||
for (string::iterator it = artist.begin(); it != artist.end(); it++) |
||||
if (*it == ' ') |
||||
*it = '+'; |
||||
|
||||
for (string::iterator it = song.begin(); it != song.end(); it++) |
||||
if (*it == ' ') |
||||
*it = '+'; |
||||
|
||||
CURLcode code; |
||||
|
||||
string url = "http://lyricwiki.org/api.php?artist=" + artist + "&song=" + song + "&fmt=xml"; |
||||
|
||||
CURL *lyrics = curl_easy_init(); |
||||
curl_easy_setopt(lyrics, CURLOPT_URL, url.c_str()); |
||||
curl_easy_setopt(lyrics, CURLOPT_WRITEFUNCTION, write_data); |
||||
curl_easy_setopt(lyrics, CURLOPT_WRITEDATA, &result); |
||||
curl_easy_setopt(lyrics, CURLOPT_CONNECTTIMEOUT, 30); |
||||
code = curl_easy_perform(lyrics); |
||||
curl_easy_cleanup(lyrics); |
||||
|
||||
if (code != CURLE_OK) |
||||
{ |
||||
result = "Error while fetching lyrics: " + string(curl_easy_strerror(code)); |
||||
return result; |
||||
} |
||||
|
||||
int a, b; |
||||
a = result.find("<lyrics>")+8; |
||||
b = result.find("</lyrics>"); |
||||
|
||||
result = result.substr(a, b-a); |
||||
|
||||
if (result == "Not found") |
||||
return result; |
||||
|
||||
for (int i = result.find("'"); i != string::npos; i = result.find("'")) |
||||
result.replace(i, 6, "'"); |
||||
for (int i = result.find("""); i != string::npos; i = result.find(""")) |
||||
result.replace(i, 6, "\""); |
||||
|
||||
std::ofstream output(fullpath.c_str()); |
||||
|
||||
if (output.is_open()) |
||||
{ |
||||
output << result; |
||||
output.close(); |
||||
} |
||||
# else |
||||
result = "Local lyrics not found. As ncmpcpp has been compiled without curl support, you can put appropriate lyrics into ~/.lyrics directory (file syntax is \"ARTIST - TITLE.txt\") or recompile ncmpcpp with curl support."; |
||||
# endif |
||||
return result + '\n'; |
||||
} |
||||
|
||||
@ -0,0 +1,34 @@ |
||||
/***************************************************************************
|
||||
* Copyright (C) 2008 by Andrzej Rybczak * |
||||
* electricityispower@gmail.com * |
||||
* * |
||||
* 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 Foundation, Inc., * |
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
||||
***************************************************************************/ |
||||
|
||||
#ifndef HAVE_LYRICS_H |
||||
#define HAVE_LYRICS_H |
||||
|
||||
#include "ncmpcpp.h" |
||||
|
||||
#include <fstream> |
||||
#ifdef HAVE_CURL_CURL_H |
||||
# include <curl/curl.h> |
||||
#endif |
||||
|
||||
string GetLyrics(string, string); |
||||
|
||||
#endif |
||||
|
||||
Loading…
Reference in new issue