aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjvech <jmvalenciae@unal.edu.co>2022-01-24 13:10:28 -0500
committerjvech <jmvalenciae@unal.edu.co>2022-01-24 13:10:28 -0500
commit2c7d72db92a00f653b5f5db77b1ce5bc22800a07 (patch)
treeaf3fc48dfe193f5f3d83e97fae7e8c0b7c3631f3
parent9bf647a224ffe3f18a19fd8957406bc4f3e111d2 (diff)
It works, now lets add comments and documentation
-rw-r--r--Makefile14
-rw-r--r--main.c136
-rw-r--r--main.h2
3 files changed, 117 insertions, 35 deletions
diff --git a/Makefile b/Makefile
index bf3c585..42c1b78 100644
--- a/Makefile
+++ b/Makefile
@@ -1,8 +1,16 @@
CC = gcc
-CFLAGS=-std=c99 -pedantic-errors
+CFLAGS=-std=c99 -pedantic-errors -Wall
+BIN=cli-tube
build: main.c
- $(CC) $(CFLAGS) main.c -o main
+ $(CC) $(CFLAGS) main.c -o $(BIN)
debug: main.c
- $(CC) -g main.c -o main $(CFLAGS)
+ $(CC) -g main.c -o $(BIN) $(CFLAGS)
+ valgrind --leak-check=full ./$(BIN) foo bar
+
+test: test.c
+ $(CC) test.c -o test $(CFLAGS) -lcurl
+
+clean:
+ rm main *.o $(BIN) -v
diff --git a/main.c b/main.c
index 7d0370a..bc3a51a 100644
--- a/main.c
+++ b/main.c
@@ -1,7 +1,11 @@
#include <stdio.h>
+#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
+#include <time.h>
+#include <sys/types.h>
+#include <sys/wait.h>
#include "main.h"
void error(char *c)
@@ -13,7 +17,7 @@ void error(char *c)
list *list_create(char title[], char id[])
{
list *x = malloc(sizeof(list));
- sscanf(title, "%100[^\n]", x->name);
+ sscanf(title, "%199[^\n]", x->name);
sscanf(id, "%11s", x->id);
x->next = NULL;
return x;
@@ -44,7 +48,7 @@ void list_print_item(list *x, int i)
} else {
for(p = x; i != 0; p = p->next, i--){
if (p == NULL) {
- printf("List index out of range");
+ fprintf(stderr, "List index out of range");
exit(1);
}
}
@@ -53,54 +57,124 @@ void list_print_item(list *x, int i)
printf("id: %s\n\n", p->id);
}
+list *list_get_node(list *x, int index)
+{
+ list *p;
+ for (p = x; index != 0; p = p->next, index--) {
+ if (p == NULL) {
+ fprintf(stderr, "List index out of range");
+ exit(1);
+ }
+ }
+ return p;
+}
+
list *parse_stream(FILE *file)
{
+ int i;
list *videos = list_create("", "");
+ char buffer_id[ID_LENGTH], buffer_title[TITLE_LENGTH];
+ do {
+ i = fscanf(file, "\"videoRenderer\":{\"videoId\":\"%11s\"", buffer_id);
+ if (i == 1){
+ do{
+ i = fscanf(file, "\"title\":{\"runs\":[{\"text\":%199[^}]\"}", buffer_title);
+ if (i == 1){
+ list_append(videos, buffer_title, buffer_id);
+ break;
+ }
+ } while (fscanf(file, "%*c") != EOF);
+ }
+ } while (fscanf(file, "%*c") != EOF);
return videos;
}
-int main(int argc, char **argv)
+char *argv_concat(char *concat_char, char **argv, int argc)
{
- FILE *file;
- list *videos = list_create("", "");
- char buffer_id[ID_LENGTH], buffer_name[TITLE_LENGTH];
int i;
+ size_t concat_length = 0;
+ concat_char[0] = '\0';
+
+ for (i = 1; i < argc; i++){
+ concat_length += strlen(argv[i]);
+ }
- if (argc != 2){
- printf("You must provide 1 argument not %i\n", argc - 1);
+ concat_length += argc - 2;
+ if (concat_length > TITLE_LENGTH - 1){
+ fprintf(stderr,
+ "Input characters exceded: you can enter %i "
+ "chars at most not %i\n",
+ TITLE_LENGTH - 1, (int)concat_length);
exit(1);
}
- if (!strcmp(argv[1], "-")){
- file = stdin;
+ strcat(concat_char, argv[1]);
+ for (i = 2; i < argc; i++) {
+ strcat(concat_char, "+");
+ strcat(concat_char, argv[i]);
+ }
+ return concat_char;
+}
- } else {
- file = fopen(argv[1], "r");
- if (file == NULL){
- error("Can't open file stream");
- }
+int main(int argc, char **argv)
+{
+ char query[TITLE_LENGTH];
+
+ if (argc < 2) {
+ printf("You must provide at least 1 argument not %i\n", argc - 1);
+ exit(1);
}
- do {
- i = fscanf(file, "\"videoRenderer\":{\"videoId\":\"%11s\"", buffer_id);
- if (i == 1){
- do{
- i = fscanf(file, "\"title\":{\"runs\":[{\"text\":\"%100[^\"]\"", buffer_name);
- if (i == 1){
- list_append(videos, buffer_name, buffer_id);
- break;
- }
- } while (fscanf(file, "%*c") != EOF);
+ argv_concat(query, argv, argc);
+ char curl_query[TITLE_LENGTH + 43] = {"https://www.youtube.com/results?search_query=algo"};
+ strcat(curl_query, query);
+
+ int curl_main_pipe[2];
+ if (pipe(curl_main_pipe) == -1) {
+ error("Can't create the pipe");
+ }
+
+ pid_t curl_pid = fork();
+
+ if (curl_pid == -1) {
+ error("Can't fork curl process");
+ } else if (!curl_pid) {
+ close(curl_main_pipe[0]);
+ dup2(curl_main_pipe[1], 1);
+ if (execlp("curl", "curl", curl_query, NULL) == -1) {
+ error("Can't run curl command");
}
- } while (fscanf(file, "%*c") != EOF);
+ }
+ dup2(curl_main_pipe[0], 0);
+ close(curl_main_pipe[1]);
- list *q;
- for (q = videos; q != NULL; q = q->next){
- printf("title: %s\n", q->name);
- printf("vidId: %s\n", q->id);
- printf("\n\n");
+ list *videos = parse_stream(stdin);
+ if (waitpid(curl_pid, NULL, 0) == -1) {
+ error("Can't wait curl process");
+ }
+ close(curl_main_pipe[0]);
+ if (!freopen("/dev/tty", "r", stdin)) {
+ error("/dev/tty");
}
+ list *q;
+ int i = 0, index_video;
+ do {
+ for (q = videos->next, i = 1; q != NULL; q = q->next, i++) {
+ printf("%3d %s\n", i, q->name);
+ }
+ printf("Youtube results select one video i=%i: ", i);
+ scanf("%d", &index_video);
+ } while (index_video >= i);
+
+
+ q = list_get_node(videos, index_video);
+ char url_watch[45];
+ sprintf(url_watch, "https://www.youtube.com/watch?v=%s", q->id);
list_free(videos);
+
+ if (execlp("mpv", "mpv", url_watch, NULL) == -1) {
+ error("Can't run mpv");
+ }
return 0;
}
diff --git a/main.h b/main.h
index 163673a..ccc9ae1 100644
--- a/main.h
+++ b/main.h
@@ -1,4 +1,4 @@
-#define TITLE_LENGTH 101
+#define TITLE_LENGTH 200
#define ID_LENGTH 12
typedef struct list {
Feel free to download, copy and edit any repo