-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Video.cpp
106 lines (92 loc) · 2.86 KB
/
Video.cpp
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "Video.h"
#include <sstream>
#include <curl/curl.h>
#include <nlohmann/json.hpp> // Include the JSON library
#include "MemoryUtility.h"
#include "fstream"
using json = nlohmann::json;
Video::Video() {
videoinfo = MemoryUtility::ReadVideoId();
if (videoinfo.size() > 1) {
type = "series";
s = videoinfo[1];
ep = videoinfo[2];
}
else {
type = "movie";
}
id = videoinfo[0];
setvidinfo();
}
std::vector<std::string> Video::getvidinfo() {
return MemoryUtility::ReadVideoId();
}
double Video::gettime() {
return MemoryUtility::ReadVideoTime();
}
void Video::setvidinfo() {
std::string url = "https://v3-cinemeta.strem.io/meta/" + type + "/" + id + ".json";
CURL* curl;
CURLcode res;
std::string readBuffer;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
curl_global_cleanup();
// Default values
poster = "default_poster_url";
name = "Unknown Title";
year = "Unknown Year";
thumbnail = "default_thumbnail_url";
epname = "Unknown Episode Name";
// Parse JSON
auto json = nlohmann::json::parse(readBuffer, nullptr, false);
if (json.is_discarded()) {
return;
}
if (json.contains("meta")) {
auto meta = json["meta"];
if (meta.contains("poster")) {
poster = meta["poster"].get<std::string>();
}
if (meta.contains("name")) {
name = meta["name"].get<std::string>();
}
if (meta.contains("year")) {
year = meta["year"].get<std::string>();
}
if (type == "series" && meta.contains("videos")) {
for (const auto& video : meta["videos"]) {
if (video.contains("id") && video["id"].get<std::string>() == join(videoinfo, ":")) {
if (video.contains("thumbnail")) {
thumbnail = video["thumbnail"].get<std::string>();
}
if (video.contains("name")) {
epname = video["name"].get<std::string>();
}
break;
}
}
}
}
}
size_t Video::WriteCallback(void* contents, size_t size, size_t nmemb, std::string* s) {
s->append((char*)contents, size * nmemb);
return size * nmemb;
}
std::string Video::join(const std::vector<std::string>& v, const std::string& delimiter) {
std::ostringstream oss;
for (size_t i = 0; i < v.size(); ++i) {
oss << v[i];
if (i != v.size() - 1) {
oss << delimiter;
}
}
return oss.str();
}