|
|
|
@ -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); |
|
|
|
|