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

Last change on this file since 188 was 188, checked in by Silvan Scherrer, 8 years ago

SMPlayer: update trunk to version 17.1.0

File size: 6.2 KB
Line 
1/* smplayer, GUI front-end for mplayer.
2 Copyright (C) 2006-2017 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 , search_retries(8)
29#endif
30{
31 rpc = new MaiaXmlRpcClient(QUrl("http://api.opensubtitles.org/xml-rpc"), this);
32}
33
34void OSClient::setServer(const QString & server) {
35 rpc->setUrl(QUrl(server));
36}
37
38#ifdef FS_USE_PROXY
39void OSClient::setProxy(const QNetworkProxy & proxy) {
40 rpc->setProxy(proxy);
41}
42#endif
43
44void OSClient::login() {
45 qDebug("OSClient::login");
46
47 QString user_agent = "SMPlayer v" + Version::stable();
48 qDebug("OSClient::login: user agent: %s", user_agent.toUtf8().constData());
49
50 QVariantList args;
51
52 args << "" << "" << "" << user_agent;
53
54 rpc->call("LogIn", args,
55 this, SLOT(responseLogin(QVariant &)),
56 this, SLOT(gotFault(int, const QString &)));
57}
58
59void OSClient::search(const QString & hash, qint64 file_size) {
60 qDebug() << "OSClient::search: hash: " << hash << "file_size: " << file_size;
61
62 search_hash = hash;
63 search_size = file_size;
64
65 disconnect(this, SIGNAL(loggedIn()), this, SLOT(doSearch()));
66
67 #if 0
68 if (logged_in) {
69 doSearch();
70 } else {
71 connect(this, SIGNAL(loggedIn()), this, SLOT(doSearch()));
72 login();
73 }
74 #else
75 connect(this, SIGNAL(loggedIn()), this, SLOT(doSearch()));
76 login();
77 #endif
78}
79
80#ifdef OS_SEARCH_WORKAROUND
81void OSClient::doSearch() {
82 best_search_count = -1;
83 for (int n = 1; n <= search_retries; n++) doSearch(n);
84}
85
86void OSClient::doSearch(int nqueries) {
87#else
88void OSClient::doSearch() {
89#endif
90 qDebug("OSClient::doSearch");
91
92 QVariantMap m;
93 m["sublanguageid"] = "all";
94 m["moviehash"] = search_hash;
95 m["moviebytesize"] = QString::number(search_size);
96
97 QVariantList list;
98#ifdef OS_SEARCH_WORKAROUND
99 // Sometimes opensubtitles return 0 subtitles
100 // A workaround seems to add the query several times
101 qDebug("OSClient::doSearch: nqueries: %d", nqueries);
102 for (int count = 0; count < nqueries; count++) list.append(m);
103 //qDebug("OSClient::doSearch: list count: %d", list.count());
104#else
105 list.append(m);
106#endif
107
108 QVariantList args;
109 args << token << QVariant(list);
110
111 /*
112 for (int n=0; n < args.count(); n++) {
113 qDebug("%d = %d (%s)", n, args[n].type(), args[n].typeName());
114 }
115 */
116
117 rpc->call("SearchSubtitles", args,
118 this, SLOT(responseSearch(QVariant &)),
119 this, SLOT(gotFault(int, const QString &)));
120}
121
122void OSClient::responseLogin(QVariant &arg) {
123 qDebug("OSClient::responseLogin");
124
125 QVariantMap m = arg.toMap();
126 QString status = m["status"].toString();
127 QString t = m["token"].toString();
128
129 qDebug("OSClient::responseLogin: status: %s", status.toLatin1().constData());
130 qDebug("OSClient::responseLogin: token: %s", t.toLatin1().constData());
131
132 if (status == "200 OK") {
133 token = t;
134 logged_in = true;
135 emit loggedIn();
136 } else {
137 emit loginFailed();
138 }
139}
140
141void OSClient::responseSearch(QVariant &arg) {
142 qDebug("OSClient::responseSearch");
143
144 QVariantMap m = arg.toMap();
145 QString status = m["status"].toString();
146
147 qDebug("OSClient::responseSearch: status: %s", status.toLatin1().constData());
148 //qDebug("count: %d", m.count());
149
150 /*
151 QMapIterator<QString, QVariant> i(m);
152 while (i.hasNext()) {
153 i.next();
154 qDebug("key: %s", i.key().toLatin1().constData());
155 }
156 */
157
158 if (status != "200 OK") {
159 emit searchFailed();
160 return;
161 }
162
163 s_list.clear();
164
165 QVariantList data = m["data"].toList();
166 qDebug("OSClient::responseSearch: data count: %d", data.count());
167
168#ifdef OS_SEARCH_WORKAROUND
169 if (best_search_count >= data.count()) {
170 qDebug("OSClient::responseSearch: we already have a better search (%d). Ignoring this one.", best_search_count);
171 return;
172 }
173 best_search_count = data.count();
174#endif
175
176 for (int n = 0; n < data.count(); n++) {
177 OSSubtitle sub;
178
179 //qDebug("%d: type: %d (%s)", n, data[n].type(), data[n].typeName());
180 QVariantMap m = data[n].toMap();
181
182 sub.releasename = m["MovieReleaseName"].toString();
183 sub.movie = m["MovieName"].toString();
184#ifdef USE_QUAZIP
185 sub.link = m["ZipDownloadLink"].toString();
186#else
187 sub.link = m["SubDownloadLink"].toString();
188#endif
189 sub.date = m["SubAddDate"].toString();
190 sub.iso639 = m["ISO639"].toString();
191 sub.rating = m["SubRating"].toString();
192 sub.comments = m["SubAuthorComment"].toString();
193 sub.format = m["SubFormat"].toString();
194 sub.language = m["LanguageName"].toString();
195 sub.user = m["UserNickName"].toString();
196 sub.files = "1";
197
198 s_list.append(sub);
199
200 /*
201 qDebug("MovieName: %s", sub.movie.toLatin1().constData());
202 qDebug("MovieReleaseName: %s", sub.releasename.toLatin1().constData());
203 //qDebug("SubFileName: %s", m["SubFileName"].toString().toLatin1().constData());
204 //qDebug("SubDownloadLink: %s", m["SubDownloadLink"].toString().toLatin1().constData());
205 qDebug("ZipDownloadLink: %s", sub.link.toLatin1().constData());
206 qDebug("SubAddDate: %s", sub.date.toLatin1().constData());
207 qDebug("ISO639: %s", sub.iso639.toLatin1().constData());
208 qDebug("SubRating: %s", sub.rating.toLatin1().constData());
209 qDebug("SubAuthorComment: %s", sub.comments.toLatin1().constData());
210 qDebug("SubFormat: %s", sub.format.toLatin1().constData());
211 qDebug("LanguageName: %s", sub.language.toLatin1().constData());
212 qDebug("UserNickName: %s", sub.user.toLatin1().constData());
213 qDebug("=======");
214 */
215 }
216
217 emit searchFinished();
218}
219
220void OSClient::gotFault(int error, const QString &message) {
221 qDebug("OSClient::gotFault: error: %d, message: %s", error, message.toUtf8().constData());
222 emit errorFound(error, message);
223}
224
225#include "moc_osclient.cpp"
Note: See TracBrowser for help on using the repository browser.