plexMonitor.pl 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. use JSON;
  10. # Prototypes
  11. sub buildCmd($$);
  12. sub xbmcJSON($$);
  13. sub printHash($$);
  14. # App config
  15. my $BASE_URL = 'http://localhost:3005/jsonrpc?request=';
  16. my $TEMP_DIR = `getconf DARWIN_USER_TEMP_DIR`;
  17. chomp($TEMP_DIR);
  18. my $DATA_DIR = $TEMP_DIR . '/plexMonitor';
  19. # Debug
  20. my $DEBUG = 0;
  21. if ($ENV{'DEBUG'}) {
  22. $DEBUG = 1;
  23. }
  24. my $JSON_DEBUG = 0;
  25. if ($ENV{'JSON_DEBUG'}) {
  26. $JSON_DEBUG = 1;
  27. }
  28. # Command-line arguments
  29. my ($DELAY) = @ARGV;
  30. if (!$DELAY) {
  31. $DELAY = 0;
  32. }
  33. # Sanity check
  34. if (!-d $TEMP_DIR) {
  35. die("Bad config\n");
  36. }
  37. # Add the data directory as needed
  38. if (!-d $DATA_DIR) {
  39. mkdir($DATA_DIR);
  40. }
  41. # Build our output file name
  42. my $OUT_FILE = $DATA_DIR . '/PLAYING';
  43. # Loop forever (unless no delay is set)
  44. my %data = ();
  45. my %dataLast = ();
  46. do {
  47. # Only update the file on a change
  48. my $changed = 0;
  49. %dataLast = %data;
  50. %data = (
  51. 'playing' => 0,
  52. 'playerid' => -1,
  53. 'time' => 0,
  54. 'title' => '',
  55. 'season', => '',
  56. 'episode', => '',
  57. 'duration', => '',
  58. 'showtitle', => '',
  59. 'thumbnail', => '',
  60. 'file', => '',
  61. 'fanart', => '',
  62. 'album' => '',
  63. 'artist', => '',
  64. 'year', => '',
  65. 'type' => '',
  66. 'windowid' => '',
  67. 'window' => '',
  68. 'selection' => '',
  69. );
  70. # See if anything is playing
  71. my $result = xbmcJSON('Player.GetActivePlayers', undef());
  72. if (defined($result) && exists($result->{'playerid'})) {
  73. $data{'playerid'} = $result->{'playerid'} + 0;
  74. }
  75. # Get details about the player
  76. if ($data{'playerid'} >= 0) {
  77. $result = xbmcJSON('Player.GetProperties', { 'playerid' => $data{'playerid'}, 'properties' => [ 'time', 'type', 'speed' ] });
  78. if (defined($result)) {
  79. if (exists($result->{'time'})) {
  80. $data{'time'} = 0;
  81. if (exists($result->{'time'}->{'hours'})) {
  82. $data{'time'} += ($result->{'time'}->{'hours'} * 3600);
  83. }
  84. if (exists($result->{'time'}->{'minutes'})) {
  85. $data{'time'} += ($result->{'time'}->{'minutes'} * 60);
  86. }
  87. if (exists($result->{'time'}->{'seconds'})) {
  88. $data{'time'} += $result->{'time'}->{'seconds'};
  89. }
  90. if (exists($result->{'time'}->{'milliseconds'})) {
  91. $data{'time'} += ($result->{'time'}->{'milliseconds'} / 1000);
  92. }
  93. }
  94. if (exists($result->{'speed'}) && $result->{'speed'} > 0) {
  95. $data{'playing'} = 1;
  96. }
  97. if (exists($result->{'type'})) {
  98. $data{'type'} = $result->{'type'};
  99. }
  100. }
  101. # Get details about the playing item
  102. $result = xbmcJSON(
  103. 'Player.GetItem',
  104. {
  105. 'playerid' => $data{'playerid'},
  106. 'properties' => [ 'title', 'season', 'episode', 'duration', 'showtitle', 'thumbnail', 'file', 'fanart', 'album', 'artist', 'year' ]
  107. }
  108. );
  109. if (defined($result) && exists($result->{'item'})) {
  110. foreach my $key ('title', 'season', 'episode', 'duration', 'showtitle', 'thumbnail', 'file', 'fanart', 'album', 'year') {
  111. if (exists($result->{'item'}->{$key})) {
  112. $data{$key} = $result->{'item'}->{$key};
  113. }
  114. }
  115. if (exists($result->{'item'}->{'artist'})) {
  116. if (ref($result->{'item'}->{'artist'}) eq 'ARRAY') {
  117. $data{'artist'} = join(', ', @{ $result->{'item'}->{'artist'} });
  118. } else {
  119. warn("Invalid artist array\n");
  120. }
  121. }
  122. }
  123. }
  124. # Get details about the GUI
  125. $result = xbmcJSON('GUI.GetProperties', { 'properties' => [ 'currentcontrol', 'currentwindow' ] });
  126. if (defined($result)) {
  127. if (exists($result->{'currentwindow'})) {
  128. if (exists($result->{'currentwindow'}->{'label'})) {
  129. $data{'window'} = $result->{'currentwindow'}->{'label'};
  130. }
  131. if (exists($result->{'currentwindow'}->{'id'})) {
  132. $data{'windowid'} = $result->{'currentwindow'}->{'id'};
  133. }
  134. }
  135. if (exists($result->{'currentcontrol'})) {
  136. if (exists($result->{'currentcontrol'}->{'label'})) {
  137. $data{'selection'} = $result->{'currentcontrol'}->{'label'};
  138. }
  139. }
  140. }
  141. # Compare this data set to the last
  142. foreach my $key (keys(%data)) {
  143. if (!exists($dataLast{$key}) || $data{$key} ne $dataLast{$key}) {
  144. $changed = 1;
  145. last;
  146. }
  147. }
  148. # Ensure the output file exists
  149. if (!-r $OUT_FILE) {
  150. $changed = 1;
  151. }
  152. # If anything changed, save the data to disk
  153. if ($changed) {
  154. my $str = '';
  155. foreach my $key (keys(%data)) {
  156. $str .= $key . ':' . $data{$key} . "\n";
  157. }
  158. if ($DEBUG) {
  159. print STDERR $str;
  160. }
  161. my ($fh, $tmp) = tempfile($OUT_FILE . '.XXXXXXXX', 'UNLINK' => 0);
  162. print $fh $str;
  163. close($fh);
  164. rename($tmp, $OUT_FILE);
  165. }
  166. # Delay and loop
  167. sleep($DELAY);
  168. } until ($DELAY == 0);
  169. # Exit cleanly
  170. exit(0);
  171. sub buildCmd($$) {
  172. my ($method, $params) = @_;
  173. if (!defined($method) || length($method) < 1) {
  174. warn('No method name provided');
  175. return undef();
  176. }
  177. # Init the command structure
  178. my %cmd = (
  179. 'jsonrpc' => '2.0',
  180. 'id' => '1',
  181. 'method' => $method,
  182. );
  183. # Add parameters, if provided
  184. if (defined($params) && ref($params) eq 'HASH') {
  185. $cmd{'params'} = $params;
  186. }
  187. # Return as JSON
  188. return encode_json(\%cmd);
  189. }
  190. sub xbmcJSON($$) {
  191. my ($method, $params) = @_;
  192. my $cmd = buildCmd($method, $params);
  193. # Send the request
  194. if ($JSON_DEBUG) {
  195. print STDERR 'Sending command: ' . $cmd . "\n";
  196. }
  197. my $data = get($BASE_URL . uri_escape($cmd));
  198. if (!defined($data)) {
  199. if ($DEBUG) {
  200. print STDERR "No data returned from RPC call\n";
  201. }
  202. return undef();
  203. }
  204. # Parse into a perl data structure
  205. my $retval = decode_json($data);
  206. if (!defined($retval) || ref($retval) ne 'HASH') {
  207. print STDERR 'Invalid JSON: ' . $data . "\n";
  208. return undef();
  209. }
  210. # Reduce to the result section
  211. if (exists($retval->{'result'})) {
  212. if (ref($retval->{'result'}) eq 'HASH') {
  213. $retval = $retval->{'result'};
  214. } elsif (ref($retval->{'result'}) eq 'ARRAY') {
  215. $retval = @{ $retval->{'result'} }[0];
  216. }
  217. }
  218. # Return what we've got
  219. if ($JSON_DEBUG) {
  220. print "Result:\n";
  221. printHash($retval, "\t");
  222. }
  223. return $retval;
  224. }
  225. sub printHash($$) {
  226. my ($hash, $prefix) = @_;
  227. my $nextPrefix = $prefix . substr($prefix, 0, 1);
  228. if (!defined($hash)) {
  229. return;
  230. } elsif (ref($hash) ne 'HASH') {
  231. print STDERR 'Invalid hash: ' . $hash . "\n";
  232. } else {
  233. foreach my $key (keys(%{$hash})) {
  234. if (ref($hash->{$key}) eq 'HASH') {
  235. print STDERR $prefix . $key . ":\n";
  236. printHash($hash->{$key}, $nextPrefix);
  237. } elsif (ref($hash->{$key}) eq 'ARRAY') {
  238. print STDERR $prefix . $key . " => [\n";
  239. foreach my $element (@{ $hash->{$key} }) {
  240. if (ref($element) eq 'HASH') {
  241. printHash($element, $nextPrefix);
  242. } else {
  243. print STDERR $nextPrefix . $element . "\n";
  244. }
  245. }
  246. print STDERR $prefix . "]\n";
  247. } else {
  248. print STDERR $prefix . $key . ' => ' . $hash->{$key} . "\n";
  249. }
  250. }
  251. }
  252. }