cnuktube/shells/add_to_channels_rss.c
2023-12-22 19:05:56 +01:00

129 lines
3.3 KiB
C

#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){
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;
}
int main(int argc, char** argv){
if(argc != 2){fprintf(stderr, "how to use: (this program) (link to the youtube channel)\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;
curl_easy_setopt(curl, CURLOPT_URL, argv[1]);
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;}
if(strlen(response.string) < 5000){fprintf(stderr, "That YouTube Channel does not exist\n");exit(1);}
char channel_name[40];
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] == 34){
if(response.string[i-1] == 61){
if(response.string[i-2] == 116){
if(response.string[i-3] == 110){
if(response.string[i-4] == 101){
if(response.string[i-5] == 116){
if(response.string[i-6] == 110){
if(response.string[i-7] == 111){
if(response.string[i-8] == 99){
if(response.string[i-9] == 32){
if(response.string[i-10] == 34){
if(response.string[i-11] == 101){
if(response.string[i-12] == 109){
if(response.string[i-13] == 97){
if(response.string[i-14] == 110){
if(response.string[i-15] == 34){
int j = 1;
while(response.string[i+j] != 34){
//TODO ??? todo what? everything is done right?
channel_name[j-1] = response.string[i+j];
j++;
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
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';
}
}
FILE *f = fopen ("./rss/channels.txt", "a");
fprintf(f, "%s\n", link);
fclose(f);
return 0;
}
}
}
}
}
}
}
curl_easy_cleanup(curl);
free(response.string);
return 0;
}