Browse Source

implemented new functions

sandbox
- 3 months ago
parent
commit
3efaa52462
  1. 28
      include/peertube.hpp
  2. 61
      main.cpp

28
include/peertube.hpp

@ -279,6 +279,34 @@ void video_print(const struct video_t * const v) { @@ -279,6 +279,34 @@ void video_print(const struct video_t * const v) {
v -> views, v -> likes, v -> dislikes);
}
#include <cmath>
#include <climits>
// pick file download of a target resolution
struct file_t *video_pick_file(const struct video_t* const v, int target_res=0) {
if (v == NULL) {
return NULL;
}
if (v -> files == NULL) {
return NULL;
}
if (target_res == 0) {
// default behavior => pick best quality
return v-> files[0];
}
int lowest_delta = INT_MAX;
struct file_t *best_file = NULL;
unsigned int i;
for (i = 0; i < v -> file_count; ++i) {
int res = v -> files[i] -> resolution;
int delta = std::abs(target_res - res);
if (delta < lowest_delta) {
lowest_delta = delta;
best_file = v -> files[i];
}
}
return best_file;
}
#include <rapidjson/document.h>
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>

61
main.cpp

@ -261,6 +261,38 @@ void test (std::string video_url) { @@ -261,6 +261,38 @@ void test (std::string video_url) {
}
}
// download torrent file from peertube server
std::string dl_torrentfile(struct file_t *video_file, const char * title = NULL) {
if (video_file == NULL) {
return std::string();
}
if (video_file -> attribs == NULL) {
return std::string();
}
std::string torrent_url (video_file -> attribs[TORRENTDOWNLOAD_URL]);
std::string buffer = request(torrent_url);
std::string filename;
{
std::stringstream ss;
if (title != NULL) {
ss << title <<"-"<< video_file -> resolution << "p.torrent";
} else { // use the file name from the url
unsigned int pos = torrent_url.rfind("/");
pos++; // +1 to omit '/'
ss << torrent_url.substr(pos, torrent_url.size());
}
filename = ss.str();
std::cout << filename << std::endl;
}
std::fstream fout;
fout.open (filename, std::fstream::out | std::fstream::binary);
if (fout.good()) {
fout << buffer;
}
fout.close();
return filename;
}
void test2 (std::string video_url) {
bool is_playlist;
std::string api = get_endpoint(video_url, is_playlist);
@ -271,31 +303,10 @@ void test2 (std::string video_url) { @@ -271,31 +303,10 @@ void test2 (std::string video_url) {
struct video_t *video = init_from_json(root);
// get torrent download rul
int highest_res = 0;
struct file_t *best_file = NULL;
for (unsigned int i = 0; i < video -> file_count; ++i) {
int res = video -> files[i] -> resolution;
if (res > highest_res) {
best_file = video -> files[i];
highest_res = res;
}
}
if (best_file != NULL) {
std::cout << best_file -> attribs[TORRENTDOWNLOAD_URL] << std::endl;
std::string torrent_url (best_file -> attribs[TORRENTDOWNLOAD_URL]);
// filename format: video title - resolution label.torrent
std::string buffer = request(torrent_url);
std::string filename;
{
std::stringstream ss;
ss << video -> name <<"-"<< best_file -> resolution << "p.torrent";
filename = ss.str();
}
std::fstream fout;
fout.open (filename, std::fstream::out | std::fstream::binary);
if (fout.good()) {
fout << buffer;
}
fout.close();
struct file_t *best_file = video_pick_file(video);
std::string saved = dl_torrentfile(best_file, video -> name);
if (!saved.empty()) {
std::cout << "acquired .torrent file! " << std::endl;
}
std::cout << "\n\n\n" << std::endl;
video_print(video);

Loading…
Cancel
Save