Advertisement
Guest User

VLC Now Playing Script for Irssi

a guest
May 6th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.87 KB | None | 0 0
  1. #! /usr/bin/env perl -w
  2.  
  3. use strict;
  4. use vars qw($VERSION %IRSSI);
  5. use Irssi;
  6.  
  7. $VERSION = '0.1';
  8. %IRSSI = (
  9.     authors     => 'rebirth',
  10.     name        => 'VLC Now Playing Script',
  11.     description => 'Scrape basic information from VLC web interface',
  12.     license     => 'GPLv3',
  13.     changed     => 'Fri May 06 20:58 EST 2016'
  14. );
  15.  
  16. sub get_data {
  17.     # you will need to enable web interface of VLC first, and set a password
  18.     my $url = "http://localhost:8080/requests/status.xml";
  19.     my $pw = ""; #!# YOU MUST ENTER YOUR PASSWORD HERE OR THE SCRIPT WON'T WORK #!#
  20.     return(`wget -qO- $url --user= --password=$pw`);
  21. }
  22.  
  23. sub scrape {
  24.     my ($tag, @data) = @_;
  25.     my $match;
  26.     foreach my $line (@data) {
  27.     $line =~ s/&/&/g;
  28.         $line =~ s/'/'/g;
  29.         if (!$match) {
  30.             if ($line =~ /<info name='$tag'>(.*?)<\/info>/) {
  31.                 $match = $1;
  32.             }            
  33.         }
  34.     }
  35.     return($match);
  36. }
  37.  
  38. sub convert_seconds {
  39.     my $time = $_[0];
  40.     my $hours = $time / 60 / 60;
  41.     my $minutes = $time / 60 % 60;
  42.     my $seconds = $time % 60;
  43.  
  44.     if ($hours >= 1) {
  45.         return(sprintf("%d:%02d:%02d", $hours, $minutes, $seconds));
  46.     }
  47.  
  48.     else {
  49.         return(sprintf("%02d:%02d", $minutes, $seconds));
  50.     }
  51.  
  52. }
  53.  
  54. sub get_tag {
  55.     my ($tag, @data) = @_;
  56.     my $match;
  57.     foreach my $line (@data) {
  58.         if (!$match) {
  59.             if ($line =~ /<$tag>(.*?)<\/$tag>/) {
  60.                 $match = $1;
  61.             }
  62.         }
  63.     }
  64.     return ($match);
  65. }
  66.  
  67. sub main {
  68.     my @data = get_data;
  69.     my $vlc = "\0037[\003VLC\0037]\003";
  70.     my $position = convert_seconds(get_tag('time', @data));
  71.     my $length = convert_seconds(get_tag('length', @data));
  72.     my $resolution = scrape('Resolution', @data);
  73.     my $output;
  74.     if (!$resolution) {
  75.         my $artist = scrape('artist', @data);
  76.         my $album = scrape('album', @data);
  77.         my $title = scrape('title', @data);
  78.         my $year = scrape('date', @data);
  79.         my $bitrate = scrape('Bitrate', @data);
  80.         if (!$bitrate) {
  81.             $bitrate = "[Lossless]";
  82.         }
  83.         else {
  84.             $bitrate = "@ " . $bitrate;
  85.         }
  86.         $output = sprintf("%s %s by %s from %s (%s) %s | %s/%s\n", $vlc,
  87.                                                            $title, $artist,
  88.                                                              $album, $year,
  89.                                                        $bitrate, $position,
  90.                                                                    $length);
  91.     }
  92.     else {
  93.         my $filename = scrape('filename', @data);
  94.         $output = sprintf("%s %s | %s | %s/%s\n", $vlc, $filename, $resolution,
  95.                                                             $position, $length);
  96.     }
  97.     Irssi::active_win->command("say $output");
  98. }
  99. Irssi::command_bind('np', 'main');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement