Skip to content
This repository has been archived by the owner on Apr 8, 2020. It is now read-only.

Commit

Permalink
download done
Browse files Browse the repository at this point in the history
  • Loading branch information
h4child committed Mar 30, 2018
1 parent b18f04b commit 1ad80a4
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 76 deletions.
3 changes: 2 additions & 1 deletion include/framebot/framebot.h
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,8 @@ UserProfilePhotos * get_user_profile_photos_chat(Bot * bot, char * dir, long use
long offset, long limit);

/* getFile */
char * get_file(Bot * bot, char * dir, const char * file_id);
File * get_file(Bot * bot, const char * file_id);
int file_download(Bot * bot, File * ofile, char *path);

/* kickChatMember */
bool kick_chat_member (Bot *bot, char *chat_id, long int user_id, char *until_date);
Expand Down
2 changes: 1 addition & 1 deletion include/framebot/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ MemStore * mem_store();
void mem_store_free(MemStore * memStore);
size_t mem_write_callback(void *content, size_t size, size_t nmemb, void *userp);
MemStore *call_method(const char *token, char *method);
char * call_method_download(const char * token, char * dir, File * ofile);
int call_method_download(const char * token, char * namefile, File * ofile);
MemStore * call_method_input_file(const char * token, IFile ifile);

#endif
3 changes: 2 additions & 1 deletion include/framebot/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

#include <framebot/framebot.h>

char *vsboolean_param_parser (char *base, int psize, ...);
char * file_extension_download(char *tgname, char *namefile);
char * vsboolean_param_parser (char *base, int psize, ...);
char * api_ltoa(long int n);
char * api_itoa(int n);
int api_atoi(char * str_int);
Expand Down
1 change: 1 addition & 0 deletions src/format.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#define STRLONG 21
#define STRINT 13


char *escape_string(char *str) {
CURL *curl = curl_easy_init();
if (curl) {
Expand Down
17 changes: 8 additions & 9 deletions src/framebot.c
Original file line number Diff line number Diff line change
Expand Up @@ -792,29 +792,28 @@ refjson *generic_method_call (const char *token, char *formats, ...) {
/**
* getFile
* https://core.telegram.org/bots/api#getfile
* info file
*/
char * get_file (Bot * bot, char * dir, const char * file_id){
File * get_file (Bot * bot, const char * file_id){

refjson *s_json;
char *path_file;
int ok = 0;

s_json = generic_method_call(bot->token, API_getfile, file_id);

if(!s_json)
return NULL;
return 0;

File * ofile = file_parse(s_json->content);

close_json(s_json);

if(ofile){
path_file = call_method_download(bot->token, dir, ofile);
file_free(ofile);
}

return path_file;
return ofile;
}

int file_download(Bot * bot, File * ofile, char *path){
return call_method_download(bot->token, path, ofile);
}

/**
* getUserProfilePhotos
Expand Down
65 changes: 28 additions & 37 deletions src/network.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <framebot/framebot.h>

static CURL *curl = NULL;
static CURL *down_curl = NULL;

/* start curl in framebot_init */
void network_init(){
Expand Down Expand Up @@ -77,11 +78,14 @@ MemStore * call_method(const char *token, char *method){
return NULL;
}

char * call_method_download(const char * token, char * dir, File *ofile){
int call_method_download(const char * token, char * namefile, File *ofile){
FILE * binary;
CURLcode res;
size_t path_len, url_size;
char * namefile, *path, *url;
size_t url_size;
char *url, *path;

if(!ofile)
return 0;

url_size = API_URL_FILE_LEN + strlen(token) + strlen(ofile->file_path) + 2;
url = (char *)calloc(1, url_size);
Expand All @@ -93,55 +97,42 @@ char * call_method_download(const char * token, char * dir, File *ofile){

url[url_size - 1] = '\0';

CURL * curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, url);
if(!down_curl)
down_curl = curl_easy_init();

curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(down_curl, CURLOPT_URL, url);
curl_easy_setopt(down_curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(down_curl, CURLOPT_SSL_VERIFYPEER, 0L);

free(url);

namefile = strstr(ofile->file_path, "/");
namefile++;

if(dir[strlen(dir) - 1] == '/')
dir[strlen(dir) - 1] = '\0';

if(!dir)
path_len = strlen(namefile) + 2;
else
path_len = strlen(dir) + strlen(namefile) + 2;

path = calloc(1, path_len);
if(!path)
return NULL;

if(dir){
strcpy(path, dir);
strcat(path, "/");
strcat(path, namefile);
if(namefile){
binary = fopen(namefile, "wb");
}
else{
strcpy(path, namefile);
}
char name[10];
static int id = 0;
sprintf(name, "file%d", id++);

binary = fopen(path, "wb");
binary = fopen(name, "wb");
}

if(binary){
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)binary);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
fclose(binary);
curl_easy_setopt(down_curl, CURLOPT_WRITEDATA, (void *)binary);
curl_easy_setopt(down_curl, CURLOPT_WRITEFUNCTION, NULL);
}
else{
return 0;
}

/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);

curl_easy_cleanup(curl);
res = curl_easy_perform(down_curl);
fclose(binary);

if (res == CURLE_OK)
return path;
return 1;

return NULL;
return 0;
}

MemStore * call_method_input_file(const char * token, IFile ifile){
Expand Down
73 changes: 48 additions & 25 deletions test/test_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,47 +19,70 @@
#define LIGHT_CYAN "\033[01;36m"
#define WHITE "\033[01;37m"

const char *token;
Bot *_bot;
Bot * _bot = NULL;
char *username = NULL;
long int chat_id = 0;
int valid_username = 0;
char *file_id = NULL;
char *namefile = NULL;

int _file(char * file_id){
char str[15];
strcpy(str, "CMakeFiles/");

get_file(_bot, str, file_id);
}
int _file(){
printf(WHITE "Send file ... "COLOR_RESET);

void _message(Update * update){
if(update){
if(update->message){
if(update->message->document)
_file(update->message->document->file_id);
}
if(file_download(_bot, get_file(_bot, file_id), namefile)){
printf(BLUE "OK\n" COLOR_RESET);
}
else{
Error *error = get_error();
if(error)
printf(RED"false\ncode:%ld | description:%s\n"COLOR_RESET, error->error_code, error->description);

exit(-1);
}

return 0;
}


/* only document */
int main(int argc, char **argv){
size_t update_length, i;
int main(int argc, char *argv[]){
framebot_init();

if(argc != 2)
fprintf(stderr, "file <token>");
if(argc < 4){
fprintf(stderr, "file <token> <username> <namefile>");
exit(-1);
}

_bot = framebot(argv[1]);
_bot = framebot(argv[1]);
if(!_bot){
fprintf(stderr, "ERROR authentic");
exit(-1);
}

Update * update;
Framebot *fbot = get_updates(_bot, NULL, 0, 0, 0, NULL);
update = fbot->up_message;
username = argv[2];
namefile = argv[3];

update_length = update_len(update);
Framebot *_update = NULL;

_update = get_updates(_bot, _update, 0, 0, 100, NULL);
Update *update = _update->up_message;

while(update){
_message(update);
if(update->message->document){
if(strcmp(update->message->from->username, argv[2]) == 0){
file_id = update->message->document->file_id;
valid_username = 1;
chat_id = update->message->from->id;
_file();
break;
}
}

update = update->next;
}

if(valid_username == 0)
printf("Username not found\n");

return 0;
}
}
2 changes: 1 addition & 1 deletion test/test_sendaudio.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ int main(int argc, char *argv[]){
framebot_init();

if(argc < 4){
fprintf(stderr, "sendphoto <token> <username> <path audio>");
fprintf(stderr, "sendaudio <token> <username> <path audio>");
exit(-1);
}

Expand Down
2 changes: 1 addition & 1 deletion test/test_sendvideonote.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ int main(int argc, char *argv[]){
framebot_init();

if(argc < 4){
fprintf(stderr, "sendphoto <token> <username> <path audio>");
fprintf(stderr, "sendvideonote <token> <username> <path audio>");
exit(-1);
}

Expand Down

0 comments on commit 1ad80a4

Please sign in to comment.