gcc: get livestream url
To get the livestream video url from youtube you’ll need to use Google API-key and channel URL.
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 |
#include <stdio.h> #include <time.h> #include <stdlib.h> #include <string.h> #include "funk.h" #define MY_INITIAL_BUFFER_SIZE (1) int main(int argc, char **argv) { CURLcode result; struct my_buffer buffer; char curl_error[CURL_ERROR_SIZE]; buffer.memory = malloc(MY_INITIAL_BUFFER_SIZE); buffer.size = MY_INITIAL_BUFFER_SIZE; buffer.used = 0; result = get_url("https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=[YOUR_CHANNEL_ID]&eventType=live&type=video&key=[YOUR_API_KEY]", &buffer, curl_error); if (result == 0) { char * inhalt = NULL; inhalt = malloc(1 + buffer.used); memcpy(inhalt, buffer.memory, buffer.used); inhalt[buffer.used] = '\0'; if (inhalt) { int pos = strpos(inhalt, "\"videoId\": \"", 0); if (pos == -1) {} int endpos = strpos (inhalt, "\"", pos+10); char vid[12]; substring(vid, inhalt, endpos+1, endpos - pos); FILE * fp; fp = fopen ("/var/www/html/url.php", "w"); fprintf(fp, "<script> jsvid = \"https://www.youtube.com/watch?v=%s%s", vid, "\";</script>"); fclose(fp); char line[75]; char * ssh = "ssh uhla@elabu.ga -p 12036 echo "; char * fname = " \\>\\> /var/www/html/url.php"; char * command; asprintf(&command, "%s%s%s", ssh, vid, fname); puts(command); FILE *cmd = popen(command, "r"); // popen("ssh uhla@elabu.ga -p 12036 pidof ffmpeg", "r"); fgets(line, 75, cmd); pid_t pid = strtoul(line, NULL, 10); printf("pid: %d", pid); pclose(cmd); } else { puts(curl_error); } } // result == 0 return 0; } // main |
declarations funk.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#ifndef FUNK_H # define FUNK_H #include <stdio.h> #include <time.h> #include <stdlib.h> #include <string.h> #include <curl/curl.h> struct my_buffer { unsigned char *memory; size_t size; size_t used; }; size_t my_curl_write(char *ptr, size_t size, size_t nmemb, void *userdata); CURLcode get_url(const char *url, struct my_buffer *buffer, char *curl_error); int strpos(char *hay, char *needle, int offset); char* substring(char *destination, const char *source, int beg, int n); #endif |
declarations funk.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 |
#include <stdio.h> #include <time.h> #include <stdlib.h> #include <string.h> #include <curl/curl.h> #include "funk.h" #define MY_MAXIMUM_BUFFER_SIZE (4 * 1024 * 1024) extern size_t my_curl_write(char *ptr, size_t size, size_t nmemb, void *userdata){ struct my_buffer *buffer = userdata; size_t needed = size * nmemb; if (needed > (buffer->size - buffer->used)) { unsigned char *new_memory; size_t new_size = 2 * buffer->size; while (needed > (new_size - buffer->used)) { new_size *= 2; if (new_size > (MY_MAXIMUM_BUFFER_SIZE)) { return 0; } } new_memory = realloc(buffer->memory, new_size); if (!new_memory) { return 0; } buffer->memory = new_memory; buffer->size = new_size; } memcpy(buffer->memory + buffer->used, ptr, needed); buffer->used += needed; return needed; } extern CURLcode get_url(const char *url, struct my_buffer *buffer, char *curl_error) { CURLcode result; CURL *my_curl = curl_easy_init(); curl_easy_setopt(my_curl, CURLOPT_ERRORBUFFER, curl_error); curl_easy_setopt(my_curl, CURLOPT_FOLLOWLOCATION, 1); curl_easy_setopt(my_curl, CURLOPT_URL, url); curl_easy_setopt(my_curl, CURLOPT_WRITEFUNCTION, my_curl_write); curl_easy_setopt(my_curl, CURLOPT_WRITEDATA, buffer); result = curl_easy_perform(my_curl); curl_easy_cleanup(my_curl); return result; } int strpos(char *hay, char *needle, int offset) { char haystack[strlen(hay)]; strncpy(haystack, hay+offset, strlen(hay)-offset); char *p = strstr(haystack, needle); if (p) return p - haystack+offset; return -1; } char* substring(char *destination, const char *source, int beg, int n) { // extracts n characters from source string starting from beg index // and copy them into the destination string while (n > 0) { *destination = *(source + beg); destination++; source++; n--; } // null terminate destination string *destination = '\0'; // return the destination string return destination; } |