about summary refs log tree commit diff stats
path: root/pyxis.pl
blob: 45e523f26f3868970bf570a5c82fae8dfce48c11 (plain) (blame)
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
#!/usr/bin/perl

use strict;
use warnings;
use feature 'say';

use Path::Tiny;
use HTTP::Simple;
use Getopt::Long;

my %options;
GetOptions(
    \%options,
    qw{ verbose help }
) or die "Error in command line arguments\n";

if ($options{help}) {
    say "pyxis is a simple twtxt client.

Commands:
    fetch
        Fetch latest feeds.

Options:
    --verbose
        Verbose operation mode.
    --help
        Print this help.";
        exit 0;
}

# $feeds_dir will store user's feeds.
my $feeds_dir = $ENV{XDG_DATA_HOME} || "$ENV{HOME}/.local/share";
$feeds_dir .= "/pyxis";

# Create $feeds_dir.
path($feeds_dir)->mkpath;

# Config file for pyxis.
my $config_file = $ENV{XDG_CONFIG_HOME} || "$ENV{HOME}/.config";
$config_file .= "/pyxis.pl";

require "$config_file";
my %feeds = get_feeds();

my %dispatch = (
    fetch => sub {
        require HTTP::Tiny;
        my $http = HTTP::Tiny
            ->new(
                verify_SSL => 1,
                # default user-agent string if ending in space.
                # agent => "pyxis: https://andinus.nand.sh/pyxis ",
            );

        foreach my $feed (sort keys %feeds) {
            my $url = $feeds{$feed};
            my $file = "$feeds_dir/$feed";

            my $res = $http->mirror($url, $file);
            $res->{success}
                ? do {say "$feed updated" if $options{verbose}}
                : warn "failed to fetch $feed - $url\n"
        }
    },
);

if ($ARGV[0] and $dispatch{$ARGV[0]}) {
    $dispatch{$ARGV[0]}->();
} else {
    die "pyxis: no such option\n";
}