plexMonitor-json.pl 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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{'playing'} = 1;
  74. $data{'playerid'} = $result->{'playerid'} + 0;
  75. }
  76. # Get details about the playing item
  77. if ($data{'playing'}) {
  78. $result = xbmcJSON('Player.GetProperties', { 'playerid' => $data{'playerid'}, 'properties' => [ 'time', 'type' ] });
  79. if (defined($result)) {
  80. if (exists($result->{'time'})) {
  81. $data{'time'} = 0;
  82. if (exists($result->{'time'}->{'hours'})) {
  83. $data{'time'} += ($result->{'time'}->{'hours'} * 3600);
  84. }
  85. if (exists($result->{'time'}->{'minutes'})) {
  86. $data{'time'} += ($result->{'time'}->{'minutes'} * 60);
  87. }
  88. if (exists($result->{'time'}->{'seconds'})) {
  89. $data{'time'} += $result->{'time'}->{'seconds'};
  90. }
  91. if (exists($result->{'time'}->{'milliseconds'})) {
  92. $data{'time'} += ($result->{'time'}->{'milliseconds'} / 1000);
  93. }
  94. }
  95. if (exists($result->{'type'})) {
  96. $data{'type'} = $result->{'type'};
  97. }
  98. }
  99. $result = xbmcJSON(
  100. 'Player.GetItem',
  101. {
  102. 'playerid' => $data{'playerid'},
  103. 'properties' => [ 'title', 'season', 'episode', 'duration', 'showtitle', 'thumbnail', 'file', 'fanart', 'album', 'artist', 'year' ]
  104. }
  105. );
  106. if (defined($result) && exists($result->{'item'})) {
  107. foreach my $key ('title', 'season', 'episode', 'duration', 'showtitle', 'thumbnail', 'file', 'fanart', 'album', 'year') {
  108. if (exists($result->{'item'}->{$key})) {
  109. $data{$key} = $result->{'item'}->{$key};
  110. }
  111. }
  112. if (exists($result->{'item'}->{'artist'})) {
  113. if (ref($result->{'item'}->{'artist'}) eq 'ARRAY') {
  114. $data{'artist'} = join(', ', @{ $result->{'item'}->{'artist'} });
  115. } else {
  116. warn("Invalid artist array\n");
  117. }
  118. }
  119. }
  120. }
  121. # Get details about the GUI
  122. $result = xbmcJSON('GUI.GetProperties', { 'properties' => [ 'currentcontrol', 'currentwindow' ] });
  123. if (defined($result)) {
  124. if (exists($result->{'currentwindow'})) {
  125. if (exists($result->{'currentwindow'}->{'label'})) {
  126. $data{'window'} = $result->{'currentwindow'}->{'label'};
  127. }
  128. if (exists($result->{'currentwindow'}->{'id'})) {
  129. $data{'windowid'} = $result->{'currentwindow'}->{'id'};
  130. }
  131. }
  132. if (exists($result->{'currentcontrol'})) {
  133. if (exists($result->{'currentcontrol'}->{'label'})) {
  134. $data{'selection'} = $result->{'currentcontrol'}->{'label'};
  135. }
  136. }
  137. }
  138. # Compare this data set to the last
  139. foreach my $key (keys(%data)) {
  140. if (!exists($dataLast{$key}) || $data{$key} ne $dataLast{$key}) {
  141. $changed = 1;
  142. last;
  143. }
  144. }
  145. # Ensure the output file exists
  146. if (!-r $OUT_FILE) {
  147. $changed = 1;
  148. }
  149. # If anything changed, save the data to disk
  150. if ($changed) {
  151. my $str = '';
  152. foreach my $key (keys(%data)) {
  153. $str .= $key . ':' . $data{$key} . "\n";
  154. }
  155. if ($DEBUG) {
  156. print STDERR $str;
  157. }
  158. my ($fh, $tmp) = tempfile($OUT_FILE . '.XXXXXXXX', 'UNLINK' => 0);
  159. print $fh $str;
  160. close($fh);
  161. rename($tmp, $OUT_FILE);
  162. }
  163. # Delay and loop
  164. sleep($DELAY);
  165. } until ($DELAY == 0);
  166. # Exit cleanly
  167. exit(0);
  168. sub buildCmd($$) {
  169. my ($method, $params) = @_;
  170. if (!defined($method) || length($method) < 1) {
  171. warn('No method name provided');
  172. return undef();
  173. }
  174. # Init the command structure
  175. my %cmd = (
  176. 'jsonrpc' => '2.0',
  177. 'id' => '1',
  178. 'method' => $method,
  179. );
  180. # Add parameters, if provided
  181. if (defined($params) && ref($params) eq 'HASH') {
  182. $cmd{'params'} = $params;
  183. }
  184. # Return as JSON
  185. return encode_json(\%cmd);
  186. }
  187. sub xbmcJSON($$) {
  188. my ($method, $params) = @_;
  189. my $cmd = buildCmd($method, $params);
  190. # Send the request
  191. if ($JSON_DEBUG) {
  192. print STDERR 'Sending command: ' . $cmd . "\n";
  193. }
  194. my $data = get($BASE_URL . uri_escape($cmd));
  195. if (!defined($data)) {
  196. if ($DEBUG) {
  197. print STDERR "No data returned from RPC call\n";
  198. }
  199. return undef();
  200. }
  201. # Parse into a perl data structure
  202. my $retval = decode_json($data);
  203. if (!defined($retval) || ref($retval) ne 'HASH') {
  204. print STDERR 'Invalid JSON: ' . $data . "\n";
  205. return undef();
  206. }
  207. # Reduce to the result section
  208. if (exists($retval->{'result'})) {
  209. if (ref($retval->{'result'}) eq 'HASH') {
  210. $retval = $retval->{'result'};
  211. } elsif (ref($retval->{'result'}) eq 'ARRAY') {
  212. $retval = @{ $retval->{'result'} }[0];
  213. }
  214. }
  215. # Return what we've got
  216. if ($JSON_DEBUG) {
  217. print "Result:\n";
  218. printHash($retval, "\t");
  219. }
  220. return $retval;
  221. }
  222. sub printHash($$) {
  223. my ($hash, $prefix) = @_;
  224. my $nextPrefix = $prefix . substr($prefix, 0, 1);
  225. if (!defined($hash)) {
  226. return;
  227. } elsif (ref($hash) ne 'HASH') {
  228. print STDERR 'Invalid hash: ' . $hash . "\n";
  229. } else {
  230. foreach my $key (keys(%{$hash})) {
  231. if (ref($hash->{$key}) eq 'HASH') {
  232. print STDERR $prefix . $key . ":\n";
  233. printHash($hash->{$key}, $nextPrefix);
  234. } elsif (ref($hash->{$key}) eq 'ARRAY') {
  235. print STDERR $prefix . $key . " => [\n";
  236. foreach my $element (@{ $hash->{$key} }) {
  237. if (ref($element) eq 'HASH') {
  238. printHash($element, $nextPrefix);
  239. } else {
  240. print STDERR $nextPrefix . $element . "\n";
  241. }
  242. }
  243. print STDERR $prefix . "]\n";
  244. } else {
  245. print STDERR $prefix . $key . ' => ' . $hash->{$key} . "\n";
  246. }
  247. }
  248. }
  249. }