-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
91 lines (78 loc) · 2.3 KB
/
main.c
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
#include "main.h"
int main(){
char *listID = (char*) malloc((MAX_ID_LENGTH + 1) * sizeof(char));
char *url = (char *) malloc(strlen(PREFIX) + 1 * sizeof(char));
strcpy(url, PREFIX);
printf("%s", "Enter the listID (64 chars max): ");
scanf("%64s", listID);
url = construct_url(url, listID);
send_playlist_request(url);
free(url);
url = NULL;
free(listID);
listID = NULL;
printf("All downloads completed!\n");
}
char* construct_url(char *url, char listID[]){
url = (char*) realloc(url, strlen(PREFIX) + strlen(listID) + 1 * sizeof(char));
strcat(url, listID);
url = (char*) realloc(url, strlen(PREFIX) + strlen(listID) + strlen(SUFFIX) + 1 * sizeof(char));
strcat(url, SUFFIX);
return url;
}
void send_playlist_request(char *url){
CURL *curl;
CURLcode res;
xml_buffer buffer;
char *xml_doc = NULL;
video_info *links;
init_buffer(&buffer);
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_to_buffer);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
res = curl_easy_perform(curl);
//Check for errors
if(res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
} else {
xml_doc = concat_buffer(&buffer, xml_doc);
links = parse_xml(xml_doc);
download_files(links);
free_video_info(links);
free(xml_doc);
xml_doc = NULL;
}
//Cleanup curl
curl_easy_cleanup(curl);
}
}
void download_files(video_info *v_info){
unsigned int i;
for (i = 0; i < v_info->amount; i++){
generate_file(v_info, i);
}
}
void generate_file(video_info *v_info, unsigned int i){
char *rest = "\\Documents\\%(title)s.%(ext)s";
char *opt = " -o \"";
int env_len = strlen(getenv("USERPROFILE"));
char opt_len = strlen(opt);
int rest_len = strlen(rest);
//1 extra for the "
char *file = (char*) malloc((env_len + opt_len + rest_len + 2) * sizeof(char));
file[0] = '\0';
strcat(file, opt);
file[opt_len] = '\0';
strcat(file, getenv("USERPROFILE"));
file[env_len + opt_len] = '\0';
strcat(file, rest);
file[env_len + rest_len + opt_len] = '\"';
file[env_len + rest_len + opt_len + 1] = '\0';
printf("Downloading \"%s\"...\n", v_info->titles[i]);
_spawnl(P_WAIT, "youtube-dl.exe", file, v_info->id[i], NULL);
free(file);
file = NULL;
printf("Download completed.\n");
}