about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2020-08-29 20:19:03 +0530
committerAndinus <andinus@nand.sh>2020-08-29 20:19:03 +0530
commit1f061ef0d9c5641fea39810f388d0ffc80a87e59 (patch)
tree1c10b6b139ae44753feaf0215c5a9ab41a3038c6
parent8aa6c2fd3c82a17dc3d1307e724e4e5a4a0a09ac (diff)
downloadpyxis-1f061ef0d9c5641fea39810f388d0ffc80a87e59.tar.gz
Add fetch feature
-rwxr-xr-xpyxis.pl67
1 files changed, 67 insertions, 0 deletions
diff --git a/pyxis.pl b/pyxis.pl
new file mode 100755
index 0000000..c45f3e6
--- /dev/null
+++ b/pyxis.pl
@@ -0,0 +1,67 @@
+#!/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);
+
+        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";
+}