Skip to content

Commit

Permalink
Add proxy support #24
Browse files Browse the repository at this point in the history
  • Loading branch information
faissaloo committed Mar 13, 2021
1 parent aafd09e commit 3077602
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
26 changes: 22 additions & 4 deletions src/sponskrub/sponskrub.d
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ int main(string[] args)
new ArgTemplate("no-id", true),
new ArgTemplate("keep-date", true),
new ArgTemplate("api-url", true, false, 1),
new ArgTemplate("proxy", true, false, 1),
]);

parsed_arguments.parse(args);
Expand All @@ -67,7 +68,7 @@ int main(string[] args)

if ("h" in parsed_arguments.flag_arguments || parsed_arguments.unrecognised_arguments.length > 0 || parsed_arguments.get_missing_arguments().length > 0) {
writeln(
"Usage: sponskrub [-h] [-chapter] [-exclude-sponsors] [-include-intros] [-include-outros] [-include-interactions] [-include-selfpromo] [-include-nonmusic] [-api-url url] video_id input_filename output_filename
"Usage: sponskrub [-h] [-chapter] [-exclude-sponsors] [-include-intros] [-include-outros] [-include-interactions] [-include-selfpromo] [-include-nonmusic] [-keep-date] [-proxy proxy] [-api-url url] video_id input_filename output_filename
SponSkrub is an application for removing sponsors from downloaded Youtube video
files, it requires an internet connection in order to consult the SponsorBlock
Expand Down Expand Up @@ -109,6 +110,9 @@ Options:
-keep-date
Give the output file the same modification date as the input file.
-proxy
Allows you to specify a proxy to route any requests through
");
return 1;
}
Expand All @@ -119,6 +123,14 @@ Options:
} else {
api_url = "sponsor.ajay.app";
}

string proxy;
if ("proxy" in parsed_arguments.flag_arguments) {
proxy = parsed_arguments.flag_arguments["proxy"].join;
} else {
proxy = "";
}

auto video_id = parsed_arguments.positional_arguments[1];
auto input_filename = parsed_arguments.positional_arguments[2];
auto output_filename = parsed_arguments.positional_arguments[3];
Expand All @@ -139,9 +151,9 @@ Options:
} else {
try {
if ("no-id" in parsed_arguments.flag_arguments) {
sponsor_times = get_video_skip_times_private(video_id, categories, api_url);
sponsor_times = get_video_skip_times_private(video_id, categories, api_url, proxy);
} else {
sponsor_times = get_video_skip_times_direct(video_id, categories, api_url);
sponsor_times = get_video_skip_times_direct(video_id, categories, api_url, proxy);
}
} catch (std.net.curl.HTTPStatusException e) {
if (e.status == 404) {
Expand All @@ -151,7 +163,13 @@ Options:
}
return 3;
} catch (std.net.curl.CurlException e) {
writeln("Couldn't connect to the specified API url, try specifying a different one using the -api-url flag");
writeln("Couldn't connect to the Sponsorblock API");
if (proxy != "") {
writeln("Ensure your proxy is correctly configured");
}
if (api_url != "sponsor.ajay.app") {
writeln("Make sure the specified api url is correct");
}
}

if (sponsor_times.length > 0) {
Expand Down
14 changes: 10 additions & 4 deletions src/sponskrub/sponsorblock.d
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ string stringify_timestamp(JSONValue raw_timestamp) {
return "%6f".format(timestamp);
}

ClipTime[] get_video_skip_times_direct(string video_id, Categories[] categories, string api_url) {
auto data = get("http://%s/api/skipSegments?videoID=%s&categories=%s".format(api_url, video_id, `["`~(cast(string[])categories).join(`", "`)~`"]`));
ClipTime[] get_video_skip_times_direct(string video_id, Categories[] categories, string api_url, string proxy="") {
auto data = proxy_get("http://%s/api/skipSegments?videoID=%s&categories=%s".format(api_url, video_id, `["`~(cast(string[])categories).join(`", "`)~`"]`), proxy);
auto json = parseJSON(data);
//This array needs sorting or whatever so they get lined up properly
//Or maybe we should get the thing that figures out the times to do that?
Expand All @@ -60,8 +60,8 @@ ClipTime[] get_video_skip_times_direct(string video_id, Categories[] categories,
).array;
}

ClipTime[] get_video_skip_times_private(string video_id, Categories[] categories, string api_url) {
auto data = get("http://%s/api/skipSegments/%s?categories=%s".format(api_url, sha256Of(video_id).toHexString!(LetterCase.lower)[0..uniform(3,32)], `["`~(cast(string[])categories).join(`", "`)~`"]`));
ClipTime[] get_video_skip_times_private(string video_id, Categories[] categories, string api_url, string proxy="") {
auto data = proxy_get("http://%s/api/skipSegments/%s?categories=%s".format(api_url, sha256Of(video_id).toHexString!(LetterCase.lower)[0..uniform(3,32)], `["`~(cast(string[])categories).join(`", "`)~`"]`), proxy);
auto json = parseJSON(data);
foreach (JSONValue video; json.array) {
if (video["videoID"].str == video_id) {
Expand All @@ -72,3 +72,9 @@ ClipTime[] get_video_skip_times_private(string video_id, Categories[] categories
}
return null;
}

auto proxy_get(string url, string proxy="") {
auto client = HTTP();
client.proxy = proxy;
return get(url, client);
}

0 comments on commit 3077602

Please sign in to comment.