Matt Drown wrote: 
> 
> I don't have metadata running, was looking to grab the info from the
> json, but having some issues crafting up the correct messages, if anyone
> has this working, please let me know.  I just want to extract current
> playing title/artist, and dump into a file.
> 
> Hope this helps someone.

Here's something I hacked together a while back.  Only requirement is
cJSON, which is a very basic json parser in C.

current.c:

Code:
--------------------
    
  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
  #include <unistd.h>
  #include <sys/socket.h> /* socket, connect */
  #include <netinet/in.h> /* struct sockaddr_in, struct sockaddr */
  #include <netdb.h> /* struct hostent, gethostbyname */
  
  #include "cJSON.h"
  #include "lms.h"
  
  void error(const char *msg) { perror(msg); exit(0); }
  
  int post(char *host, int portno, char *q, char *buf, int buflen)
  {
  int i;
  
  struct hostent *server;
  struct sockaddr_in serv_addr;
  int sockfd, bytes, sent, received, total, message_size;
  char *message;
  char *response = buf;
  
  /* How big is the message? */
  message_size=0;
  message_size+=strlen("%s %s HTTP/1.0\r\nPOST /jsonrpc.js");
  message_size+=strlen(q)+strlen("\r\n");
  message_size+=strlen("Content-Length: %d\r\n")+10; /* content length */
  message_size+=strlen("\r\n");                          /* blank line     */
  
  /* allocate space for the message */
  message=malloc(message_size);
  
  /* fill in the parameters */
  sprintf(message,"POST /jsonrpc.js HTTP/1.0\r\n");
  sprintf(message+strlen(message),"Content-Length: %d\r\n",strlen(q));
  strcat(message,"\r\n");                                /* blank line     */
  strcat(message,q);
  strcat(message,"\r\n");
  
  /* create the socket */
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0) error("ERROR opening socket");
  
  /* lookup the ip address */
  server = gethostbyname(host);
  if (server == NULL) error("ERROR, no such host");
  
  /* fill in the structure */
  memset(&serv_addr,0,sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_port = htons(portno);
  memcpy(&serv_addr.sin_addr.s_addr,server->h_addr,server->h_length);
  
  /* connect the socket */
  if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
  error("ERROR connecting");
  
  /* send the request */
  total = strlen(message);
  sent = 0;
  do {
  bytes = write(sockfd,message+sent,total-sent);
  if (bytes < 0)
  error("ERROR writing message to socket");
  if (bytes == 0)
  break;
  sent+=bytes;
  } while (sent < total);
  
  /* receive the response */
  memset(response,0,buflen);
  total = buflen-1;
  
  received = 0;
  do {
  bytes = read(sockfd,response+received,total-received);
  if (bytes < 0)
  error("ERROR reading response from socket");
  if (bytes == 0)
  break;
  received+=bytes;
  } while (received < total);
  
  /*
  * if the number of received bytes is the total size of the
  * array then we have run out of space to store the response
  * and it hasn't all arrived yet - so that's a bad thing
  */
  if (received == total)
  error("ERROR storing complete response from socket");
  
  /* close the socket */
  close(sockfd);
  
  free(message);
  return 0;
  }
  
  int main(void) {
  
  char query[2048];
  char param[1024];
  char decoded[2048];
  char *q = query;
  char *p = param;
  char *d = decoded;
  char *r;
  int arraylen, i;
  int status = 1;
  cJSON *results = NULL;
  cJSON *result = NULL;
  cJSON *items = NULL;
  cJSON *item = NULL;
  cJSON *json = NULL;
  
  char *host = "192.168.68.121";
  int port = 9000;
  
  q = "{ \"id\": 1, \"method\": \"slim.request\", \"params\": 
[\"4c:e1:73:42:26:0c\",[\"path\", \"?\"]]}";
  
  
  post(host,port,q,decoded,2048);
  
  r = strstr(decoded,"{");
  strcpy(decoded,r);
  json = cJSON_Parse(decoded);
  
  if (json == NULL)
  {
  const char *error_ptr = cJSON_GetErrorPtr();
  if (error_ptr != NULL)
  {
  fprintf(stderr, "Error before %s\n", error_ptr);
  }
  status = 0;
  
  }
  
  results = cJSON_GetObjectItemCaseSensitive(json, "result");
  cJSON_ArrayForEach(result, results) 
  {
  cJSON *path = cJSON_GetObjectItemCaseSensitive(result, "_path");
  if (cJSON_IsString(result) && (result->valuestring != NULL))
  {
  p = result->valuestring;
  }
  else {
  printf("Error getting path\n");
  }
  }
  
  query[0] = '\0';
  
sprintf(query,"{\"method\":\"slim.request\",\"params\":[\"4c:e1:73:42:26:0c\",[\"songinfo\",0,99,\"url:%s\"]]}",p);
  
  cJSON_Delete(json);
  p = param;
  
  post(host,port,query,decoded,2048);
  r = strstr(decoded,"{");
  strcpy(decoded,r);
  
  json = cJSON_Parse(decoded);
  if (json == NULL)
  {
  const char *error_ptr = cJSON_GetErrorPtr();
  if (error_ptr != NULL)
  {
  fprintf(stderr, "Error before %s\n", error_ptr);
  }
  status = 0;
  }
  
  results = cJSON_GetObjectItem(json, "result");
  
  items = cJSON_GetObjectItem(results, "songinfo_loop");
  
  cJSON_ArrayForEach(item, items)
  {
  if (cJSON_IsString(item->child) && (item->child->valuestring != NULL))
  {
  printf("%s=%s\n", item->child->string,item->child->valuestring);
  } 
  }
  cJSON_Delete(json);
  }
  
--------------------


lms.h:

Code:
--------------------
    
  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
  
  struct metadata {
  char artist[1024];
  char album[1024];
  char title[1024];
  char bitrate[30];
  char type[30];
  int new_title;
  int new_album;
  int new_artist;
  int terminate;
  int playing;
  };
  
  struct memory {
  char *response;
  size_t size;
  };
  
  int post(char *host, int portno, char *q, char *buf, int buflen);
  
  void remove_char(char *str, char garbage); 
  
  int lms_query(char *query, char *decoded); 
  
  void *get_metadata(void *m);
  
--------------------


Compile:
gcc -o current current.c cJSON.c

Run:
./current 

id=-561724432
title=Jazz Record Requests - New discoveries and evergreen classics
artist=Alyn Shipton presents jazz records of all styles as requested by
you, with music from Fats Waller, Anita O'Day and Miles Davis.
coverid=-561724432
duration=3540
coverart=0
type=BBCSounds
bitrate=96kbps CBR
year=0
channels=2
artwork_url=https://ichef.bbci.co.uk/images/ic/320x320/p09c8r13.jpg
remote_title=Play
samplerate=48000



piTouch™ w/JustBoom DigiHat -> RME ADI-2 DAC FS -> JBL 305P MkII
monitors; LMS 8.2 on piCorePlayer/Pi 4;  Material Skin.
------------------------------------------------------------------------
chicks's Profile: http://forums.slimdevices.com/member.php?userid=71798
View this thread: http://forums.slimdevices.com/showthread.php?t=112809

_______________________________________________
plugins mailing list
[email protected]
http://lists.slimdevices.com/mailman/listinfo/plugins

Reply via email to