-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrlm_spinitron.c
161 lines (147 loc) · 5.43 KB
/
rlm_spinitron.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* rlm_spinitron.c
* Log songs playing in Rivendell to Spinitron
*
* Module by Eric Berg, Benjamin Yu (C) Copyright 2009.
* Enhancements by Max Goldstein (C) Copyright 2012.
* For more copyright and legal information, see TERMS and COPYING.
*
* For documentation, see the README. For compilation, see the Makefile.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include "rlm.h"
#include "credentials.h" // defines const char*s USERNAME, PASSWORD, STATION
#define LOG_CART_NUM 1 // Boolean: should log cart numbers in notes field?
#define NOTE_LEN 20
#define BUFFER_SIZE 512
#define LARGE_BUFFER BUFFER_SIZE*4
char old_title[BUFFER_SIZE]; //Global to persist between callbacks
void rlm_spinitron_RLMStart(void *ptr, const char *arg)
{
(void) arg; //Silence compiler warning
RLMLog(ptr,LOG_INFO,"Rivendell-Spinitron Update will start logging now.");
strcpy(old_title, "A string that will never be the title of a song.");
}
void rlm_spinitron_RLMFree(void *ptr)
{
RLMLog(ptr,LOG_INFO, "Rivendell-Spinitron Update will stop logging now.");
}
void url_encode (char *src, int len) {
int soffset = 0;
int doffset = 0;
char dst[len];
memset(dst, '\0', len);
char a = src[soffset++];
while (doffset < len && a != '\0'){
if ((a >= '0' && a <= '9') ||
(a >= 'A' && a <= 'Z') ||
(a >= 'a' && a <= 'z')) {
dst[doffset++] = a;
}else if (a == ' '){
dst[doffset++] = '+';
}else{
if (doffset +4 < len){
snprintf(&dst[doffset], 4, "%%%02X", a);
doffset += 3;
}else{
dst[doffset] = '\0';
break;
}
}
a = src[soffset++];
}
dst[len-1] = '\0';
strncpy(src, dst, len);
}
void rlm_spinitron_RLMPadDataSent(void *ptr,const struct rlm_svc *svc,
const struct rlm_log *log,
const struct rlm_pad *now,
const struct rlm_pad *next)
{
(void) svc;
(void) next;
char title[BUFFER_SIZE];
char artist[BUFFER_SIZE];
char album[BUFFER_SIZE];
char genre[BUFFER_SIZE];
int chars_needed;
int duration = now->rlm_len / 1000; // rlm is in milliseconds
openlog("Rivendell-Spinitron", LOG_PID, LOG_USER);
strncpy (title, now->rlm_title, BUFFER_SIZE);
strncpy (artist, now->rlm_artist, BUFFER_SIZE);
strncpy (album, now->rlm_album, BUFFER_SIZE);
strncpy (genre, now->rlm_userdef, BUFFER_SIZE);
title [BUFFER_SIZE-1] = '\0';
artist[BUFFER_SIZE-1] = '\0';
album [BUFFER_SIZE-1] = '\0';
genre [BUFFER_SIZE-1] = '\0';
url_encode(title, BUFFER_SIZE);
url_encode(artist, BUFFER_SIZE);
url_encode(album, BUFFER_SIZE);
url_encode(genre, BUFFER_SIZE);
int live = log->log_onair ? 1 : 0;
char notes[NOTE_LEN];
if (LOG_CART_NUM){
snprintf(notes, NOTE_LEN, "(cart %u)", now->rlm_cartnum);
notes[NOTE_LEN -1] = '\0';
}else{
notes[0]='\0';
}
if (! *title){
syslog(LOG_WARNING, "No title. Dropping.");
RLMLog(ptr, LOG_WARNING, "No title. Dropping.");
return;
}
if (!(strncmp(old_title, title, BUFFER_SIZE))) {
syslog(LOG_WARNING, "Same title. Dropping.");
RLMLog(ptr, LOG_WARNING, "Same title. Dropping.");
return;
}
strncpy(old_title, title, BUFFER_SIZE);
#ifdef AXIA_CHECK_ENABLED
char axiabuf[LARGE_BUFFER];
chars_needed = snprintf(axiabuf, LARGE_BUFFER,
"curl -s -u %s:%s --connect-timeout 2 %s | grep \"name=%s\" | grep -c %s > /dev/null",
NODE_USERNAME, NODE_PASSWORD, NODE_URL, NODE_CHANNEL, NODE_STUDIO_PGM_CHANNEL);
if (chars_needed >= LARGE_BUFFER){
RLMLog(ptr, LOG_ERR, "Insufficient buffer size to check axia routing.");
} else {
if (system(axiabuf) == 0) {
syslog(LOG_DEBUG,"Node Indicates Configured Source Active, Continue.");
RLMLog(ptr, LOG_INFO, "Node Indicates Configured Source Active, Continue.");
} else {
syslog(LOG_DEBUG, "Node Indicates Configured Source Inactive, Stop.");
RLMLog(ptr, LOG_INFO, "Node Indicates Configured Source Inactive, Stop.");
return;
}
}
#endif
char sendToSpinitron[LARGE_BUFFER];
chars_needed = snprintf(sendToSpinitron, LARGE_BUFFER,
"curl -s -H 'Authorization: Bearer %s' --connect-timeout 2 https://spinitron.com/api/spins -d \"duration=%d\" -d \"artist=%s\" -d \"release=%s\" -d \"genre=%s\" -d \"song=%s\" -d \"note=%s\" -d \"live=%d\"",
API_KEY, duration, artist, album, genre, title, notes, live);
if (chars_needed >= LARGE_BUFFER){
syslog(LOG_ERR,"Insufficient buffer size to send to Spinitron.");
RLMLog(ptr, LOG_ERR, "Insufficient buffer size to send to Spinitron.");
}else{
int retval = system(sendToSpinitron);
if (retval == -1 || retval == 127){
syslog(LOG_ERR, "Unable to send to Spinitron. Something is wrong with the shell.");
RLMLog(ptr, LOG_ERR, "Unable to send to Spinitron. Something is wrong with the shell.");
} else if (retval) {
syslog(LOG_ERR, "Unable to send to Spinitron. Something is wrong with curl.");
RLMLog(ptr, LOG_ERR, "Unable to send to Spinitron. Something is wrong with curl.");
}
}
syslog(LOG_INFO, "%s", sendToSpinitron);
RLMLog(ptr, LOG_INFO, sendToSpinitron);
}
void rlm_spinitron_RLMTimerExpired(void *ptr, int timernum)
{
(void) ptr;
(void) timernum;
}