QUOTE: Enjoy small things, cherish moments.

rx

A terminal-based radio player

byb.c (1455B)


      1 #include <stdlib.h>
      2 #include <string.h>
      3 #include <curl/curl.h>
      4 #define LIB_STR_IMPL
      5 #include <libstr.h>
      6 
      7 #define RX_BYB_URL "https://playerservices.streamtheworld.com/api/livestream?station=BYB_RADIO&transports=http%2Chls&version=1.10&request.preventCache=1735994936887"
      8 
      9 size_t rx_curl_write(void *ptr, size_t size, size_t nmemb, void *data) {
     10   size_t len = size * nmemb;
     11   char **curl_data = (char **) data;
     12   str_append_len(curl_data, ptr, len);
     13   return len;
     14 }
     15 
     16 char *rx_resolve(void) {
     17   CURL *curl = curl_easy_init();
     18   if (!curl) {
     19     fprintf(stderr, "CURL init failed!\n");
     20     return NULL;
     21   }
     22   char *curl_data = NULL;
     23   curl_easy_setopt(curl, CURLOPT_URL, RX_BYB_URL);
     24   curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, rx_curl_write);
     25   curl_easy_setopt(curl, CURLOPT_WRITEDATA, &curl_data);
     26   CURLcode code = curl_easy_perform(curl);
     27   if (code != CURLE_OK) {
     28     curl_easy_cleanup(curl);
     29     fprintf(stderr, "CURL fetch failed!\n%s\n", curl_easy_strerror(code));
     30     return NULL;
     31   }
     32   curl_easy_cleanup(curl);
     33   char *url = NULL;
     34   size_t set = 0;
     35   for (size_t i = 0; i < strlen(curl_data); i++) {
     36     if(strncmp(curl_data + i, "<ip>", 4) == 0) {
     37       set = i + 4;
     38       continue;
     39     }
     40     if (set && curl_data[i] == '<') {
     41       size_t len = i - set;
     42       str_append(&url, "https://");
     43       str_append_len(&url, curl_data + set, len);
     44       str_append(&url, "/BYB_RADIO.mp3");
     45       break;
     46     }
     47   }
     48   free(curl_data);
     49   return url;
     50 }