summaryrefslogtreecommitdiff
path: root/inforx.c
blob: 1c4903508b9f24f550a42ca7877579c8bae756b0 (plain)
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * inetrx.c - part of radio.c, a plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 */

#include <fstream>
#include "inforx.h"


void fill_rtpbuffer(char *text1, char *text2) {

    if (++rtp_content.rt_Index >= 2 * MAX_RTPC)
        rtp_content.rt_Index = 0;
    asprintf(&rtp_content.radiotext[rtp_content.rt_Index], "%s", text1);
    snprintf(RT_Text[RT_Index], RT_MEL, "%s",
            rtp_content.radiotext[rtp_content.rt_Index]);
    RT_Index += 1;
    if (RT_Index >= S_RtOsdRows)
        RT_Index = 0;

    if (++rtp_content.rt_Index >= 2 * MAX_RTPC)
        rtp_content.rt_Index = 0;
    asprintf(&rtp_content.radiotext[rtp_content.rt_Index], "%s", text2);
    snprintf(RT_Text[RT_Index], RT_MEL, "%s",
            rtp_content.radiotext[rtp_content.rt_Index]);
    RT_Index += 1;
    if (RT_Index >= S_RtOsdRows) {
        RT_Index = 0;
    }

    if (++rtp_content.item_Index >= MAX_RTPC) {
        rtp_content.item_Index = 0;
    }
    if (rtp_content.item_Index >= 0) {
        rtp_content.item_Start[rtp_content.item_Index] = RTP_Starttime;
        asprintf(&rtp_content.item_Artist[rtp_content.item_Index], "%s",
                RTP_Artist);
        asprintf(&rtp_content.item_Title[rtp_content.item_Index], "%s",
                RTP_Title);
    }

    if ((S_Verbose & 0x0f) >= 1) {
        dsyslog("radio-inforx: %s / %s\n", RTP_Artist, RTP_Title);
    }
}

int info_request(int chantid, int chanapid) {

    char ident[80];
    char artist[RT_MEL], titel[RT_MEL];
    struct tm tm_store;
    static int repeat = 0;
    char temp[2][2 * RT_MEL];

    char *command, *tempfile;
    asprintf(&command, "%s/radioinfo-%d-%d", ConfigDir, chantid, chanapid);
    asprintf(&tempfile, "%s/%s", DataDir, RadioInfofile);
    dsyslog("radio: inforxcall '%s'\n", command);

    DoInfoReq = false;
    if (file_exists(command)) {
        if (file_exists(tempfile)) {	// delete tempfile
            char *delcmd;
            asprintf(&delcmd, "rm -f \"%s\"", tempfile);
            system(delcmd);
            free(delcmd);
        }
        asprintf(&command, "%s \"%s\"", command, tempfile);
        if (!system(command)) {			// execute script && read tempfile
            if (file_exists(tempfile)) {
                std::ifstream infile(tempfile);
                if (infile.is_open()) {
                    infile.getline(ident, 80);
                    infile.getline(artist, RT_MEL);
                    infile.getline(titel, RT_MEL);
                }
                infile.close();
                DoInfoReq = true;
            }
            else {
                esyslog("radio: ERROR inforx tempfile = %s\n", command);
            }
        }
        else {
            esyslog("radio: ERROR inforx command = %s\n", command);
        }
    }
    free(command);
    free(tempfile);

    if (!DoInfoReq) {
        dsyslog("radio: inforxcall cancled !\n");
        return -1;
    }

    // Info empty?
    if (strcmp(artist, "") == 0 && strcmp(titel, "") == 0) {
        if (repeat < 700) {		// ~ 6x switch off
            repeat += 120;
            sprintf(RTP_Artist, "---");
            sprintf(RTP_Title, "---");
            RTP_Starttime = time(NULL);
            sprintf(temp[0], "%s  Error :-(", ident);
            struct tm *ts = localtime_r(&RTP_Starttime, &tm_store);
            sprintf(temp[1], "%02d:%02d  no Songinfo received !", ts->tm_hour,
                    ts->tm_min);
            fill_rtpbuffer(temp[0], temp[1]);
            RT_MsgShow = true;
            RT_PlusShow = false;
            (RT_Info > 0) ? : RT_Info = 1;
            radioStatusMsg();
            return 120;
        }
        else {
            RTP_Starttime = time(NULL);
            sprintf(temp[0], "%s  Error :-(", ident);
            struct tm *ts = localtime_r(&RTP_Starttime, &tm_store);
            sprintf(temp[1], "%02d:%02d  no Songinfo received, switching off !",
                    ts->tm_hour, ts->tm_min);
            asprintf(&rtp_content.info_Url,
                    "%02d:%02d  no Songinfo, switched off!", ts->tm_hour,
                    ts->tm_min);
            fill_rtpbuffer(temp[0], temp[1]);
            RT_MsgShow = true;
            RT_PlusShow = false;
            (RT_Info > 0) ? : RT_Info = 1;
            radioStatusMsg();
            DoInfoReq = false;
            return 60;
        }
    }

    // Info
    xhtml2text(artist);
    xhtml2text(titel);
    if (strcmp(RTP_Artist, artist) != 0 || strcmp(RTP_Title, titel) != 0) {
        snprintf(RTP_Artist, RT_MEL, "%s", artist);
        snprintf(RTP_Title, RT_MEL, "%s", titel);
        RTP_Starttime = time(NULL);
        sprintf(temp[0], "%s  ok :-)", ident);
        struct tm *ts = localtime_r(&RTP_Starttime, &tm_store);
        snprintf(temp[1], 2 * RT_MEL, "%02d:%02d  %s: %s", ts->tm_hour,
                ts->tm_min, RTP_Artist, RTP_Title);
        fill_rtpbuffer(temp[0], temp[1]);
        RT_MsgShow = RT_PlusShow = true;
        (RT_Info > 0) ? : RT_Info = 2;
        radioStatusMsg();
        repeat = 0;
        return 90;
    }
    else {
        if (repeat < 1200) {		// ~20 Min. timeout
            repeat += 10;
            return 10;
        }
        else {
            sprintf(RTP_Artist, "---");
            sprintf(RTP_Title, "---");
            RTP_Starttime = time(NULL);
            sprintf(temp[0], "%s  Error :-(", ident);
            struct tm *ts = localtime_r(&RTP_Starttime, &tm_store);
            sprintf(temp[1],
                    "%02d:%02d  no more different Songinfo received, switching off !",
                    ts->tm_hour, ts->tm_min);
            asprintf(&rtp_content.info_Url,
                    "%02d:%02d  no more Songinfo, switched off!", ts->tm_hour,
                    ts->tm_min);
            fill_rtpbuffer(temp[0], temp[1]);
            RT_MsgShow = true;
            RT_PlusShow = false;
            (RT_Info > 0) ? : RT_Info = 1;
            radioStatusMsg();
            DoInfoReq = false;
            return 60;
        }
    }

    return 10;
}


// end