source: smplayer/trunk/src/findsubtitles/osclient.cpp@ 170

Last change on this file since 170 was 170, checked in by Silvan Scherrer, 11 years ago

SMPlayer: updated trunk to 14.9.0

File size: 6.1 KB
Line 
1/* smplayer, GUI front-end for mplayer.
2 Copyright (C) 2006-2014 Ricardo Villalba <rvm@users.sourceforge.net>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17*/
18
19#include "osclient.h"
20#include "version.h"
21
22OSClient::OSClient(QObject* parent) :
23 QObject(parent)
24 , logged_in(false)
25 , search_size(0)
26#ifdef OS_SEARCH_WORKAROUND
27 , best_search_count(0)
28#endif
29{
30 rpc = new MaiaXmlRpcClient(QUrl("http://api.opensubtitles.org/xml-rpc"), this);
31}
32
33void OSClient::setServer(const QString & server) {
34 rpc->setUrl(QUrl(server));
35}
36
37void OSClient::setProxy(const QNetworkProxy & proxy) {
38 rpc->setProxy(proxy);
39}
40
41void OSClient::login() {
42 qDebug("OSClient::login");
43
44 QString user_agent = "SMPlayer v" + Version::stable();
45 qDebug("OSClient::login: user agent: %s", user_agent.toUtf8().constData());
46
47 QVariantList args;
48
49 args << "" << "" << "" << user_agent;
50
51 rpc->call("LogIn", args,
52 this, SLOT(responseLogin(QVariant &)),
53 this, SLOT(gotFault(int, const QString &)));
54}
55
56void OSClient::search(const QString & hash, qint64 file_size) {
57 qDebug() << "OSClient::search: hash: " << hash << "file_size: " << file_size;
58
59 search_hash = hash;
60 search_size = file_size;
61
62 #if 0
63 if (logged_in) {
64 doSearch();
65 } else {
66 connect(this, SIGNAL(loggedIn()), this, SLOT(doSearch()));
67 login();
68 }
69 #else
70 connect(this, SIGNAL(loggedIn()), this, SLOT(doSearch()));
71 login();
72 #endif
73}
74
75#ifdef OS_SEARCH_WORKAROUND
76void OSClient::doSearch() {
77 best_search_count = -1;
78 for (int n = 1; n < 8; n++) doSearch(n);
79}
80
81void OSClient::doSearch(int nqueries) {
82#else
83void OSClient::doSearch() {
84#endif
85 qDebug("OSClient::doSearch");
86
87 QVariantMap m;
88 m["sublanguageid"] = "all";
89 m["moviehash"] = search_hash;
90 m["moviebytesize"] = QString::number(search_size);
91
92 QVariantList list;
93#ifdef OS_SEARCH_WORKAROUND
94 // Sometimes opensubtitles return 0 subtitles
95 // A workaround seems to add the query several times
96 qDebug("OSClient::doSearch: nqueries: %d", nqueries);
97 for (int count = 0; count < nqueries; count++) list.append(m);
98 //qDebug("OSClient::doSearch: list count: %d", list.count());
99#else
100 list.append(m);
101#endif
102
103 QVariantList args;
104 args << token << QVariant(list);
105
106 /*
107 for (int n=0; n < args.count(); n++) {
108 qDebug("%d = %d (%s)", n, args[n].type(), args[n].typeName());
109 }
110 */
111
112 rpc->call("SearchSubtitles", args,
113 this, SLOT(responseSearch(QVariant &)),
114 this, SLOT(gotFault(int, const QString &)));
115}
116
117void OSClient::responseLogin(QVariant &arg) {
118 qDebug("OSClient::responseLogin");
119
120 QVariantMap m = arg.toMap();
121 QString status = m["status"].toString();
122 QString t = m["token"].toString();
123
124 qDebug("OSClient::responseLogin: status: %s", status.toLatin1().constData());
125 qDebug("OSClient::responseLogin: token: %s", t.toLatin1().constData());
126
127 if (status == "200 OK") {
128 token = t;
129 logged_in = true;
130 emit loggedIn();
131 } else {
132 emit loginFailed();
133 }
134}
135
136void OSClient::responseSearch(QVariant &arg) {
137 qDebug("OSClient::responseSearch");
138
139 QVariantMap m = arg.toMap();
140 QString status = m["status"].toString();
141
142 qDebug("OSClient::responseSearch: status: %s", status.toLatin1().constData());
143 //qDebug("count: %d", m.count());
144
145 /*
146 QMapIterator<QString, QVariant> i(m);
147 while (i.hasNext()) {
148 i.next();
149 qDebug("key: %s", i.key().toLatin1().constData());
150 }
151 */
152
153 if (status != "200 OK") {
154 emit searchFailed();
155 return;
156 }
157
158 s_list.clear();
159
160 QVariantList data = m["data"].toList();
161 qDebug("OSClient::responseSearch: data count: %d", data.count());
162
163#ifdef OS_SEARCH_WORKAROUND
164 if (best_search_count >= data.count()) {
165 qDebug("OSClient::responseSearch: we already have a better search (%d). Ignoring this one.", best_search_count);
166 return;
167 }
168 best_search_count = data.count();
169#endif
170
171 for (int n = 0; n < data.count(); n++) {
172 OSSubtitle sub;
173
174 //qDebug("%d: type: %d (%s)", n, data[n].type(), data[n].typeName());
175 QVariantMap m = data[n].toMap();
176
177 sub.releasename = m["MovieReleaseName"].toString();
178 sub.movie = m["MovieName"].toString();
179#ifdef USE_QUAZIP
180 sub.link = m["ZipDownloadLink"].toString();
181#else
182 sub.link = m["SubDownloadLink"].toString();
183#endif
184 sub.date = m["SubAddDate"].toString();
185 sub.iso639 = m["ISO639"].toString();
186 sub.rating = m["SubRating"].toString();
187 sub.comments = m["SubAuthorComment"].toString();
188 sub.format = m["SubFormat"].toString();
189 sub.language = m["LanguageName"].toString();
190 sub.user = m["UserNickName"].toString();
191 sub.files = "1";
192
193 s_list.append(sub);
194
195 /*
196 qDebug("MovieName: %s", sub.movie.toLatin1().constData());
197 qDebug("MovieReleaseName: %s", sub.releasename.toLatin1().constData());
198 //qDebug("SubFileName: %s", m["SubFileName"].toString().toLatin1().constData());
199 //qDebug("SubDownloadLink: %s", m["SubDownloadLink"].toString().toLatin1().constData());
200 qDebug("ZipDownloadLink: %s", sub.link.toLatin1().constData());
201 qDebug("SubAddDate: %s", sub.date.toLatin1().constData());
202 qDebug("ISO639: %s", sub.iso639.toLatin1().constData());
203 qDebug("SubRating: %s", sub.rating.toLatin1().constData());
204 qDebug("SubAuthorComment: %s", sub.comments.toLatin1().constData());
205 qDebug("SubFormat: %s", sub.format.toLatin1().constData());
206 qDebug("LanguageName: %s", sub.language.toLatin1().constData());
207 qDebug("UserNickName: %s", sub.user.toLatin1().constData());
208 qDebug("=======");
209 */
210 }
211
212 emit searchFinished();
213}
214
215void OSClient::gotFault(int error, const QString &message) {
216 qDebug("OSClient::gotFault: error: %d, message: %s", error, message.toUtf8().constData());
217 emit errorFound(error, message);
218}
219
220#include "moc_osclient.cpp"
Note: See TracBrowser for help on using the repository browser.