aboutsummaryrefslogtreecommitdiffstats
path: root/src/video.cpp
blob: 64bf526a1ff39e56739c7fffa0243e12d37d8ad6 (plain) (blame)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "video.hpp"

using json = nlohmann::json;

void Video::to_json(json& j, const format& f) {

    j = json{{"format", f.format}, 
        {"url", f.url}, 
        {"vcodec", f.vcodec}, 
        {"acodec", f.acodec}, 
        {"ext", f.ext}
    };

    if (f.quality.has_value()) { j["quality"] = f.quality.value(); }
    if (f.resolution.has_value()) { j["resolution"] = f.resolution.value(); }
    if (f.height.has_value()) { j["height"] = f.height.value(); }
    if (f.width.has_value()) { j["width"] = f.width.value(); }
}

void Video::to_json(json& j, const std::vector<format>& v) {

    j = json::array();

    for (format f: v) {

        j.emplace_back(json(f));
    }
}

void Video::to_json(json& j, const thumbnail& t) {

    j = json{{"url", t.url}, 
        {"preference", t.preference}, 
        {"id", t.id}
    };

    if (t.resolution.has_value()) { j["resolution"] = t.resolution.value(); }
    if (t.height.has_value()) { j["height"] = t.height.value(); }
    if (t.width.has_value()) { j["width"] = t.width.value(); }
}

void Video::to_json(json& j, const std::vector<thumbnail>& v) {

    j = json::array();

    for (thumbnail t: v) {

        j.emplace_back(json(t));
    }
}

void Video::to_json(json& j, const video& v) {

    j = json{{"id", v.id}, 
        {"title", v.title}, 
        {"webpage_url", v.url}, 
        {"channel_id", v.channelId}, 
        {"channel_url", v.channelUrl}};

    if (v.uploader.has_value()) { j["uploader"] = v.uploader.value(); }
    if (v.uploaderId.has_value()) { j["uploader_id"] = v.uploaderId.value(); }
    if (v.uploaderUrl.has_value()) { j["uploader_url"] = v.uploaderUrl.value(); }
    if (v.duration.has_value()) { j["duration"] = v.duration.value(); }
    if (v.viewcount.has_value()) { j["view_count"] = v.viewcount.value(); }
    if (v.description.has_value()) { j["description"] = v.description.value(); }

    j["formats"] = json::array();

    for (format f: v.formats) {

        j.at("formats").emplace_back(json(f));
    }

    j["thumbnails"] = json::array();

    for (thumbnail t: v.thumbnails) {

        j.at("thumbnails").emplace_back(json(t));
    }
}

void Video::from_json(const json& j, format& f) {

    j.at("format").get_to(f.format);
    j.at("url").get_to(f.url);
    j.at("vcodec").get_to(f.vcodec);
    j.at("acodec").get_to(f.acodec);
    j.at("ext").get_to(f.ext);

    if (j.contains("quality") && j.at("quality").is_number()) { f.quality = j.at("quality"); }
    if (j.contains("resolution") && j.at("resolution").is_string()) { f.resolution = j.at("resolution"); }
    if (j.contains("width") && j.at("width").is_number()) { f.width = j.at("width"); }
    if (j.contains("height") && j.at("height").is_number()) { f.height = j.at("height"); }
}

void Video::from_json(const json& j, std::vector<format>& v) {

    v.clear();
    for (json f: j) {

        v.push_back(f.get<format>());
    }
}

void Video::from_json(const json& j, thumbnail& t) {

    j.at("url").get_to(t.url);
    j.at("preference").get_to(t.preference);
    j.at("id").get_to(t.id);

    if (j.contains("resolution") && j.at("resolution").is_string()) { t.resolution = j.at("resolution"); }
    if (j.contains("width") && j.at("width").is_number()) { t.width = j.at("width"); }
    if (j.contains("height") && j.at("height").is_number()) { t.height = j.at("height"); }
}

void Video::from_json(const json& j, std::vector<thumbnail>& v) {

    v.clear();
    for (json t: j) {

        v.push_back(t.get<thumbnail>());
    }
}

void Video::from_json(const json& j, video& v) {

    j.at("id").get_to(v.id);
    j.at("title").get_to(v.title);
    j.at("webpage_url").get_to(v.url);
    j.at("channel_id").get_to(v.channelId);
    j.at("channel_url").get_to(v.channelUrl);

    if (j.contains("uploader")) { v.uploader = j.at("uploader"); }
    if (j.contains("uploader_id")) { v.uploaderId = j.at("uploader_id"); }
    if (j.contains("uploader_url")) { v.uploaderUrl = j.at("uploader_url"); }
    if (j.contains("duration")) { v.duration = j.at("duration"); }
    if (j.contains("view_count")) { v.viewcount = j.at("view_count"); }
    if (j.contains("description")) { v.description = j.at("description"); }

    for (json f: j.at("formats")) {

        v.formats.push_back(f.get<format>());
    }

    for (json t: j.at("thumbnails")) {
        
        v.thumbnails.push_back(t.get<thumbnail>());
    }
}