A variety of simple automation scripts and enhancements to Sway and i3.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.5KB

  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. # Dependencies
  5. use LWP::UserAgent; # Usually pre-installed, available on CPAN
  6. use XML::Hash::XS; # CPAN or libxml-hash-xs-perl on Debian
  7. # Feeds that should be used if no arguments are provided
  8. my @default_feeds = (
  9. # CBC FrontBurner
  10. "https://www.cbc.ca/podcasting/includes/frontburner.xml",
  11. # Majority Report AM Quickie
  12. "https://feeds.fans.fm/5883fe04-e11e-4578-a018-3b93ddfb5723.xml"
  13. );
  14. # Player command (must accept stream as STDIN)
  15. #my $player = "| mpv -";
  16. # OMXPlayer is the only one capable of playing without stutter on the pi
  17. # It requires FIFO to stream. For a player that does support STDIN, uncomment
  18. # above and delete the following 4 lines
  19. unless (-e "/tmp/fifo.mp3") {
  20. system("mkfifo /tmp/fifo.mp3");
  21. }
  22. my $player = "> /tmp/fifo.mp3 & omxplayer /tmp/fifo.mp3";
  23. # Multiple feeds can be given as CLI arguments instead
  24. my @feeds;
  25. if (scalar @ARGV) {
  26. foreach (@ARGV) {
  27. push @feeds, $_;
  28. }
  29. }
  30. # Only use default feeds if no CLI feeds were given
  31. unless (scalar @feeds) {
  32. @feeds = @default_feeds;
  33. }
  34. # Local function to get date pattern provided as 'pubDate'
  35. my $today = get_date();
  36. # Setup Webpage agent and XML parser
  37. my $ua = LWP::UserAgent->new();
  38. my $xml = XML::Hash::XS->new(utf8 => 0, encoding => 'utf-8');
  39. my @playlist;
  40. # Go through each feed
  41. foreach my $url (@feeds) {
  42. # Ensure that it's actual an XML link
  43. next unless $url =~ m#^https?://.*\.xml$#;
  44. # Ensure it is fetched okay
  45. my $raw = $ua->get("$url")->content() || next;
  46. # Ensure it is parsed okay
  47. my $xml_hash = $xml->xml2hash($raw) || next;
  48. # Collect all episodes published today
  49. foreach my $item (@{$xml_hash->{'channel'}->{'item'}}) {
  50. if ($item->{'pubDate'} =~ m/$today/) {
  51. # Add any found to playlist
  52. push @playlist, $item->{'enclosure'}->{'url'};
  53. }
  54. }
  55. }
  56. # Fetch each item and pass it to player via STDIN
  57. foreach (@playlist) {
  58. # Uncomment the following to print the media URL
  59. print $_ . "\n";
  60. system("curl -NL $_ $player");
  61. }
  62. unlink("/tmp/fifo.mp3");
  63. sub get_date
  64. {
  65. my ($second, $minute, $hour, $day, $month, $year) = localtime(time);
  66. my @months = (
  67. 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
  68. 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
  69. );
  70. return $day . ' ' . $months[$month] . ' ' . ($year+1900);
  71. }