File: playlist.c

package info (click to toggle)
shell-fm 0.7%2Bgit20100414-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 444 kB
  • ctags: 305
  • sloc: ansic: 4,422; makefile: 135; python: 80; haskell: 76; sh: 67; perl: 19
file content (228 lines) | stat: -rw-r--r-- 4,218 bytes parent folder | download | duplicates (2)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
	Copyright (C) 2006 by Jonas Kramer
	Published under the terms of the GNU General Public License (GPL).
*/

#define _GNU_SOURCE


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <sys/types.h>


#include "hash.h"
#include "http.h"
#include "settings.h"
#include "service.h"
#include "strary.h"
#include "util.h"

#include "playlist.h"
#include "globals.h"

#include "interface.h"


int expand(struct playlist * list) {
	char url[512], ** response, * xml = NULL;
	const char * fmt =
		"http://ws.audioscrobbler.com/radio/xspf.php"
		"?sk=%s&discovery=%d&desktop=0";

	assert(list != NULL);

	memset(url, 0, sizeof(url));
	snprintf(
		url, sizeof(url), fmt,
		value(& data, "session"), !!enabled(DISCOVERY)
	);

	response = fetch(url, NULL, NULL, NULL);

	if(response != NULL) {
		int retval;

		xml = join(response, 0);
		response = NULL;

		retval = parsexspf(list, xml);
		return retval;
	}

	return 0;
}

void trim(char * string){
	int offset = 0;
	while(string[offset] == ' ')
		offset++;

	if(offset)
		memmove(string, string + offset, strlen(string + offset) + 1);
}


int parsexspf(struct playlist * list, char * xml) {
	char * ptr = xml;
	unsigned i, tracks = 0;
	char * track;

	assert(list != NULL);
	assert(xml != NULL);

	while((track = strcasestr(ptr, "<track>")) != NULL) {
		struct tracknode * node = NULL;
		char * next = strcasestr(track + 7, "<track>"), * duration;

		const char * tags [] = {
			"location", "title", "album", "creator", "duration", "image",
			"lastfm:trackauth",
		};

		const char * links [] = { "artistpage", "albumpage", "trackpage", "freeTrackURL", };


		if(next)
			* (next - 1) = 0;

		node = malloc(sizeof(struct tracknode));
		assert(node != NULL);

		memset(node, 0, sizeof(struct tracknode));

		for(i = 0; i < (sizeof(tags) / sizeof(char *)); ++i) {
			char begin[32] = { 0 }, end[32] = { 0 };

			sprintf(begin, "<%s>", tags[i]);
			sprintf(end, "</%s>", tags[i]);

			if((ptr = strcasestr(track, begin)) != NULL) {
				char * text = strndup(
						ptr + strlen(begin),
						(strcasestr(ptr, end)) - (ptr + strlen(begin))
				);

				assert(text != NULL);

				unhtml(text);
				set(& node->track, tags[i], text);
				free(text);
			}
		}

		for(i = 0; i < (sizeof(links) / sizeof(char *)); ++i) {
			char begin[64] = { 0 };

			sprintf(begin, "<link rel=\"http://www.last.fm/%s\">", links[i]);

			if((ptr = strcasestr(track, begin)) != NULL) {
				char * text = strndup(
						ptr + strlen(begin),
						(strcasestr(ptr, "</link>")) - (ptr + strlen(begin))
						);

				assert(text != NULL);

				set(& node->track, links[i], text);
				free(text);
			}
		}

		if(list->title) {
			trim(list->title);

			set(& node->track, "station", list->title);
		}
		else {
			set(& node->track, "station", "Unknown Station");
		}

		duration = strdup(value(& node->track, "duration"));
		if(duration != NULL) {
			duration[strlen(duration) - 3] = 0;
			set(& node->track, "duration", duration);
		}

		push(list, node);

		++tracks;

		if(!next)
			break;

		ptr = next;
	}

	return 1;
}


void freelist(struct playlist * list) {
	if(list->title != NULL)
		free(list->title);

	while(list->track)
		shift(list);

	memset(list, 0, sizeof(struct playlist));
}


void push(struct playlist * list, struct tracknode * node) {
	if(!list->track)
		list->track = node;
	else {
		struct tracknode * last = list->track;
		while(last->next != NULL)
			last = last->next;
		last->next = node;
	}

	++list->left;
}


void shift(struct playlist * list) {
	if(list->track) {
		struct tracknode * node = list->track;
		list->track = node->next;
		empty(& node->track);
		free(node);
		--(list->left);
	}
}

void preview(struct playlist list) {
	struct tracknode * node;
	unsigned n = 0;

	if (list.track != NULL)
		node = list.track->next;
	else
	{
		puts("No tracks in queue.");
		return;
	}

	if(node == NULL) {
		puts("No tracks in queue.");
	}
	else {
		puts("Upcoming tracks:");
		while(node != NULL) {
			const char * format;

			format = haskey(& rc, "preview-format")
				? value(& rc, "preview-format")
				: "%a - %t";

			printf("%2d %s\n", n++, meta(format, M_COLORED, & node->track));

			node = node->next;
		}
	}
}