Mercurial > minori
comparison src/gui/pages/torrents.cc @ 183:01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
services/anilist.cc: use constexpr STL string_view for HTTP queries
| author | Paper <mrpapersonic@gmail.com> |
|---|---|
| date | Mon, 04 Dec 2023 13:40:42 -0500 |
| parents | bc8d2ccff09c |
| children | 53211cb1e7f5 |
comparison
equal
deleted
inserted
replaced
| 182:c413e475f496 | 183:01d259b9c89f |
|---|---|
| 52 | 52 |
| 53 QByteArray TorrentsPageListModel::DownloadTorrentList() { | 53 QByteArray TorrentsPageListModel::DownloadTorrentList() { |
| 54 return HTTP::Get(session.config.torrents.feed_link); | 54 return HTTP::Get(session.config.torrents.feed_link); |
| 55 } | 55 } |
| 56 | 56 |
| 57 void TorrentsPageListModel::ParseFeedDescription(const std::string& description, Torrent& torrent) { | |
| 58 /* Parse description... */ | |
| 59 enum class Keys { SIZE, AUTHORIZED, SUBMITTER, COMMENT }; | |
| 60 | |
| 61 const std::unordered_map<std::string, Keys> KeyMap = { | |
| 62 {"Size", Keys::SIZE}, | |
| 63 {"Authorized", Keys::AUTHORIZED}, | |
| 64 {"Submitter", Keys::SUBMITTER}, | |
| 65 {"Comment", Keys::COMMENT} | |
| 66 }; | |
| 67 | |
| 68 /* Parse size from description */ | |
| 69 std::istringstream descstream(description); | |
| 70 | |
| 71 for (std::string line; std::getline(descstream, line);) { | |
| 72 const size_t pos = line.find_first_of(':', 0); | |
| 73 if (pos == std::string::npos) | |
| 74 continue; | |
| 75 | |
| 76 const std::string key = line.substr(0, pos); | |
| 77 const std::string value = line.substr(line.find_first_not_of(": ", pos)); | |
| 78 | |
| 79 switch (KeyMap.at(key)) { | |
| 80 case Keys::COMMENT: | |
| 81 torrent.SetDescription(value); | |
| 82 break; | |
| 83 case Keys::SIZE: | |
| 84 torrent.SetSize(Strings::HumanReadableSizeToBytes(value)); | |
| 85 break; | |
| 86 default: | |
| 87 break; | |
| 88 } | |
| 89 } | |
| 90 } | |
| 91 | |
| 57 void TorrentsPageListModel::ParseTorrentList(const QByteArray& ba) { | 92 void TorrentsPageListModel::ParseTorrentList(const QByteArray& ba) { |
| 58 std::istringstream stdstream(Strings::ToUtf8String(ba)); | 93 std::istringstream stdstream(Strings::ToUtf8String(ba)); |
| 59 | 94 |
| 60 pugi::xml_document doc; | 95 pugi::xml_document doc; |
| 61 if (!doc.load(stdstream)) | 96 if (!doc.load(stdstream)) |
| 84 torrent.SetTitle(Strings::ToUtf8String(elements.get(anitomy::kElementAnimeTitle))); | 119 torrent.SetTitle(Strings::ToUtf8String(elements.get(anitomy::kElementAnimeTitle))); |
| 85 torrent.SetEpisode(Strings::RemoveLeadingChars(Strings::ToUtf8String(elements.get(anitomy::kElementEpisodeNumber)), '0')); | 120 torrent.SetEpisode(Strings::RemoveLeadingChars(Strings::ToUtf8String(elements.get(anitomy::kElementEpisodeNumber)), '0')); |
| 86 torrent.SetGroup(Strings::ToUtf8String(elements.get(anitomy::kElementReleaseGroup))); | 121 torrent.SetGroup(Strings::ToUtf8String(elements.get(anitomy::kElementReleaseGroup))); |
| 87 torrent.SetResolution(Strings::ToUtf8String(elements.get(anitomy::kElementVideoResolution))); | 122 torrent.SetResolution(Strings::ToUtf8String(elements.get(anitomy::kElementVideoResolution))); |
| 88 } | 123 } |
| 89 torrent.SetDescription(Strings::TextifySynopsis(item.child_value("description"))); | 124 |
| 90 { | 125 ParseFeedDescription(Strings::TextifySynopsis(item.child_value("description")), torrent); |
| 91 /* Parse description... */ | 126 |
| 92 enum class Keys { SIZE, AUTHORIZED, SUBMITTER, COMMENT }; | |
| 93 | |
| 94 const std::unordered_map<std::string, Keys> KeyMap = { | |
| 95 {"Size", Keys::SIZE}, | |
| 96 {"Authorized", Keys::AUTHORIZED}, | |
| 97 {"Submitter", Keys::SUBMITTER}, | |
| 98 {"Comment", Keys::COMMENT} | |
| 99 }; | |
| 100 | |
| 101 const std::string description = Strings::TextifySynopsis(item.child_value("description")); | |
| 102 | |
| 103 /* Parse size from description */ | |
| 104 std::istringstream descstream(description); | |
| 105 | |
| 106 for (std::string line; std::getline(descstream, line);) { | |
| 107 const size_t pos = line.find_first_of(':', 0); | |
| 108 if (pos == std::string::npos) | |
| 109 continue; | |
| 110 | |
| 111 const std::string key = line.substr(0, pos); | |
| 112 const std::string value = line.substr(line.find_first_not_of(": ", pos)); | |
| 113 | |
| 114 switch (KeyMap.at(key)) { | |
| 115 case Keys::COMMENT: | |
| 116 torrent.SetDescription(value); | |
| 117 break; | |
| 118 case Keys::SIZE: | |
| 119 torrent.SetSize(Strings::HumanReadableSizeToBytes(value)); | |
| 120 break; | |
| 121 default: | |
| 122 break; | |
| 123 } | |
| 124 } | |
| 125 } | |
| 126 torrent.SetLink(item.child_value("link")); | 127 torrent.SetLink(item.child_value("link")); |
| 127 torrent.SetGuid(item.child_value("guid")); | 128 torrent.SetGuid(item.child_value("guid")); |
| 128 { | 129 { |
| 129 const QString date_str = Strings::ToQString(item.child_value("pubDate")); | 130 const QString date_str = Strings::ToQString(item.child_value("pubDate")); |
| 130 torrent.SetDate(QDateTime::fromString(date_str, "ddd, dd MMM yyyy HH:mm:ss t")); | 131 torrent.SetDate(QDateTime::fromString(date_str, "ddd, dd MMM yyyy HH:mm:ss t")); |
