1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
|
/*
* Copyright 2006 Nick White
*
* This file is part of GetHT
*
* GetHT 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 2 of the License, or
* (at your option) any later version.
*
* GetHT 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 GetHT; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <string.h>
#include <sys/stat.h>
#include <curl/curl.h>
#include "version.h"
#include "issue.h"
#include "getht.h"
void show_iss_struct(iss ** issue, int no_of_issues);
void show_med_struct(med * issue);
void clearmed(med * cur_media);
void cleariss(iss * cur_issue);
void showusage();
med * findnewestmed(iss ** issue, int no_of_issues);
proxytype proxy_type; char proxy_addr[STR_MAX]; long proxy_port;
proxyauth proxy_auth;
char proxy_user[STR_MAX]; char proxy_pass[STR_MAX];
char issue_xml[STR_MAX]; char media_xml[STR_MAX]; char media_rev[STR_MAX];
CURL *main_curl_handle;
int main(int argc, char *argv[])
{
char getht_path[STR_MAX];
char save_path[STR_MAX];
/* Define & set up paths */
snprintf(getht_path,STR_MAX,"%s/.getht",getenv("HOME"));
if(!opendir(getht_path))
if(mkdir(getht_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH))
{
fprintf(stderr,"Cannot open/create directory %s",getht_path);
printf("Please enter the path of a directory to save settings in: ");
scanf("%s", getht_path);
}
snprintf(issue_xml,STR_MAX,"%s/%s",getht_path,ISS_XML_FILE);
snprintf(media_xml,STR_MAX, "%s/%s", getht_path, MED_XML_FILE);
snprintf(media_rev,STR_MAX,"%s/%s",getht_path,MED_REVGZ_FILE);
snprintf(save_path,STR_MAX,"%s/hinduism_today",getenv("HOME"));
int downall = 0, downlatest = 0;
int downallmedia = 0, downlatestmedia = 0;
int downissue = 0, downmedia = 0;
int force = 0, update = 0, showstr = 0;
proxy_type = NONE;
proxy_port = 0;
proxy_addr[0] = '\0';
proxy_auth = NOAUTH;
proxy_user[0] = '\0';
proxy_pass[0] = '\0';
if(loadconfig(getht_path, &save_path, &update) != 0)
writefreshconfig(getht_path, &save_path, &update);
if(!opendir(save_path))
if(mkdir(save_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH))
{
fprintf(stderr,"Cannot open/create directory %s",save_path);
printf("Please enter the path of a directory to save issues in: ");
scanf("%s", save_path);
updateconfig(getht_path, &save_path, NULL);
}
/* Parse command line options */
char c;
static struct option long_opts[] =
{
{"download-all", no_argument, 0, 'a'},
{"download-latest", no_argument, 0, 'd'},
{"download-all-media", no_argument, 0, 'm'},
{"download-latest-media", no_argument, 0, 'n'},
{"force", no_argument, 0, 'f'},
{"update", no_argument, 0, 'u'},
{"tocfile", required_argument, 0, 't'},
{"help", no_argument, 0, 'h'},
{"version", no_argument, 0, 'v'},
{0, 0, 0, 0}
};
while((c = getopt_long(argc, argv, "adfhmnsuvxt:", long_opts, NULL)) != -1) {
switch(c) {
case 'a':
downall = 1;
downissue = 1;
break;
case 'd':
downlatest = 1;
downissue = 1;
break;
case 'm':
downallmedia = 1;
downmedia = 1;
break;
case 'n':
downlatestmedia = 1;
downmedia = 1;
break;
case 'f':
force = 1;
break;
case 'u':
update = 1;
break;
case 's':
showstr = 1;
break;
case 't':
strcpy(issue_xml, strdup(optarg));
break;
case 'h':
showusage();
return 0;
break;
case 'v':
printf("GetHT version: %s\n",VERSION);
return 0;
break;
default:
fprintf(stderr,"Unknown option: %c\n",c);
}
}
main_curl_handle = curl_easy_init();
if(update)
{
if(update_contents_files(NULL, NULL))
fprintf(stderr,"Could not update contents files");
}
/* Parse TOC, filling issue structure */
iss **issue;
int no_of_issues = -1;
int latest_index = -1;
int i;
if(downissue || showstr)
{
issue = parsetoc(issue_xml, &no_of_issues, &latest_index);
if(showstr)
show_iss_struct(issue, no_of_issues);
if(latest_index == -1)
{
fprintf(stderr, "Error: Cannot ascertain latest issue. ");
fprintf(stderr, "Defaulting to first issue in contents file\n");
latest_index = 0;
}
if(downall)
{
for(i = 0; i < no_of_issues; i++)
downloadissue(NULL, save_path, issue[i], force);
}
if(downlatest)
downloadissue(NULL, save_path, issue[latest_index], force);
}
if(downmedia)
{
med * cur_media;
int newest;
issue = parsemedia(media_xml, issue, &no_of_issues);
if(downlatestmedia)
{
newest = findnewestiss(issue, no_of_issues);
for(i = 0; i <= issue[newest]->no_of_media; i++)
downloadmedia(NULL, save_path, &(issue[newest]->media[i]), force);
}
if(downallmedia)
{
for(i = 0; i <= no_of_issues; i++)
{
for(newest = 0; newest <= issue[i]->no_of_media; newest++)
downloadmedia(NULL, save_path, &(issue[i]->media[newest]), force);
}
}
}
if(no_of_issues > -1)
{
for(i = 0; i < no_of_issues; i++)
free(issue[i]);
free(issue);
}
/* Ensure curl cleans itself up */
curl_easy_cleanup(main_curl_handle);
return 0;
}
int update_contents_files(CURL *curl_handle, int temp)
/* Returns 0 on success, 1 on failure */
{
save_file(NULL, XML_TOC_URL, issue_xml);
/* see if current issue's media toc has already
been written to the xml, if not do so */
char isstitle[STR_MAX];
issdates date;
if(access(issue_xml, R_OK) == 0)
cur_identifiers(issue_xml, isstitle, &date);
else
return 1;
if(media_accounted_for(media_xml, &date))
{
save_file(curl_handle, MEDIA_TOC_URL, media_rev);
med temp_med[MED_NO];
clearmed(temp_med);
/* BUG: for no good reason, this resets title[] too...
* Until we can find why, just get the title again */
cur_identifiers(issue_xml, isstitle, &date);
int med_no = -1;
parsemediagz(media_rev, temp_med, &med_no);
/* BUG: this blanks title too... strange
* Until we can find why, just get the title again */
cur_identifiers(issue_xml, isstitle, &date);
unlink(media_rev);
addmediaissue(media_xml, isstitle, &date, temp_med, med_no);
}
return 0;
}
void show_iss_struct(iss ** issue, int no_of_issues)
/* Prints issue information */
{
int iss_no, sec_no, it_no;
printf("%i Issues\n",no_of_issues);
for(iss_no=0;iss_no<no_of_issues;iss_no++)
{
printf("-Issue %i-\n", (iss_no+1));
printf("Title:\t'%s'\n", issue[iss_no]->title);
printf("Preview URI:\t'%s'\n", issue[iss_no]->preview_uri);
printf("Months:\t'%i' - '%i'\n",issue[iss_no]->date.firstmonth,issue[iss_no]->date.lastmonth);
printf("Year:\t'%i'\n",issue[iss_no]->date.year);
printf("Number of Sections:\t'%i'\n",issue[iss_no]->no_of_sections);
printf("\t-Cover-\n");
printf("\tTitle:\t'%s'\n", issue[iss_no]->cover.title);
printf("\tURI:\t'%s'\n", issue[iss_no]->cover.uri);
for(sec_no=0; sec_no < (issue[iss_no]->no_of_sections); sec_no++)
{
printf("\t-Section %i-\n", (sec_no+1));
printf("\tTitle:\t'%s'\n", issue[iss_no]->section[sec_no].title);
printf("\tURI:\t'%s'\n", issue[iss_no]->section[sec_no].uri);
printf("\tNo. of Items:\t'%i'\n", issue[iss_no]->section[sec_no].no_of_items);
for(it_no=0; it_no < issue[iss_no]->section[sec_no].no_of_items; it_no++)
{
printf("\t\t-Item-\n");
printf("\t\tTitle:\t'%s'\n",issue[iss_no]->section[sec_no].item[it_no].title);
printf("\t\tFirst page:\t'%i'",issue[iss_no]->section[sec_no].item[it_no].firstpage);
printf("\tLast page:\t'%i'\n",issue[iss_no]->section[sec_no].item[it_no].lastpage);
}
it_no = 0;
}
sec_no = 0;
}
}
void show_med_struct(med * cur_media)
/* Prints media information */
{
int tmp;
for(tmp=0;tmp<MED_NO;cur_media++,tmp++)
{
printf("--Media item--\n");
printf("uri: '%s'\n", cur_media->uri);
printf("title: '%s'\n", cur_media->title);
printf("comment: '%s'\n", cur_media->comment);
printf("preview_uri: '%s'\n", cur_media->preview_uri);
printf("size: '%i'\n", cur_media->size);
printf("\n");
}
}
void cleariss(iss * cur_issue)
/* turns off exist flags for all issue structures */
{
int tmp;
for(tmp=0; tmp<MAX_ISS; cur_issue++,tmp++)
{
cur_issue->preview_uri[0] = '\0';
cur_issue->title[0] = '\0';
cur_issue->size = 0;
cur_issue->no_of_sections = 0;
cur_issue->no_of_media = 0;
clearmed(cur_issue->media);
}
}
void clearmed(med * cur_media)
/* clears all members of media arrays */
{
int tmp;
for(tmp=0; tmp<=MED_NO; cur_media++,tmp++)
{
cur_media->uri[0] = '\0';
cur_media->title[0] = '\0';
cur_media->comment[0] = '\0';
cur_media->preview_uri[0] = '\0';
cur_media->size = 0;
}
}
void clearsec(sec * cur_section)
/* clears all members of section arrays */
{
int tmp;
for(tmp=0; tmp<=SEC_NO; cur_section++,tmp++)
{
cur_section->uri[0] = '\0';
cur_section->title[0] = '\0';
cur_section->size = 0;
cur_section->number = 0;
cur_section->no_of_items = 0;
}
}
int findnewestiss(iss ** issue, int no_of_issues)
/* returns newest issue indice */
{
iss * tmp_issue; issdates newest;
int new_iss;
new_iss = -1;
newest.year = 0; newest.firstmonth = 0; newest.lastmonth = 0;
int i;
for(i = 0; i <= no_of_issues; i++)
{
if(issue[i]->date.year > newest.year ||
(issue[i]->date.year == newest.year && issue[i]->date.firstmonth > newest.firstmonth))
{
newest.year = issue[i]->date.year;
newest.firstmonth = issue[i]->date.firstmonth;
newest.lastmonth = issue[i]->date.lastmonth;
new_iss = i;
}
}
return new_iss;
}
void showusage()
{
printf("Usage: getht -u -a -d -m -n -f [-t tocfile] -h -v\n");
printf("-a | --download-all Download all issues\n");
printf("-d | --download-latest Download latest issue\n");
printf("-m | --download-all-media Download all media\n");
printf("-n | --download-latest-media Download latest issue's media\n");
printf("-f | --force Force redownloading of existent issues\n");
printf("-u | --update Update contents files\n");
printf("-t | --tocfile file Use alternative contents xml file\n");
printf("-h | --help Print this help message\n");
printf("-v | --version Print version information\n");
printf(" ---DEBUGGING--\n");
printf("-s Print structure information\n");
}
|