json - C / Youtube API Request > VLC -


im trying improve tiny c application wich streams youtube videos. reason im not beeing able send request , decode json. point me in right direction, original.

#include <arpa/inet.h> #include <assert.h> #include <errno.h> #include <netinet/in.h> #include <signal.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <sys/wait.h> #include <unistd.h>  #define sa      struct sockaddr #define maxline 4096 #define listenq 1024  void process_http(int sockfd, char argv[]){      char sendline[maxline], recvline[maxline];     char storeline[maxline];     char run_command[50];     char * linkpos;     ssize_t n;     int arraysize;     int i;      arraysize = strlen(&argv[0]);      if(&argv[0] == "\32") {          printf("har space\n");      }        strcpy(sendline,"get /?youtubereq=");     strcat(sendline,&argv[0]);     strcat(sendline," /http/1.0\r\n");     strcat(sendline,"connection: keep-alive\r\n");     strcat(sendline,"pragma: no-cache\r\n");     strcat(sendline,"host: 127.0.0.1\r\n");     strcat(sendline,"accept: www/source\r\n");     strcat(sendline,"accept: text/html\r\n\r\n");     write(sockfd, sendline, strlen(sendline));      while ( ( n = read(sockfd, recvline, maxline)) != 0 )     {         recvline[n]='\0';         strcat(storeline, recvline);     }     linkpos = strstr(storeline, "http://www.youtube.com/");     strcpy(run_command, "cvlc ");     strcat(run_command, linkpos);     system("clear");     printf("playing video...\n");     system(run_command); }  void encode(unsigned char *s, char *enc, char *tb) {     (; *s; s++) {         if (tb[*s]) sprintf(enc, "%c", tb[*s]);         else        sprintf(enc, "%%%02x", *s);         while (*++enc);     } }  int main(int argc, char* argv[]) {   int sockfd;   struct sockaddr_in servaddr;   char rfc3986[256] = {0};   char html5[256] = {0};    sockfd = socket(af_inet, sock_stream, 0);   bzero(&servaddr, sizeof(servaddr));   servaddr.sin_family = af_inet;   servaddr.sin_port = htons(80);   inet_pton(af_inet, "127.0.0.1", &servaddr.sin_addr);    connect(sockfd, (sa *) &servaddr, sizeof(servaddr));    char enc[sizeof(argv[1]) * 3];      int i;     (i = 0; < 256; i++) {         rfc3986[i] = isalnum(i)||i == '~'||i == '-'||i == '.'||i == '_'             ? : 0;         html5[i] = isalnum(i)||i == '*'||i == '-'||i == '.'||i == '_'             ? : (i == ' ') ? '+' : 0;     }     printf("loading video, please wait...\n");   encode(argv[1], enc, rfc3986);   process_http(sockfd, enc);    return 0;  } 

im relying on php/apache run on localhost api-request me, far optimal. stated above cant seem able implement part in code.

(im quite new c)

the php code follows.

<?php if($_get['youtubereq']) { $req = $_get['youtubereq']; $req = urlencode($req); $build_yt_url = "http://gdata.youtube.com/feeds/api/videos?q='" . $req  . "'&format=5&max-results=1&v=2&alt=jsonc";   $response = file_get_contents($build_yt_url); $response = json_decode($response, true);  $raw_url = "http://www.youtube.com/watch?v=" . $response["data"]["items"][0]["id"]; echo $raw_url; } else { echo "."; } ?> 

program works ./youtubeplayer "some video search for"

ideas? :)

you're complicating. can run php command line:

<?php if ($argc != 2) die("wrong params\n"); $build_yt_url = "http://gdata.youtube.com/feeds/api/videos?q='"     . urlencode($argv[1]) . "'&format=5&max-results=1&v=2&alt=jsonc";  $response = file_get_contents($build_yt_url) or die("error fetching data\n"); $response = json_decode($response, true);  if (!isset($response["data"]["items"][0]["id"]))     die("error finding in result\n");  $raw_url = "http://www.youtube.com/watch?v=" .         $response["data"]["items"][0]["id"];  system("cvlc " . escapeshellarg($raw_url)); 

then run with:

php myscript.php cat 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -