mirror of
https://github.com/305a385/ytfind.git
synced 2024-07-26 23:54:30 +00:00
Initial
This commit is contained in:
commit
f07b6e7aa6
15
Makefile
Normal file
15
Makefile
Normal file
|
@ -0,0 +1,15 @@
|
|||
include config.mk
|
||||
|
||||
default: build
|
||||
|
||||
build: clean
|
||||
$(CC) -Wall -o $(NAME) main.c util.c -l curl -ljson-c
|
||||
|
||||
clean:
|
||||
rm -rf $(NAME)
|
||||
|
||||
test: build
|
||||
./$(NAME) roar
|
||||
|
||||
#json: clean
|
||||
# $(CC) json.c -ljson-c -o json
|
2
config.h
Normal file
2
config.h
Normal file
|
@ -0,0 +1,2 @@
|
|||
static const char NAME[] = "ytfind";
|
||||
static const char QUERY_RESULT_FILE[] = "/tmp/query.json";
|
78
main.c
Normal file
78
main.c
Normal file
|
@ -0,0 +1,78 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <curl/curl.h>
|
||||
|
||||
#include "util.h"
|
||||
#include "config.h"
|
||||
|
||||
#include <json-c/json.h>
|
||||
#include <string.h>
|
||||
|
||||
int printTitles(void){
|
||||
json_object *root = json_object_from_file(QUERY_RESULT_FILE);
|
||||
if (!root) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int n = json_object_array_length(root);
|
||||
for (int i=0; i<n; i++) {
|
||||
json_object *type = json_object_object_get(json_object_array_get_idx(root, i), "type");
|
||||
//make sure a video is being chosen (not a channel, playlist, etc)
|
||||
if(! strcmp(json_object_get_string(type), "video")){
|
||||
json_object *title = json_object_object_get(json_object_array_get_idx(root, i), "title");
|
||||
json_object *videoId = json_object_object_get(json_object_array_get_idx(root, i), "videoId");
|
||||
|
||||
printf("%s\n%s\n\n", json_object_get_string(title), json_object_get_string(videoId));
|
||||
}
|
||||
}
|
||||
json_object_put(root);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if( argc != 2 ) {
|
||||
printf("usage: try './%s [query]' to make a get request.\n", NAME);
|
||||
return 1;
|
||||
}
|
||||
|
||||
char query[] = "https://invidious.projectsegfau.lt/api/v1/search?q=";
|
||||
strcat(query, argv[1]);
|
||||
|
||||
CURL *curl_handle;
|
||||
CURLcode res;
|
||||
|
||||
struct MemoryStruct chunk;
|
||||
chunk.memory = malloc(1);
|
||||
chunk.size = 0;
|
||||
|
||||
curl_handle = curl_easy_init();
|
||||
if(curl_handle) {
|
||||
curl_easy_setopt(curl_handle, CURLOPT_URL, query);
|
||||
curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1L);
|
||||
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
|
||||
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
|
||||
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "fetch-agent/0.1");
|
||||
|
||||
res = curl_easy_perform(curl_handle);
|
||||
|
||||
if(res != CURLE_OK) {
|
||||
fprintf(stderr, "error: %s\n", curl_easy_strerror(res));
|
||||
} else {
|
||||
// create pointer to QUERY_RESULT_FILE (where the query will be stored, you can change the value in config.h)
|
||||
FILE *tmpFilePtr = fopen(QUERY_RESULT_FILE,"w");
|
||||
fprintf(tmpFilePtr, "%s", chunk.memory);
|
||||
fclose(tmpFilePtr);
|
||||
}
|
||||
|
||||
curl_easy_cleanup(curl_handle);
|
||||
free(chunk.memory);
|
||||
|
||||
|
||||
//print titles of videos to stdout
|
||||
printTitles();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
21
util.c
Normal file
21
util.c
Normal file
|
@ -0,0 +1,21 @@
|
|||
#include "util.h"
|
||||
|
||||
size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp) {
|
||||
size_t realsize = size * nmemb;
|
||||
struct MemoryStruct *mem = (struct MemoryStruct *)userp;
|
||||
|
||||
char *ptr = realloc(mem->memory, mem->size + realsize + 1);
|
||||
if(ptr == NULL) {
|
||||
printf("error: not enough memory\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
mem->memory = ptr;
|
||||
memcpy(&(mem->memory[mem->size]), contents, realsize);
|
||||
mem->size += realsize;
|
||||
mem->memory[mem->size] = 0;
|
||||
|
||||
return realsize;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user