77#include < Geode/Result.hpp>
88#include < Geode/utils/web.hpp>
99
10- #include < jukebox/download/download.hpp>
1110#include < jukebox/download/hosted.hpp>
1211
1312using namespace geode ::prelude;
13+ using namespace arc ;
1414
15- web::WebTask getMetadata (const std::string& id);
16- Result<std::string> getUrlFromMetadataPayload (web::WebResponse* resp );
17- jukebox::download::DownloadTask onMetadata (web::WebResponse* );
15+ web::WebFuture getMetadata (const std::string& id);
16+ Result<std::string> getUrlFromMetadataPayload (web::WebResponse r );
17+ Future<Result<ByteVector>> onMetadata (web::WebResponse result );
1818
19- namespace jukebox {
19+ namespace jukebox ::download {
2020
21- namespace download {
22-
23- DownloadTask startYoutubeDownload (const std::string& id) {
21+ Future<Result<ByteVector>> startYoutubeDownload (const std::string& id) {
2422 if (id.length () != 11 ) {
25- return DownloadTask::immediate ( Err (" Invalid YouTube ID" ) );
23+ co_return Err (" Invalid YouTube ID" );
2624 }
2725
28- return getMetadata (id). chain (
29- [](web::WebResponse* r) { return onMetadata (r); } );
26+ auto metadata = co_await getMetadata (id);
27+ co_return co_await onMetadata ( std::move (metadata) );
3028}
3129
32- } // namespace download
33-
34- } // namespace jukebox
30+ } // namespace jukebox::download
3531
36- Result<std::string> getUrlFromMetadataPayload (web::WebResponse* r) {
37- if (!r-> ok ()) {
32+ Result<std::string> getUrlFromMetadataPayload (web::WebResponse r) {
33+ if (!r. ok ()) {
3834 return Err (fmt::format (
39- " cobalt metadata query failed with status code {}" , r-> code ()));
35+ " cobalt metadata query failed with status code {}" , r. code ()));
4036 }
4137
42- Result<matjson::Value> jsonRes = r-> json ();
38+ Result<matjson::Value> jsonRes = r. json ();
4339 if (jsonRes.isErr ()) {
4440 return Err (" cobalt metadata query returned invalid JSON" );
4541 }
@@ -57,15 +53,15 @@ Result<std::string> getUrlFromMetadataPayload(web::WebResponse* r) {
5753 return Ok (payload[" url" ].asString ().unwrap ());
5854}
5955
60- web::WebTask getMetadata (const std::string& id) {
56+ web::WebFuture getMetadata (const std::string& id) {
6157 int timeout = Mod::get ()->getSettingValue <int >(" download-timeout" );
6258
6359 if (timeout < 30 ) {
6460 timeout = 30 ;
6561 }
6662
6763 return web::WebRequest ()
68- .timeout (std::chrono::seconds (30 ))
64+ .timeout (std::chrono::seconds (timeout ))
6965 .bodyJSON (matjson::makeObject (
7066 {{" url" , fmt::format (" https://www.youtube.com/watch?v={}" , id)},
7167 {" aFormat" , " mp3" },
@@ -75,11 +71,11 @@ web::WebTask getMetadata(const std::string& id) {
7571 .post (" https://api.cobalt.tools/api/json" );
7672}
7773
78- jukebox::download::DownloadTask onMetadata (web::WebResponse* result) {
79- Result<std::string> res = getUrlFromMetadataPayload (result);
74+ Future<Result<ByteVector>> onMetadata (web::WebResponse result) {
75+ Result<std::string> res = getUrlFromMetadataPayload (std::move ( result) );
8076 if (res.isErr ()) {
81- return jukebox::download::DownloadTask::immediate ( Err (res.unwrapErr () ));
77+ co_return Err (res.unwrapErr ());
8278 }
8379
84- return jukebox::download::startHostedDownload (res.unwrap ());
80+ co_return co_await jukebox::download::startHostedDownload (res.unwrap ());
8581}
0 commit comments