📄 download.c
字号:
/*U2B Downloader is a simple program with a user interface to download videosfrom youTube.com and convert them to mpeg format (optional). Copyright (c) 2007, Fabio Bruno Pereira <fabytes@gmail.com>This file is part of U2B Downloader. U2B Downloader is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. U2B Downloader is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with U2B Downloader. If not, see <http://www.gnu.org/licenses/>.*/#include <gtk/gtk.h>#include <sys/socket.h>#include <arpa/inet.h>#include <netdb.h>#include <errno.h>#include <unistd.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <strings.h>#include <regex.h>#include "download.h"extern GtkWidget *label_percent;char * http_post(const char * url, const char * params);#define MAX_BUFFER 204800char buffer[MAX_BUFFER+1];int size = 0; char* match(const char *string, const char *pattern){ int status; regex_t re; regmatch_t match; char * tparam = NULL; if ((status = regcomp(&re, pattern, REG_EXTENDED)) != 0) { if(status == REG_NOMATCH) printf("regex REG_NOMATCH\n"); if(status == REG_BADPAT) printf("regex REG_BADPAT\n"); if(status == REG_BADRPT) printf("regex REG_BADRPT\n"); if(status == REG_ERANGE) printf("regex REG_ERANGE\n"); return NULL; /* report error */ } status = regexec(&re, string, 1, &match, 0); regfree(&re); if (status != 0) { return NULL; /* report error */ } size = match.rm_eo - match.rm_so ; if( size > 5 ){ tparam = (char*)malloc(sizeof(char) * size); memset(tparam,0,sizeof(char) * size); strncpy(tparam,&(string[match.rm_so+4]),size-5); } return tparam;}static char * metacafe_download(char * url){ char * ret = NULL; char * endURL = NULL; char * mediaURL = NULL ; char video_id[256] ; static char data_location[256]; memset(video_id,0,256); ret = strstr(url,"com/"); if(ret == NULL) return NULL; strcpy(video_id,&(ret[3])); gdk_threads_enter(); gtk_label_set_text(GTK_LABEL(label_percent), "connecting www.metacafe.com..." ); gdk_threads_leave(); ret = http_post("www.metacafe.com",video_id); if(ret == NULL) return NULL; mediaURL = strstr(ret,"mediaURL"); if(mediaURL == NULL) return NULL; mediaURL += 9; endURL = strstr(mediaURL,".flv"); if(endURL == NULL) return NULL; int i =0 ; for(i = 0; i < (endURL-mediaURL)+4;i++){ data_location[i] = mediaURL[i] ; } data_location[i] = '\0' ; return (char*)&data_location;}char * find_url_to_download(char * url){ int connectionFd; struct sockaddr_in servaddr; struct hostent * hptr = NULL; static char data_location[256]; char * tparam = NULL; char video_id[256] ; char * ret; memset(data_location,0,256); memset(video_id,0,256); if((ret = strstr(url,"metacafe")) != NULL){ printf("fazendo download do metacafe!\n"); return metacafe_download(url); } ret = strstr(url,"com/"); if(ret == NULL) return NULL; strcpy(video_id,&(ret[3])); gdk_threads_enter(); gtk_label_set_text(GTK_LABEL(label_percent), "connecting www.youtube.com..." ); gdk_threads_leave(); ret = http_post("www.youtube.com",video_id); if(ret == NULL) return NULL; ret = strstr(url,"?v="); if(ret == NULL) return NULL; char vparam[126]; memset(vparam,0,126); sprintf(vparam,"/v/%s",(ret+3)); ret = http_post("www.youtube.com",vparam); //**************** tparam = strstr(ret,"&t="); if(tparam == NULL) return NULL; char * breakline = strchr(tparam,'\r'); if(breakline != NULL) *(breakline) = '\0' ; char tparam_ok[126]; int i = 0 ; for(i=0; i < 125 && *tparam ; i++){ tparam_ok[i] = *tparam; tparam++; } tparam_ok[i] = '\0' ; //************************* char tmp[160]; memset(tmp,0,160); sprintf(tmp,"/get_video?video_id=%s%s",&(video_id[9]),tparam_ok); gdk_threads_enter(); gtk_label_set_text(GTK_LABEL(label_percent), "fetching video url..." ); gdk_threads_leave(); ret = http_post("www.youtube.com", tmp); if(ret == NULL) return NULL ; char * ptr = strstr(buffer,"Location:"); if(ptr != NULL){ char *ptr_end = strstr(ptr+10,"\n"); int i = 0; while(ptr+10 < ptr_end){ data_location[i] = *(ptr+10) ; i++; ptr++; } data_location[i-1] = 0 ; ptr = index(&(data_location[7]),'/'); // jump the http:// char domain[265] ; memset(domain,0,256); strncpy(domain,&(data_location[7]),ptr-&(data_location[7])); connectionFd = socket(AF_INET,SOCK_STREAM,0); memset(&servaddr,0,sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(80); hptr = gethostbyname2(domain,AF_INET); if(hptr == NULL){ return NULL; } //printf("addr=%x\n",*(unsigned long *) *(hptr->h_addr_list)); servaddr.sin_addr.s_addr = *(unsigned long *) *(hptr->h_addr_list); //printf("addr=%s\n",inet_ntoa(servaddr.sin_addr)); sprintf(data_location,"http://%s%s",inet_ntoa(servaddr.sin_addr),ptr); //printf("data_location=%s*\n",data_location); }else return NULL ; return (char*)&data_location ;}char * http_post(const char * url, const char * params){ int connectionFd; int in; unsigned long limit = MAX_BUFFER ,index =0; struct sockaddr_in servaddr; struct hostent * hptr = NULL; char tmp[512]; memset(buffer,0,MAX_BUFFER+1); memset(tmp,0,512); sprintf(tmp,"GET %s HTTP/1.0\r\nHost: %s\r\nUser-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0\r\nAccept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\nAccept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png, */*;q=0.5\r\nAccept-Language: en-us,en;q=0.5\r\n\n",params,url); // printf("link:%s\n",tmp); char * string = (char*)malloc(sizeof(char)*strlen(tmp)+1); memset(string, 0, sizeof(char)*strlen(tmp)+1); strcpy(string,tmp); connectionFd = socket(AF_INET,SOCK_STREAM,0); memset(&servaddr,0,sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(80); hptr = gethostbyname2(url, AF_INET); if(hptr == NULL){ return NULL; } // printf("addr=%x\n",*(unsigned long *) *(hptr->h_addr_list)); servaddr.sin_addr.s_addr = *(unsigned long *) *(hptr->h_addr_list); if(connect(connectionFd,(struct sockaddr*)&servaddr,sizeof(servaddr)) ){ return NULL ; } write(connectionFd, string, strlen(string)); while((in=read(connectionFd, &(buffer[index]), limit)) >0 ){ index += in; limit -= in; } close(connectionFd); buffer[index] = 0 ; // printf("return=%s\n",buffer); return buffer;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -