Created LyricWikiDownloader, make AZLyricsDownloader's output plain text
[maelyrica:maelyrica.git] / lyricsDownloader.cpp
1 /* File: lyricsDownloader.cpp
2  *
3  * This file is part of MaeLyrica.
4  *
5  * Copyright (C) 2012 Marcin Mielniczuk.
6  *
7  * MaeLyrica is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation, version 3 of the License.
10  *
11  * MaeLyrica is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  *  along with MaeLyrica.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "lyricsDownloader.h"
21 #include "AZLyricsDownloader.h"
22 #include <curl/curl.h>
23 #include <QDebug>
24 #include <iostream>
25 #include <QFile>
26 #include <QSettings>
27 #include <QtNetwork/QtNetwork>
28
29 using namespace std;
30
31 /////////////////////////////////////////////////////////////////////////////
32
33 short lyricsDownloader::perform(const string& a, const string& t)
34 {
35     CURLcode dlret; // return value
36     dlret = download(a, t); // it doesn't fail yet to recognize
37
38     if (dlret == -2) return -1; // no internet
39     else if (dlret != 0) return 1; // error downloading
40     //else continues!
41
42     if ( parse() == false)
43     {
44         qDebug() << "Error while parsing lyrics\n";
45         return 2;
46     }
47
48     return 0; // ok
49 }
50
51 CURLcode lyricsDownloader::download(const string& a, const string& t)
52 {
53     QNetworkConfigurationManager manager;
54     QNetworkConfiguration cfg = manager.defaultConfiguration();
55     /*QNetworkSession* session = new QNetworkSession(cfg);
56     session->setSessionProperty("ConnectInBackground", true);
57     session->open();*/
58
59     CURL * handle;
60     CURLcode err;
61     handle = curl_easy_init();
62     if (! handle) return static_cast<CURLcode>(-1);
63     // set verbose if debug on
64     curl_easy_setopt( handle, CURLOPT_VERBOSE, DEBUG );
65     curl_easy_setopt( handle, CURLOPT_URL, toProviderCode(a, t).c_str() ); // set the download url to the generated one
66     curl_easy_setopt(handle, CURLOPT_WRITEDATA, &buff);
67     curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, &lyricsDownloader::write_data_to_var); // set the function writing to ostringstream
68     curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1); // curl needs to follow redirects with this provider
69     curl_easy_setopt(handle, CURLOPT_FAILONERROR, 1);
70     if (manager.isOnline() == false) return static_cast<CURLcode>(-2);
71     err = curl_easy_perform(handle);
72     curl_easy_cleanup(handle);
73     return err;
74 }
75
76 size_t lyricsDownloader::write_data_to_var(char *ptr, size_t size, size_t nmemb, void *userdata)
77 {
78     ostringstream * stream = (ostringstream*) userdata;
79     size_t count = size * nmemb;
80     stream->write(ptr, count);
81     return count;
82 }
83
84 /////////////////////////////////////////////////////////////////////////
85
86 Settings lyricsDownloaderManager::progSettings;
87 const QString lyricsDownloaderManager::OMPLyricsRoot("/home/user/.lyrics/");
88
89 lyricsDownloaderManager::lyricsDownloaderManager(LyricsDownloaderClass lyricsDownloaderName)
90 {
91     connect(this, SIGNAL(dataChanged()), this, SLOT(cleanTitle()));
92     connect(this, SIGNAL(dataChanged()), this, SLOT(findSavedLyricsKey())); // recalculate the key, so that it's always up-to-date
93
94     currentClass = lyricsDownloaderName;
95     switch (lyricsDownloaderName)
96     {
97     case AZLyricsDownloaderClass:
98         downloader = new AZLyricsDownloader;
99         break;
100     }
101 }
102
103 lyricsDownloaderManager::~lyricsDownloaderManager()
104 {
105     delete downloader;
106 }
107
108 void lyricsDownloaderManager::changeLyricsDownloader(LyricsDownloaderClass lyricsDownloaderName)
109 {
110     if (lyricsDownloaderName != currentClass) // we don't need to do the same again if it's the same downloader
111     {
112         lyricsDownloader * oldDownloader = downloader;
113         switch (lyricsDownloaderName)
114         {
115         case AZLyricsDownloaderClass:
116             downloader = new AZLyricsDownloader;
117             break;
118         }
119         delete oldDownloader;
120     }
121 }
122
123 short lyricsDownloaderManager::perform()
124 {
125     short ret = downloader -> perform(artist, track);
126     lyrics = downloader -> lyrics_qstr; // clean it too!
127     downloader -> lyrics_qstr.clear();
128     emit lyricsChanged(lyrics);
129     return ret;
130 }
131
132 void lyricsDownloaderManager::cleanTitle()
133 {
134     cleanedTitle = cleanQString( QString::fromStdString(artist));
135     cleanedTitle += "-";
136     cleanedTitle += cleanQString(QString::fromStdString(track));
137 }
138
139 QString lyricsDownloaderManager::cleanQString(const QString qstr)
140 {
141     return qstr.toLower().replace("&", "and").remove(QRegExp("\\([^)]*\\)")).remove(QRegExp("[\\W_]"));
142 }
143
144 bool lyricsDownloaderManager::lyricsExistOMP()
145 {
146     lyricsUrlOMP = OMPLyricsRoot + cleanedTitle + ".txt";
147     return QFile(lyricsUrlOMP).exists();
148 }
149
150 void lyricsDownloaderManager::saveLyricsMaeLyrica()
151 {
152     Settings().setValue("lyrics/" + cleanedTitle, lyrics);
153 }
154
155 bool lyricsDownloaderManager::saveLyricsOMP()
156 {
157     qDebug() << "Saving to " << lyricsUrlOMP << "\n";
158     QFile lyricsFile(lyricsUrlOMP);
159     if (!lyricsFile.open(QIODevice::WriteOnly | QIODevice::Text)) return false;
160     QTextStream lyricsFileStream(&lyricsFile);
161     lyricsFileStream << lyrics;
162     lyricsFile.close();
163     if (lyricsFile.error() != QFile::NoError) return false;
164     else return true;
165 }
166