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
|
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
use Path::Tiny;
use IPC::Run3;
my $fortune_dir = "$ENV{HOME}/fortunes";
my %fortunes = (
# mst quotes.
mst => "http://www.trout.me.uk/quotes.txt",
# All these are from rindolf's website.
sholmif => "https://www.shlomifish.org/humour/fortunes/shlomif",
"shlomif-fav"
=> "https://www.shlomifish.org/humour/fortunes/shlomif-fav",
"shlomif-factoids"
=> "https://www.shlomifish.org/humour/fortunes/shlomif-factoids",
"sholmif-email-sig"
=> "https://raw.githubusercontent.com/shlomif/shlomif-email-signature/master/shlomif-sig-quotes.txt",
# Quotes from the Joel on Software site.
# (http://www.joelonsoftware.com/)
"joel-on-software"
=> "https://www.shlomifish.org/humour/fortunes/joel-on-software",
# Quotes from the essays and writings of Paul Graham.
# (http://www.paulgraham.com/)
"paul-graham"
=> "https://www.shlomifish.org/humour/fortunes/paul-graham",
# “The Rules of Open-Source Programming”.
osp_rules => "https://www.shlomifish.org/humour/fortunes/osp_rules",
# Excerpts from the online Subversion folklore.
# (http://subversion.tigris.org/)
"subversion"
=> "https://www.shlomifish.org/humour/fortunes/subversion",
# A collection of conversations from Freenode’s #perl .
"sharp-perl"
=> "https://www.shlomifish.org/humour/fortunes/sharp-perl",
# A collection of conversations from Freenode’s ##programming .
"sharp-programming"
=> "https://www.shlomifish.org/humour/fortunes/sharp-programming",
# katspace quotes (ref: rindolf's website).
"katspace_sayings"
=> "http://katspace.com/fandom/quotes/sayings",
"katspace_more-sayings"
=> "http://katspace.com/fandom/quotes/kaijen",
"katspace_books"
=> "http://katspace.com/fandom/quotes/book",
"katspace_quotes"
=> "http://katspace.com/fandom/quotes/quotes",
# levonk quotes (had starred kirch's fortunes repo, checked
# profile & found this).
"levon"
=> "https://raw.githubusercontent.com/levonk/fortune/master/levonkquotes",
);
if ( $ARGV[0] ) {
if ($fortunes{ $ARGV[0] } or -e "$fortune_dir/$ARGV[0]") {
my $fortune_file = "$fortune_dir/$ARGV[0]";
random($fortune_file); }
elsif ( $ARGV[0] eq "latest") { get_kirch(); get_latest(); }
elsif ( $ARGV[0] eq "mirror") { get_kirch(); get_mirror(); }
elsif ( $ARGV[0] eq "ls") { run3[ "ls", $fortune_dir]; }
else { say "lyra: no such fortune"; }
} else { say "Usage: lyra <fortune>"; }
sub random {
my $fortune_file = shift @_;
my $file = path($fortune_file)->absolute;
my @fortunes = split/\n%\n/, $file->slurp;
say $fortunes[ rand @fortunes ]; # Print random fortune.
}
sub get_kirch {
path("/tmp")->mkpath;
run3 [qw(git -C /tmp clone https://github.com/JKirchartz/fortunes.git kirch)];
$? # We assume non-zero is an error.
? warn "[WARN] Cannot get kirch fortunes :: $?\n"
: say "Cloned kirch fortunes to /tmp/kirch";
foreach my $file (qw( README.md LICENSE Makefile )) {
my $file_t = "/tmp/kirch/$file";
unlink $file_t and say "$file_t deleted."
or warn "[WARN] Could not delete $file_t: $!\n";
}
system("rm -r /tmp/kirch/bin");
system("cp -r /tmp/kirch/* /home/andinus/fortunes/");
}
sub get_latest {
foreach my $fortune (sort keys %fortunes) {
ftp("$fortune_dir/$fortune", $fortunes{$fortune});
$? # We assume non-zero is an error.
? warn "[WARN] Failed to get $fortune :: $?\n"
: say "got $fortune";
}
}
sub get_mirror {
require HTTP::Simple;
# Ignore a warning, next line would've printed a warning.
no warnings 'once';
$HTTP::Simple::UA->verify_SSL(1);
foreach my $fortune (sort keys %fortunes) {
say "$fortune $fortunes{$fortune}";
my $status =
HTTP::Simple::getstore($fortunes{$fortune},
"$fortune_dir/$fortune");
warn "[WARN] Failed to fetch latest fortune\n"
unless HTTP::Simple::is_success($status);
}
}
sub ftp { run3 ["ftp", "-mvo", @_]; }
|