This repository has been archived by the owner on Oct 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
magbot.perl
executable file
·888 lines (715 loc) · 20 KB
/
magbot.perl
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
#!/usr/bin/env perl
use v5.10;
use strict;
use warnings;
binmode STDOUT, ':utf8';
=head1 NAME
magbot - For fetching media from jw.org feeds
=head1 SYNOPSIS
> magbot
# downloads magazines as specified in the $HOME/.magbot configuration file
> magbot -d
# starts in daemon mode and checks for new downloads each day
> magbot -c
# checks for new items in feeds in the $HOME/.magbot configuration file reports
# the results on the screen and exits
> magbot -c g E MP3
# checks the current English Awake! feed and exits
> magbot g E MP3
# downloads the current English Awake! magazines in MP3 format
> magbot w J PDF
# downloads the current Japanese Watchtower magazines in PDF format
> magbot w AL PDF 2012-08
# downloads August 2012 Albanian Watchtower magazine in PDF format
=head1 DESCRIPTION
For fetching media from jw.org from the command-line prints
Gtk notifications, to standard output, and to Syslog.
=head1 DEPENDENCIES
YAML
XML::LibXML
Gtk2::Notify (optional)
HTTP::Tiny
=head1 AUTHOR
Delon Newman <[email protected]>
=cut
use YAML;
use HTTP::Tiny;
use XML::LibXML;
use Getopt::Long;
use Data::Dump qw{ dump };
use File::Spec;
use File::Basename;
#
# Settings
#
my $VERSION = '0.7.0';
my $APP_NAME = 'MagBot';
my $ROOT_URL = 'http://www.jw.org/apps/index.xjp';
my $FILE_URL = 'http://download.jw.org/files/media_magazines';
my $MAGS = config_or_defaults('mags');
my $DIR = config_or_defaults('dir');
my $VERBOSE = 0;
my $HOME = $ENV{HOME} || "$ENV{HOMEDRIVE}$ENV{HOMEPATH}";
# Valid mags, languages, formats
my %MAGS = (
g => 'Awake!',
w => 'Watchtower',
wp => 'Watchtower (Public Edition)'
);
my %MAGS_DAYS = (
g => '',
w => '15',
wp => '01',
);
# map valid formats to basic 'type'
my %FORMATS = (
MP3 => 'audio',
M4B => 'audio',
AAC => 'audio',
EPUB => 'pub',
PDF => 'pub'
);
my %FORMATS_EXTENSIONS = (
MP3 => 'mp3.zip',
M4B => 'm4b',
AAC => 'm4b',
EPUB => 'epub',
PDF => 'pdf'
);
my %FORMAT_EQUIVALENTS = (
AAC => 'M4B'
);
my %LANGUAGES = (
AP => 'Aymara',
B => 'čeština',
D => 'Dansk',
X => 'Deutsch',
ST => 'eesti',
E => 'English',
S => 'español',
F => 'Français',
C => 'hrvatski',
YW => 'Ikinyarwanda',
IN => 'Indonesia',
I => 'italiano',
SW => 'Kiswahili',
LT => 'Latviešu',
L => 'lietuvių',
H => 'magyar',
MG => 'Malagasy',
MT => 'Malti',
O => 'Nederlands',
N => 'Norsk',
PA => 'Papiamentu (Kòrsou)',
P => 'polski',
T => 'Português',
QUB => 'Quechua (Bolivia)',
M => 'Română',
AL => 'shqip',
V => 'slovenčina',
SV => 'slovenščina',
FI => 'Suomi',
Z => 'Svenska',
TK => 'Türkçe',
G => 'Ελληνική',
BL => 'български',
U => 'русский',
SB => 'српски',
K => 'українська',
GE => 'ქართული',
REA => 'Հայերեն',
TL => 'தமிழ்',
MY => 'മലയാളം',
SI => 'ไทย',
J => '日本語',
CHS => '汉语简化字',
CH => '漢語繁體字',
KO => '한국어',
);
# some important functions
BEGIN {
#
# Print messages with Gtk2::Notify, to syslog, and to STDOUT
# any of which are available.
#
my $gtk;
eval {
require Gtk2::Notify;
Gtk2::Notify->import(-init, basename $0);
$gtk = 1;
};
my $syslog;
sub notify {
if ( $gtk ) {
Gtk2::Notify->new($APP_NAME . ' says:', join('', @_))->show;
}
print @_, "\n"
}
#
# Error Reporting
#
sub notify_fatal {
if ( $gtk ) {
Gtk2::Notify->new($APP_NAME . ' says:', join('', @_))->show;
}
print STDERR @_, "\n";
}
#
# Consise, cross-platform path building
#
sub path { File::Spec->join(@_) }
# replace but don't destory
sub replace($$$) {
my ($str, $pat, $replace) = @_;
$str =~ s/$pat/$replace/;
$str;
}
# replace but don't destory
sub greplace($$$) {
my ($str, $pat, $replace) = @_;
$str =~ s/$pat/$replace/g;
$str;
}
sub fatal($) {
print STDERR @_, "\n";
}
}
#
# Constants
#
use constant HOME => $^O =~ /win32/i ? "$ENV{HOMEDRIVE}\\$ENV{HOMEPATH}" : $ENV{HOME};
use constant CONFIG_FILE => path(HOME, '.magbot' => 'config');
use constant LOG_FILE => path(HOME, '.magbot' => 'log');
use constant DEFAULTS => {
mags => {
w => { E => [qw{ EPUB M4B }] },
wp => { E => [qw{ EPUB M4B }] },
g => { E => [qw{ EPUB M4B }] }
},
dir => {
audio => path(HOME, 'Podcasts'),
pub => path(HOME, 'Reading')
}
};
sub log {
open my $l, '>>', LOG_FILE or
die "Can't append to file ", LOG_FILE, ": $!";
say @_ if $VERBOSE;
say $l @_;
}
#
# Functions for accessing configuration
#
# read from configuration file or from defaults
sub config_or_defaults {
if ( wantarray ) {
my $ref = config(@_) || defaults(@_);
if ( ref $ref eq 'HASH' || ref $ref eq 'ARRAY' ) { @{$ref} }
else { ($ref) }
}
else {
config(@_) || defaults(@_);
}
}
# read from configuration file
{
my $config;
sub config {
$config //= do {
my $dir = dirname CONFIG_FILE;
mkdir $dir unless -e $dir;
my $f = CONFIG_FILE;
if ( -e $f && (my $yaml = YAML::LoadFile($f)) ) { $yaml }
else {
open my $fh, '>', $f or die "can't write to $f";
print $fh YAML::Dump(DEFAULTS);
close $fh;
YAML::LoadFile($f);
}
};
if ( wantarray ) { @{read_config($config, @_)} }
else { read_config($config, @_) }
}
}
# read from defaults
sub defaults {
if ( wantarray ) { @{read_config(DEFAULTS, @_)} }
else { read_config(DEFAULTS, @_) }
}
# read from configuration data structure
sub read_config {
my ($config, @attrs) = @_;
if ( @attrs == 1 ) {
my $ref = $config->{$attrs[0]};
if ( wantarray ) {
if ( ref $ref eq 'HASH' ) { %{$ref} }
elsif ( ref $ref eq 'ARRAY' ) { @{$ref} }
else { ($ref) }
}
else {
$ref
}
}
else {
read_config($config->{shift @attrs}, @attrs);
}
}
#
# Some monkey business for IO
#
# get content from HTTP
sub get {
my $res = eval { HTTP::Tiny->new->get(@_) };
if ( $@ ) {
say "Can't fetch '", @_, '\' ', $@;
notify_fatal "Can't download feed, exiting.";
}
if ( $res->{content} =~ qr{File not found.} ) {
notify_fatal "When fetching ", @_, ":\n ", $res->{content};
}
else {
$res->{content};
}
}
# get and store content from HTTP to a file
sub getstore {
my ($url, $file) = @_;
my $content = get($url); # allows to die before writing file
open my $fh, '>', $file or die "can't write to $file: $!";
print $fh $content;
}
#
# For parsing feeds
#
# Feed constructor
sub Feed {
my %feed = @_;
$feed{title} // die "title is required";
$feed{url} // die "url is required";
$feed{items} // notify_fatal "Didn't find any items in feed";
bless \%feed, 'Feed';
}
# Item constructor
sub Item {
my %item = @_;
$item{link} // die "link is required";
$item{title} // die "title is required";
$item{date} // die "date is required";
$item{feed} // die "feed is required";
$item{format} = do {
my ($f, $ext) = split(/\./, basename $item{link});
my ($format) = grep { $FORMATS_EXTENSIONS{$_} eq $ext } keys %FORMATS_EXTENSIONS;
$format;
};
bless \%item, 'Item';
}
# trys to always returns as much of a workable data structure as possible
#
# $feed = {
# title => '',
# description => '',
# language => '',
# url => '',
# image => '',
# items => [
# {
# title => '',
# link => '',
# date => '',
# feed => \$feed,
# },
# ...
# ]
# }
#
sub parse_feed {
my ($xml) = @_;
my %feed = ();
my $dom = XML::LibXML->load_xml(string => $xml);
my $chan = $dom->getElementsByTagName('channel')->get_node(0);
sub child {
my ($parent, %args) = @_;
my @tags = @{$args{tags}} if $args{tags};
my $tag = $args{tag} // shift @tags;
my $nodes = $parent->getChildrenByTagName($tag);
if ( $nodes->size > 1 ) {
my @elems = $nodes->get_nodelist;
wantarray ? @elems : \@elems;
}
elsif ( $nodes->size == 1 ) {
my $elem = $nodes->get_node(0);
if ( $args{attr} ) { $elem->getAttribute($args{attr}) }
elsif ( !@tags ) { $elem->textContent }
else { child($elem, tags => \@tags) }
}
else {
undef;
}
}
sub items {
my ($feed, @elems) = @_;
return undef if (@elems == 1 && grep !defined $_, @elems);
my @items = map {
Item(title => child($_, tag => 'title'),
link => child($_, tag => 'link'),
date => child($_, tag => 'pubDate'),
feed => $feed);
} @elems;
\@items;
}
$feed{title} = child $chan, tag => 'title';
$feed{description} = child $chan, tag => 'description';
$feed{language} = child $chan, tag => 'language';
$feed{image} = child $chan, tags => ['image', 'url'];
$feed{url} = child $chan, tag => 'atom:link', attr => 'href';
$feed{items} = items(\%feed, child($chan, tag => 'item'));
(my $pub, $feed{format}) = split ' ', $feed{description};
$pub =~ /([a-z]+)([A-Z]+)/;
($feed{mag_code}, $feed{lang}) = ($1, $2);
$feed{mag} = Mag($feed{mag_code}, $feed{lang}, $feed{format});
Feed(%feed);
}
#
# Some functions for accessing directory structure
#
sub feed_dir {
my $feed = shift;
$feed->{dir} //= do {
my $dir = greplace $feed->{title}, qr/(?:JW: )|[:\(\)]/ => '';
path(root_dir($feed) => $dir);
};
$feed->{dir};
}
sub root_dir {
my $arg = shift;
my $type = do {
if ( $arg && ref $arg && $arg->{format} ) {
$FORMATS{$arg->{format}};
}
else {
$FORMATS{$arg};
}
} // die 'cannot find root directory for: ', $arg;
if ( ref $DIR eq 'HASH' ) {
if ( ref $DIR->{$type} eq 'ARRAY' ) {
if ( -e $DIR->{$type}->[0] ) { $DIR->{$type}->[0] }
else { $DIR->{$type}->[1] }
}
else { $DIR->{$type} }
}
else { $DIR }
}
sub issue_dir {
my ($item, $feed_dir) = @_;
my $dir = greplace $item->{date}, qr/( \d{2}:\d{2}:\d{2} \w{3})|[,:]/ => '';
path($feed_dir => $dir);
}
sub item_dir {
my ($item) = @_;
my $issue_dir = issue_dir $item => feed_dir($item->{feed});
$item->{dir} //= path($issue_dir => item_file($item));
$item->{dir}
}
sub item_file {
my ($item) = @_;
$item->{file} //= basename $item->{link} if $item->{link};
$item->{file};
}
#
# Top-Level Functions
#
sub doeach($$) {
my ($urls, $fn, @args) = @_;
sub download($$@) {
my ($url, $fn, @args) = @_;
$fn->($url, @args);
}
sub worker {
my ($fn, $q, @args) = @_;
my $kid;
while ( my $url = $q->dequeue ) {
$kid = download $url => $fn, @args;
last if !$kid;
}
if ( threads->list > 1 ) {
my @threads = threads->list;
for my $t (threads->list) {
next if $t->tid == threads->tid;
next if $t->is_detached;
eval { $t->join };
if ( $@ ) { &log($@) }
}
}
}
#if ( @$urls > 1 ) {
# my $q = Thread::Queue->new(@$urls, undef);
# worker($fn, $q, @args);
#}
#else {
download($urls->[0] => $fn, @args); #->join;
#}
}
sub find_new {
map { $_->[1] }
grep { !$_->[0] }
map {
map {
[ -e item_dir($_), $_ ]
} @{$_->{items}}
} @_;
}
sub has_items {
my ($feed) = @_;
if ( $feed->{items} ) { 1 }
else {
notify "No items found for ", $feed->{title};
0;
}
}
sub get_new_items($$) {
my ($urls, $fn) = @_;
doeach $urls => sub {
my ($url) = @_;
my $content = get $url;
my $feed = parse_feed($content);
if ( has_items $feed ) {
my @items = find_new $feed;
$fn->($feed->{mag}, @items);
}
};
}
sub download_media {
my @urls = @_;
get_new_items [@urls] => sub {
my ($mag, @items) = @_;
if ( @items == 0 ) {
notify_fatal report_mags($mag), "\n", ' ' x 4, "No new items found.\n";
}
else {
notify report_mags($mag),
"\n", ' ' x 4,
int(@items), " items found, downloading...\n";
}
doeach [@items] => sub {
my ($item) = @_;
my $root_dir = root_dir $item->{format};
mkdir $root_dir unless -e $root_dir;
my $feed_dir = feed_dir $item->{feed};
mkdir $feed_dir unless -e $feed_dir;
my $issue_dir = issue_dir $item, $feed_dir;
mkdir $issue_dir unless -e $issue_dir;
my $path = item_dir $item;
if ( $item->{link} ) {
getstore($item->{link}, $path) unless -e $path;
say " OK - downloaded '", $item->{title}, "' to \n --> ", $path, "\n";
}
};
};
notify "Okay the work is complete! ;-)";
}
# Yaay!! Gen them URLs
sub gen_urls {
my (@mags) = @_;
map {
my ($mag, $lang, $format) = ($_->{mag}, $_->{lang}, $_->{format});
my %types = (
audio => 'sFFZRQVNZNT',
pub => 'sFFCsVrGZNT'
);
my $type = $FORMATS{$format};
$format = $FORMAT_EQUIVALENTS{$format} if $FORMAT_EQUIVALENTS{$format};
"$ROOT_URL?option=$types{$type}&rln=$lang&rmn=$mag&rfm=$format";
} @mags;
}
sub parse_date {
local ($_) = @_;
# pad month with zero
my $m = sub {
local ($_) = @_;
if ( /\d{2,2}/ ) { $_ }
else { "0$_" }
};
# YYYY-MM
if ( /(\d\d\d\d)-(\d{1,2})/ ) { ($1, $m->($2)) }
# MM/YYYY
elsif ( /(\d{1,2})\/(\d\d\d\d)/ ) { ($2, $m->($1)) }
# MM
elsif ( /(\d{1,2})/ ) {
my ($s, $min, $h, $d, $mon, $year) = localtime;
($year + 1900, $m->($1))
}
else { @_ }
}
sub gen_file_url {
my ($m) = @_;
my $ext = $FORMATS_EXTENSIONS{$m->{format}} //
notify_fatal "'", $m->{format}, "' is not a valid format";
my ($year, $month) = parse_date($m->{date});
sprintf "%s/%s_%s_%s%s%s.%s",
$FILE_URL,
$m->{mag},
$m->{lang},
$year,
$month,
$MAGS_DAYS{$m->{mag}},
$ext;
}
# a higer order function for creating lists composed of
# magazine components ($mag, $language, and $format)
# from magazine data structures
sub magmap($$) {
my ($mags, $fn) = @_;
my @mags = keys %$mags;
map {
my $mag = $_;
my @langs = keys %{$mags->{$mag}};
map {
my $lang = $_;
my @formats = @{$mags->{$mag}->{$lang}};
map {
my $format = $_;
$fn->($mag, $lang, $format);
} @formats
} @langs
} @mags
}
# Mag constructor
sub Mag {
my ($mag, $lang, $format, $date) = @_;
$mag // fatal "magazine code is required";
fatal "'$mag' is an invalid magazine" unless $MAGS{$mag};
$lang // fatal "language is required";
fatal "'$lang' is an invalid language" unless $LANGUAGES{$lang};
$format // fatal "format is required";
fatal "'$format' is an invalid format" unless $FORMATS{$format};
bless { mag => $mag,
lang => $lang,
format => $format,
date => $date }, 'Mag';
}
sub get_mags {
my @args = @_;
if ( @args ) {
if ( !(my @mags = Mag(@args)) ) {
notify_fatal "Need to specify magazine, language, and format to download.";
}
else { @mags }
}
else {
magmap $MAGS => sub { Mag(@_) };
}
}
sub report_mags {
my (@mags) = @_;
join "\n", map {
my ($m, $l, $f) = ($_->{mag}, $_->{lang}, $_->{format});
" $LANGUAGES{$l} $MAGS{$m} in $f format";
} @mags
}
sub daemonize_or_run($$) {
my ($cond, $fn) = @_;
if ( $cond ) {
my $int = config_or_defaults('check-interval');
#AE::timer 0, $int => sub {
$fn->();
#};
}
else { $fn->() }
}
#
# Commands
#
# And now the main event!!
sub main {
my ($daemonize, @args) = @_;
daemonize_or_run $daemonize => sub {
if ( @args < 4 ) {
my @mags = get_mags(@args);
notify "Checking for new items and downloading...";
download_media gen_urls(@mags);
}
else {
my $mag = Mag(@args);
my $url = gen_file_url($mag);
my $dir = root_dir($mag->{format});
my $rpt = report_mags($mag);
my $path = File::Spec->join($dir, basename $url);
if ( -e $path ) { notify "$path already exists." }
else {
notify "Downloading:\n", $rpt,
"\n from: ", $url,
"\n to: ", $path, "\n";
getstore($url => $path);
notify "download complete :-)";
}
}
};
exit 0;
}
sub check {
my ($daemonize, @args) = @_;
my @mags = get_mags(@args);
notify "Checking for new items...";
daemonize_or_run $daemonize => sub {
get_new_items [gen_urls @mags] => sub {
my ($mag, @items) = @_;
if ( @items ) {
notify report_mags($mag), ":\n",
join("\n", map { " " x 4 . $_->{title} } @items), "\n";
}
else {
notify report_mags($mag), ":\n",
" " x 4, "No new items found.\n";
}
};
};
}
sub list {
say "I'm currently configured to watch";
say report_mags(mags_from_config($MAGS));
exit 0;
}
sub help {
say qq{
$APP_NAME $VERSION
Usage: $0 [OPTIONS] [MAG CODE] [LANGUAGE CODE] [FORMAT]
Options:
--check, -c
check for new feed items as sepecified by arguments
or if none are present by configuration in
$HOME/.magbot, report and exit
--list, -l
list mags that are set to be downloaded in
configuration file
--verbose, -v
flag verbose mode
--daemonize, -d (not working)
run as a backgroud process daily
--help, -h
display this message
Other arguments
MAG CODE:
Used to indicate which magazine to download valid
values are 'w' (Watchtower), 'g' (Awake!), and 'wp'
(Watchtower - Public Edition).
LANGUAGE CODE:
Used to indicate which language edition of the
specified magazine should be downloaded.
FORMAT:
Used to indicate which file format to download the
magazine in. Valid values are 'MP3', 'M4B', 'AAC',
'EPUB', and 'PDF'.\n};
exit 0;
}
if ( __FILE__ eq $0 ) {
my $daemonize = 0;
GetOptions(
'daemonize|d' => sub { $daemonize = 1 }, # run daily
'help|h' => sub { help() },
'check|c' => sub { check($daemonize, @ARGV); exit 0 },
'list|l' => sub { list() },
'verbose|v' => sub { $VERBOSE = 1 }
);
main($daemonize, @ARGV);
}
1;