plexMonitor-json.pl 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use LWP::Simple;
  5. use File::Basename;
  6. use Time::HiRes qw( sleep );
  7. use File::Temp qw( tempfile );
  8. use URI::Escape;
  9. # Prototypes
  10. sub xbmcHTTP($);
  11. # App config
  12. my $TEMP_DIR = `getconf DARWIN_USER_TEMP_DIR`;
  13. chomp($TEMP_DIR);
  14. my $DATA_DIR = $TEMP_DIR . '/plexMonitor';
  15. # xbmcHTTP commands
  16. my %CMDS = (
  17. 'PLAYING' => 'GetCurrentlyPlaying',
  18. 'GUI' => 'GetGuiStatus'
  19. );
  20. # Debug
  21. my $DEBUG = 0;
  22. if ($ENV{'DEBUG'}) {
  23. $DEBUG = 1;
  24. }
  25. # Command-line arguments
  26. my ($DELAY) = @ARGV;
  27. if (!$DELAY) {
  28. $DELAY = 0;
  29. }
  30. # Sanity check
  31. if (!-d $TEMP_DIR) {
  32. die("Bad config\n");
  33. }
  34. # Mode
  35. my $MODE = 'PLAYING';
  36. if (basename($0) =~ /GUI/i) {
  37. $MODE = 'GUI';
  38. }
  39. # Add the data directory as needed
  40. if (!-d $DATA_DIR) {
  41. mkdir($DATA_DIR);
  42. }
  43. # Build our file name
  44. my $OUT_FILE = $DATA_DIR . '/' . $MODE;
  45. # Loop forever (unless no delay is set)
  46. my $data = '';
  47. my $dataLast = '';
  48. do {
  49. # Run the monitor command
  50. my $changed = 0;
  51. $dataLast = $data;
  52. $data = xbmcHTTP($CMDS{$MODE});
  53. # Compare this data set to the last
  54. if ($data ne $dataLast) {
  55. $changed = 1;
  56. if ($DEBUG) {
  57. print STDERR "Change detected in data:\n" . $data . "\n";
  58. }
  59. }
  60. # Ensure the output file exists
  61. if (!-r $OUT_FILE) {
  62. $changed = 1;
  63. }
  64. # If anything changed, save the data to disk
  65. if ($changed) {
  66. my ($fh, $tmp) = tempfile($OUT_FILE . '.XXXXXXXX', 'UNLINK' => 0);
  67. print $fh $data;
  68. close($fh);
  69. rename($tmp, $OUT_FILE);
  70. }
  71. # Delay and loop
  72. sleep($DELAY);
  73. } until ($DELAY == 0);
  74. # Exit cleanly
  75. exit(0);
  76. sub xbmcHTTP($) {
  77. my ($cmd) = @_;
  78. #$cmd = '{"jsonrpc":"2.0","method":"Player.GetActivePlayers","id":1}';
  79. #$cmd = '{"jsonrpc":"2.0","method":"Player.GetProperties","id":1,"params":{"playerid": 1,"properties": ["time"]}}';
  80. $cmd = '{"jsonrpc": "2.0", "method": "Player.GetItem", "params": { "properties": ["title", "album", "artist", "season", "episode", "duration", "showtitle", "tvshowid", "thumbnail", "file", "fanart", "streamdetails"], "playerid": 1 }, "id": "VideoGetItem"}';
  81. # Build the command
  82. my $url = 'http://localhost:3005/jsonrpc?request=' . uri_escape($cmd);
  83. if ($DEBUG) {
  84. print STDERR 'Command: ' . $cmd . "\n\t" . $url . "\n";
  85. }
  86. # Send the request
  87. my $data = get($url);
  88. if (!defined($data)) {
  89. if ($DEBUG) {
  90. print STDERR "No data returned from HTTP call\n";
  91. }
  92. $data = '';
  93. }
  94. return $data;
  95. }