Advertisement
Guest User

VLC Now Playing Script for Irssi

a guest
May 6th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. sub get_tag {
  54.     my ($tag, @data) = @_;
  55.     my $match;
  56.     foreach my $line (@data) {
  57.         if (!$match) {
  58.             if ($line =~ /<$tag>(.*?)<\/$tag>/) {
  59.                 $match = $1;
  60.             }
  61.         }
  62.     }
  63.     return ($match);
  64. }
  65.  
  66. sub main {
  67.     my @data = get_data;
  68.     my $vlc = "\0037[\003VLC\0037]\003";
  69.     my $position = convert_seconds(get_tag('time', @data));
  70.     my $length = convert_seconds(get_tag('length', @data));
  71.     my $resolution = scrape('Resolution', @data);
  72.     my $output;
  73.     if (!$resolution) {
  74.         my $artist = scrape('artist', @data);
  75.         my $album = scrape('album', @data);
  76.         my $title = scrape('title', @data);
  77.         my $year = scrape('date', @data);
  78.         my $bitrate = scrape('Bitrate', @data);
  79.         if (!$bitrate) {
  80.             $bitrate = "[Lossless]";
  81.         }
  82.         else {
  83.             $bitrate = "@ " . $bitrate;
  84.         }
  85.         $output = sprintf("%s %s by %s from %s (%s) %s | %s/%s\n", $vlc,
  86.                                                            $title, $artist,
  87.                                                              $album, $year,
  88.                                                        $bitrate, $position,
  89.                                                                    $length);
  90.     }
  91.     else {
  92.         my $filename = scrape('filename', @data);
  93.         $output = sprintf("%s %s | %s | %s/%s\n", $vlc, $filename, $resolution,
  94.                                                             $position, $length);
  95.     }
  96.     Irssi::active_win->command("say $output");
  97. }
  98. Irssi::command_bind('np', 'main');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement