| 1 | /***************************************************************************** |
| 2 | ** QNapi |
| 3 | ** Copyright (C) 2008-2017 Piotr KrzemiĆski <[email protected]> |
| 4 | ** |
| 5 | ** This program is free software; you can redistribute it and/or modify |
| 6 | ** it under the terms of the GNU General Public License as published by |
| 7 | ** the Free Software Foundation; either version 2 of the License, or |
| 8 | ** (at your option) any later version. |
| 9 | ** |
| 10 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
| 11 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | ** |
| 13 | *****************************************************************************/ |
| 14 | |
| 15 | #include "opensubtitlesdownloadengine.h" |
| 16 | #include "subtitlelanguage.h" |
| 17 | |
| 18 | #include <QDir> |
| 19 | |
| 20 | namespace OpenSubtitlesDownloadEngineConst { |
| 21 | const QString openSubtitlesXmlRpcUrl = "http://api.opensubtitles.org/xml-rpc"; |
| 22 | } |
| 23 | |
| 24 | SubtitleDownloadEngineMetadata OpenSubtitlesDownloadEngine::metadata = |
| 25 | SubtitleDownloadEngineMetadata( |
| 26 | "OpenSubtitles", |
| 27 | QObject::tr("<b>www.opensubtitles.org</b> subtitles download engine"), |
| 28 | just(QUrl("http://www.opensubtitles.org/newuser")), |
| 29 | just(QUrl("http://www.opensubtitles.org/upload"))); |
| 30 | |
| 31 | OpenSubtitlesDownloadEngine::OpenSubtitlesDownloadEngine( |
| 32 | const QString& tmpPath, const EngineConfig& config, |
| 33 | const QSharedPointer<const P7ZipDecoder>& p7zipDecoder, |
| 34 | const QString& qnapiDisplayableVersion, const QString& language) |
| 35 | : SubtitleDownloadEngine(tmpPath), |
| 36 | engineConfig(config), |
| 37 | p7zipDecoder(p7zipDecoder), |
| 38 | qnapiDisplayableVersion(qnapiDisplayableVersion), |
| 39 | language(language), |
| 40 | rpc(QUrl(OpenSubtitlesDownloadEngineConst::openSubtitlesXmlRpcUrl)) {} |
| 41 | |
| 42 | OpenSubtitlesDownloadEngine::~OpenSubtitlesDownloadEngine() { |
| 43 | cleanup(); |
| 44 | if (isLogged()) logout(); |
| 45 | } |
| 46 | |
| 47 | SubtitleDownloadEngineMetadata OpenSubtitlesDownloadEngine::meta() const { |
| 48 | return OpenSubtitlesDownloadEngine::metadata; |
| 49 | } |
| 50 | |
| 51 | const char* const* OpenSubtitlesDownloadEngine::enginePixmapData() const { |
| 52 | return OpenSubtitlesDownloadEngine::pixmapData; |
| 53 | } |
| 54 | |
| 55 | // oblicza sume kontrolna dla pliku filmowego |
| 56 | QString OpenSubtitlesDownloadEngine::checksum(QString filename) { |
| 57 | if (filename.isEmpty()) filename = movie; |
| 58 | |
| 59 | QFile file(filename); |
| 60 | if (!file.open(QIODevice::ReadOnly)) return QString(""); |
| 61 | |
| 62 | fileSize = file.size(); |
| 63 | quint64 hash = fileSize; |
| 64 | quint64 tmp, i; |
| 65 | |
| 66 | for (tmp = 0, i = 0; |
| 67 | i < 65536 / sizeof(tmp) && file.read((char*)&tmp, sizeof(tmp)); |
| 68 | i++, hash += tmp) |
| 69 | ; |
| 70 | file.seek(qMax(0, (int)((qint64)fileSize - 65536))); |
| 71 | for (tmp = 0, i = 0; |
| 72 | i < 65536 / sizeof(tmp) && file.read((char*)&tmp, sizeof(tmp)); |
| 73 | i++, hash += tmp) |
| 74 | ; |
| 75 | |
| 76 | return (checkSum = QString("%1").arg(hash, 16, 16, QChar('0'))); |
| 77 | } |
| 78 | |
| 79 | // szuka napisow |
| 80 | bool OpenSubtitlesDownloadEngine::lookForSubtitles(QString lang) { |
| 81 | if (checkSum.isEmpty()) return false; |
| 82 | if (!isLogged() && !login()) return false; |
| 83 | |
| 84 | QVariantMap paramsMap; |
| 85 | paramsMap["sublanguageid"] = SubtitleLanguage(lang).toTriLetter(); |
| 86 | paramsMap["moviehash"] = checkSum; |
| 87 | paramsMap["moviebytesize"] = (int)fileSize; |
| 88 | |
| 89 | QVariantList requestList; |
| 90 | requestList << paramsMap; |
| 91 | |
| 92 | QVariantList args; |
| 93 | args << token << (QVariant)requestList; |
| 94 | |
| 95 | if (!rpc.request("SearchSubtitles", args)) return false; |
| 96 | |
| 97 | QVariantMap responseMap = rpc.getResponse().toMap(); |
| 98 | |
| 99 | if (!responseMap.contains("data")) return false; |
| 100 | |
| 101 | QVariantList dataList = responseMap["data"].toList(); |
| 102 | QVariantList::iterator i; |
| 103 | |
| 104 | i = dataList.begin(); |
| 105 | while (i != dataList.end()) { |
| 106 | responseMap = (*(i++)).toMap(); |
| 107 | |
| 108 | if ((checkSum != responseMap["MovieHash"]) && |
| 109 | (fileSize != responseMap["MovieByteSize"])) |
| 110 | continue; |
| 111 | |
| 112 | SubtitleResolution r = SUBTITLE_UNKNOWN; |
| 113 | |
| 114 | if (responseMap["SubBad"].toString() != "0") { |
| 115 | r = SUBTITLE_BAD; |
| 116 | } else if (QFileInfo(movie).completeBaseName() == |
| 117 | QFileInfo(responseMap["SubFileName"].toString()) |
| 118 | .completeBaseName()) { |
| 119 | r = SUBTITLE_GOOD; |
| 120 | } else if (QRegExp(QString("^%1").arg( |
| 121 | responseMap["MovieReleaseName"].toString()), |
| 122 | Qt::CaseInsensitive) |
| 123 | .exactMatch(QFileInfo(movie).completeBaseName())) { |
| 124 | r = SUBTITLE_GOOD; |
| 125 | } |
| 126 | |
| 127 | QString subtitleName = responseMap["MovieReleaseName"].toString(); |
| 128 | if (subtitleName.isEmpty()) |
| 129 | subtitleName = QFileInfo(movie).completeBaseName(); |
| 130 | |
| 131 | subtitlesList << SubtitleInfo( |
| 132 | responseMap["ISO639"].toString(), meta().name(), |
| 133 | responseMap["IDSubtitleFile"].toString(), subtitleName.trimmed(), |
| 134 | responseMap["SubAuthorComment"].toString(), |
| 135 | QFileInfo(responseMap["SubFileName"].toString()).suffix(), r); |
| 136 | } |
| 137 | |
| 138 | return (subtitlesList.size() > 0); |
| 139 | } |
| 140 | |
| 141 | QList<SubtitleInfo> OpenSubtitlesDownloadEngine::listSubtitles() { |
| 142 | std::sort(subtitlesList.begin(), subtitlesList.end()); |
| 143 | return subtitlesList; |
| 144 | } |
| 145 | |
| 146 | bool OpenSubtitlesDownloadEngine::download(QUuid id) { |
| 147 | Maybe<SubtitleInfo> ms = resolveById(id); |
| 148 | |
| 149 | if (!ms) return false; |
| 150 | |
| 151 | SubtitleInfo s = ms.value(); |
| 152 | |
| 153 | QVariantList paramsList; |
| 154 | QVariantList requestList; |
| 155 | |
| 156 | subFileName = generateTmpFileName() + "." + s.format; |
| 157 | paramsList << s.sourceLocation; |
| 158 | requestList << (QVariant)paramsList; |
| 159 | |
| 160 | QVariantList args; |
| 161 | args << token << requestList; |
| 162 | |
| 163 | if (!rpc.request("DownloadSubtitles", args)) return false; |
| 164 | |
| 165 | QVariantMap responseMap = rpc.getResponse().toMap(); |
| 166 | |
| 167 | if (!responseMap.contains("data")) return false; |
| 168 | |
| 169 | QVariantList responseList = responseMap["data"].toList(); |
| 170 | |
| 171 | if (responseList.size() == 0) return false; |
| 172 | |
| 173 | QByteArray subtitleContent = |
| 174 | QByteArray::fromBase64(responseList.at(0).toMap()["data"].toByteArray()); |
| 175 | |
| 176 | s.sourceLocation = generateTmpPath(); |
| 177 | QFile file(s.sourceLocation); |
| 178 | if (file.exists()) file.remove(); |
| 179 | if (!file.open(QIODevice::WriteOnly)) return false; |
| 180 | |
| 181 | int r = file.write(subtitleContent); |
| 182 | file.close(); |
| 183 | |
| 184 | updateSubtitleInfo(s); |
| 185 | |
| 186 | return (bool)r; |
| 187 | } |
| 188 | |
| 189 | bool OpenSubtitlesDownloadEngine::unpack(QUuid id) { |
| 190 | Maybe<SubtitleInfo> ms = resolveById(id); |
| 191 | if (!ms) return false; |
| 192 | |
| 193 | if (!QFile::exists(movie)) return false; |
| 194 | |
| 195 | QString archivePath = ms.value().sourceLocation; |
| 196 | |
| 197 | if (!p7zipDecoder->unpackArchiveFiles(archivePath, tmpPath)) return false; |
| 198 | |
| 199 | QString unpackedTmp = |
| 200 | tmpPath + QDir::separator() + QFileInfo(archivePath).completeBaseName(); |
| 201 | subtitlesTmp = tmpPath + QDir::separator() + subFileName; |
| 202 | |
| 203 | QFile::copy(unpackedTmp, subtitlesTmp); |
| 204 | QFile::remove(unpackedTmp); |
| 205 | |
| 206 | return true; |
| 207 | } |
| 208 | |
| 209 | void OpenSubtitlesDownloadEngine::cleanup() { |
| 210 | clearSubtitlesList(); |
| 211 | if (QFile::exists(subtitlesTmp)) QFile::remove(subtitlesTmp); |
| 212 | } |
| 213 | |
| 214 | bool OpenSubtitlesDownloadEngine::login() { |
| 215 | QString userAgent = QString("QNapi v%1").arg(qnapiDisplayableVersion); |
| 216 | QVariantList args; |
| 217 | args << engineConfig.nick() << engineConfig.password() << language.toLower() |
| 218 | << userAgent; |
| 219 | if (!rpc.request("LogIn", args)) return false; |
| 220 | token = (rpc.getResponse().toMap())["token"].toString(); |
| 221 | |
| 222 | return !token.isEmpty(); |
| 223 | } |
| 224 | |
| 225 | void OpenSubtitlesDownloadEngine::logout() { |
| 226 | QVariantList args; |
| 227 | args << token; |
| 228 | rpc.request("LogOut", args); |
| 229 | token = ""; |
| 230 | } |
| 231 | |
| 232 | const char* const OpenSubtitlesDownloadEngine::pixmapData[] = { |
| 233 | "16 16 14 1", ". c #000000", "h c #111111", |
| 234 | "c c #222222", "j c #333333", "g c #444444", |
| 235 | "l c #555555", "e c #777777", "k c #888888", |
| 236 | "a c #999999", "b c #aaaaaa", "f c #cccccc", |
| 237 | "i c #dddddd", "d c #eeeeee", "# c #ffffff", |
| 238 | "................", ".##.##.##.##.##.", ".##.##.##.##.##.", |
| 239 | "................", "................", "...a##b..cd##d..", |
| 240 | "..ea..be.fg.hi..", "..ic..hi.ig.....", "..dh..hd.g##ij..", |
| 241 | "..ea..ak.k..gi..", "...b##b..fd##l..", "................", |
| 242 | "................", ".##.##.##.##.##.", ".##.##.##.##.##.", |
| 243 | "................"}; |