youtube_to_rss/curl_get_youtube_rss_link.c
2023-12-24 17:49:37 +01:00

95 lines
2.3 KiB
C

// @COMPILECMD gcc $@ -o $* -lcurl
#include <stdio.h>
#include <curl/curl.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
char *string;
size_t size;
} Response;
size_t write_chunk(void *data, size_t size, size_t nmemb, void *userdata);
int main(int argc, char** argv){
if(argc != 2){
fprintf(stderr, "how to use: ./curl_get_youtube_rss_link BroCodez (aka. the youtube channel name as seen on the link when you are on that channel`s home or video page ... without the @\n");
exit(1);
}
CURL *curl;
CURLcode result;
curl = curl_easy_init();
if (curl == NULL){
fprintf(stderr, "HTTP request failed\n");
return -1;
}
Response response;
response.string = malloc(1);
response.size = 0;
char url[120];
sprintf(url, "https://www.youtube.com/@%s/videos", argv[1]);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_chunk);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) &response);
result = curl_easy_perform(curl);
if (result != CURLE_OK){
fprintf(stderr, "Error: %s\n", curl_easy_strerror(result));
return -1;
}
int start_of_rss_link = 0;
int end_of_rss_link = 0;
for(int i = strlen(response.string); i != 0; i--){
if(response.string[i] == 102){
if(response.string[i+1] == 101){
if(response.string[i+2] == 101){
if(response.string[i+3] == 100){
if(response.string[i+4] == 115){
if(response.string[i+5] == 47){
start_of_rss_link = i-24;
end_of_rss_link = i+51;
char link[80];
int k = 0;
for(int j = start_of_rss_link; j <= end_of_rss_link; j++){
link[k] = response.string[j];
k++;
if(j == end_of_rss_link){
link[k] = '\0';
}
}
printf("%s\n", link);
return 0;
}
}
}
}
}
}
}
curl_easy_cleanup(curl);
free(response.string);
return 0;
}
size_t write_chunk(void *data, size_t size, size_t nmemb, void *userdata){
size_t real_size = size * nmemb;
Response *response = (Response *) userdata;
char *ptr = realloc(response->string, response->size + real_size + 1);
if (ptr == NULL){
return CURL_WRITEFUNC_ERROR;
}
response->string = ptr;
memcpy(&(response->string[response->size]), data, real_size);
response->size += real_size;
response->string[response->size] = 0; // '\0';
return real_size;
}