-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsponsor.h
65 lines (58 loc) · 1.95 KB
/
sponsor.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#pragma once
#include <iostream>
#include <regex>
static std::vector<double> splitToDouble(std::string string)
{
for (char &c : string) {
if (c == ',') {
c = ' ';
}
}
std::vector<double> ret;
const char *begin = string.c_str();
char* end = nullptr;
for (double timestamp = std::strtod(begin, &end); begin != end; timestamp = std::strtod(begin, &end)) {
begin = end;
if (errno == ERANGE){
std::cout << "range error, got " << timestamp << '\n';
errno = 0;
return {};
}
ret.push_back(timestamp);
}
return ret;
}
std::vector<Segment> downloadSegments(const std::string &videoId)
{
if (videoId.empty()) {
puts("Got empty videoId");
return {};
}
std::string query = "?videoID=" + videoId;
for (const std::string &category : s_categories) {
query += "&category=" + category;
}
std::string json = downloadFile("sponsor.ajay.app", 443, "/api/skipSegments" + query);
if (json.empty()) {
puts("Failed to download segments to skip");
return {};
}
std::regex segmentsRegex(R"--("segment"\s*:\[([^\]]+)\])--");
std::vector<Segment> segments;
std::sregex_iterator it = std::sregex_iterator(json.begin(), json.end(), segmentsRegex);
for (; it != std::sregex_iterator(); it++) {
const std::smatch match = *it;
std::string segmentsArray = match[1].str();
std::vector<double> numbers = splitToDouble(segmentsArray);
if (numbers.size() != 2) {
std::cout << "Invalid segment " << segmentsArray << std::endl;
return {};
}
if (s_verbose) {
std::cout << "Got segment " << numbers[0] << " -> " << numbers[1] << std::endl;
}
segments.push_back({numbers[0], numbers[1]});
}
std::cout << " - Got " << segments.size() << " skip segments for " << videoId << std::endl;
return segments;
}