0

我正在尝试改进流式传输 YouTube 视频的小型 C 应用程序。出于某种原因,我无法发送请求和解码 JSON。有人可以指出我正确的方向,这是原来的。

#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)
{
    for (; *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;
    for (i = 0; i < 256; i++) {
        rfc3986[i] = isalnum(i)||i == '~'||i == '-'||i == '.'||i == '_'
            ? i : 0;
        html5[i] = isalnum(i)||i == '*'||i == '-'||i == '.'||i == '_'
            ? i : (i == ' ') ? '+' : 0;
    }
   

  printf("Loading video, please wait...\n");
  encode(argv[1], enc, rfc3986);
  process_http(sockfd, enc);

  return 0; 
}

我目前依靠 PHP/Apache 在 localhost 上运行来为我执行 API 请求,这远非最佳。但如上所述,似乎无法在此代码中实现该部分。

(我对C很陌生)

PHP 代码如下。

<?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 ".";
}
?>

程序像这样工作 ./youtubeplayer “一些要搜索的视频”

想法?

4

1 回答 1

0

你很复杂。您可以从命令行运行 php:

<?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));

然后运行:

php myscript.php cat
于 2013-05-19T00:25:09.870 回答