From 957738c38ce7a364cc4886b5151a8891cae2a028 Mon Sep 17 00:00:00 2001 From: Nick White Date: Fri, 6 Jun 2008 10:55:54 +0100 Subject: Added section download support --- src/cli.c | 1 + src/download.c | 90 +++++++++++++++++++++++++++++++++------------------------- src/getht.c | 25 ++++++++++++++-- 3 files changed, 76 insertions(+), 40 deletions(-) diff --git a/src/cli.c b/src/cli.c index d15634d..9903f65 100644 --- a/src/cli.c +++ b/src/cli.c @@ -63,6 +63,7 @@ void showusage() printf("-l | --list-issues List available issues\n"); printf("-a | --download-all Download all issues\n"); printf("-d | --download-issue issno Download issue number issno\n"); + printf("-s | --download-section secno Download section number secno\n"); printf("-f | --force Force re-download of existing files\n"); printf("-h | --help Print this help message\n"); printf("-q | --quiet Make output less verbose\n"); diff --git a/src/download.c b/src/download.c index 7f2610a..41ec44b 100644 --- a/src/download.c +++ b/src/download.c @@ -175,19 +175,16 @@ double getremotefilesize(char *uri, struct config * options) return filesize; } -void downloadissue(struct config * options, iss * issue, int force) -/* Download issue pointed to */ +char * getissuedir(struct config * options, iss * issue) +/* Returns and prepares download directory */ { - sec * cur_section; - char newdir[STR_MAX]; - char filename[STR_MAX]; - char filepath[STR_MAX]; + char * newdir; + + newdir = malloc(STR_MAX); snprintf(newdir,STR_MAX,"%s/%i_%i-%i",options->save_path, issue->date.year,issue->date.firstmonth,issue->date.lastmonth); - printf("Downloading %s to %s\n",issue->title, newdir); - if(!opendir(newdir)) if(mkdir(newdir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) { @@ -195,39 +192,56 @@ void downloadissue(struct config * options, iss * issue, int force) scanf("%s", newdir); } - int count; - for(count = 0; count <= issue->no_of_sections; count++) - { - cur_section = issue->section[count]; - - snprintf(filename,STR_MAX,"section_%i.pdf", cur_section->number); - snprintf(filepath,STR_MAX,"%s/%s", newdir, filename); - if(!force){ - struct stat fileinfo; - /* see if local file exists */ - if(stat(filepath, &fileinfo)) - save_file(cur_section->uri, filepath, filename, 0, options); + return newdir; +} + +void downloadsection(struct config * options, sec * section, char * downdir, int force) +/* Download section pointed to */ +{ + char filename[STR_MAX]; + char filepath[STR_MAX]; + + snprintf(filename,STR_MAX,"section_%i.pdf", section->number); + snprintf(filepath,STR_MAX,"%s/%s", downdir, filename); + + if(!force){ + struct stat fileinfo; + /* see if local file exists */ + if(stat(filepath, &fileinfo)) + save_file(section->uri, filepath, filename, 0, options); + else + { + /* get size of local file */ + long localsize = 0; + localsize = (long) fileinfo.st_size; + + /* get size of remote file */ + long remotesize = 0; + remotesize = (long) getremotefilesize(section->uri, options); + + /* if size of local file != size of remote file, resume */ + if(remotesize > 0 && localsize < remotesize) + save_file(section->uri, filepath, filename, localsize, options); else { - /* get size of local file */ - long localsize = 0; - localsize = (long) fileinfo.st_size; - - /* get size of remote file */ - long remotesize = 0; - remotesize = (long) getremotefilesize(cur_section->uri, options); - - /* if size of local file != size of remote file, resume */ - if(remotesize > 0 && localsize < remotesize) - save_file(cur_section->uri, filepath, filename, localsize, options); - else - { - if(!options->quiet) - printf("Skipping download of completed section %i\n", cur_section->number); - } + if(!options->quiet) + printf("Skipping download of completed section %i\n", section->number); } } - else - save_file(cur_section->uri, filepath, filename, 0, options); } + else + save_file(section->uri, filepath, filename, 0, options); +} + +void downloadissue(struct config * options, iss * issue, int force) +/* Download issue pointed to */ +{ + char downdir[STR_MAX]; + strncpy(downdir, getissuedir(options, issue), STR_MAX); + + printf("Downloading %s to %s\n",issue->title, downdir); + + int count; + for(count = 0; count <= issue->no_of_sections; count++) + downloadsection(options, issue->section[count], downdir, force); } diff --git a/src/getht.c b/src/getht.c index 8f52d6f..6528652 100644 --- a/src/getht.c +++ b/src/getht.c @@ -67,6 +67,7 @@ int main(int argc, char *argv[]) int downall = 0; int downissue = 0, downissueno = -1; + int downsection = 0, downsectionno = -1; int listissues = 0; int force = 0; int option = 0; @@ -90,6 +91,7 @@ int main(int argc, char *argv[]) { {"download-all", no_argument, 0, 'a'}, {"download-issue", required_argument, 0, 'd'}, + {"download-section", required_argument, 0, 's'}, {"force", no_argument, 0, 'f'}, {"list-issues", no_argument, 0, 'l'}, {"quiet", no_argument, 0, 'q'}, @@ -99,7 +101,7 @@ int main(int argc, char *argv[]) {"version", no_argument, 0, 'V'}, {0, 0, 0, 0} }; - while((c = getopt_long(argc, argv, "ad:fhluqvV", long_opts, NULL)) != -1) { + while((c = getopt_long(argc, argv, "ad:fs:hluqvV", long_opts, NULL)) != -1) { switch(c) { case 'a': downall = 1; @@ -111,6 +113,11 @@ int main(int argc, char *argv[]) downissueno = atoi(optarg); option = 1; break; + case 's': + downsection = 1; + downsectionno = atoi(optarg); + option = 1; + break; case 'l': listissues = 1; option = 1; @@ -189,7 +196,21 @@ int main(int argc, char *argv[]) downloadissue(&options, issue[i], force); } else if(downissueno >= 0 && downissueno <= no_of_issues) - downloadissue(&options, issue[downissueno], force); + { + if(downsection && downsectionno >= 0 && downsectionno <= issue[downissueno]->no_of_sections) + { + char downdir[STR_MAX]; + sec * cursec; + strncpy(downdir, (char *) getissuedir(&options, issue[downissueno]), STR_MAX); + cursec = issue[downissueno]->section[downsectionno]; + + printf("Downloading %s to %s\n", cursec->title, downdir); + + downloadsection(&options, cursec, &downdir, force); + } + else + downloadissue(&options, issue[downissueno], force); + } } -- cgit v1.2.3