2013-05-18 1 views
0

Im essayant d'améliorer ma petite application de C qui jette des vidéos de youtube. Pour une raison quelconque, je ne suis pas en mesure d'envoyer une requête et de décoder json. Quelqu'un pourrait-il me diriger dans la bonne direction, c'est l'original.C/Youtube Demande d'API> VLC

#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; 
} 

Im actuellement sur PHP comptant/Apache à exécuter sur localhost faire l'API demande pour moi, c'est loin d'être optimale. mais comme indiqué ci-dessus ne peux pas être en mesure de mettre en œuvre cette partie dans ce code.

(Im tout à fait nouveau à C)

Le code PHP est le suivant.

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

programme fonctionne comme ceci ./youtubeplayer "une vidéo pour rechercher"

Idées? :)

+0

Si vous voulez vraiment utiliser C pour interroger l'API YouTube, il serait préférable d'utiliser une bibliothèque JSON. Et essayez d'utiliser une bibliothèque comme curl pour la requête http, c'est plus simple que d'utiliser seulement les sockets api. – Artefacto

+0

Déjà fait avec la bibliothèque cURL, déplacé sur les sockets pour l'apprendre en utilisant simplement C. – JazzCat

+0

JSON Library n'était pas une mauvaise idée si – JazzCat

Répondre

0

Vous vous compliquez. Vous pouvez exécuter php à partir de la ligne de commande:

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

Lancez ensuite avec:

php myscript.php cat 
+1

Eh bien oui, mais par intention était d'apprendre C :) – JazzCat