-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.c
219 lines (199 loc) · 5.68 KB
/
player.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
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
#include <string.h>
#include "login.h"
#include "player.h"
Playlist *plist;
Playlist *first;
void freeSongs(Playlist *list)
{
while(list != NULL)
{
Playlist *temp = list->next;
free(list->song->songIdentity);
free(list->song->trackToken);
free(list->song->songName);
free(list->song->artistName);
free(list->song->albumName);
free(list->song->audioUrl);
free(list->song);
free(list);
list = temp;
}
}
void DownloadSong(Playlist *list)
{
SongData *song = malloc(sizeof(SongData));
song->data = malloc(1);
song->data[0] = '\0';
song->length = 0;
AudioRequest(list->song->audioUrl, song);
printf("%ld\n", song->length);
FILE *songFile = fopen("test.aac", "wb");
int ii;
for(ii = 0; ii < song->length; ii++)
{
fputc(song->data[ii], songFile);
}
}
void allocInfo(char **info, const char *s, json_t *item)
{
size_t length = json_string_length(json_object_get(item, s));
*info = malloc(length + 1);
strcpy(*info, json_string_value(json_object_get(item, s)));
(*info)[length] = '\0';
}
void GetSongInfo(Playlist *list, json_t *item)
{
list->song = malloc(sizeof(Song));
allocInfo(&(list->song->songIdentity), "songIdentity", item);
allocInfo(&(list->song->trackToken), "trackToken", item);
allocInfo(&(list->song->songName), "songName", item);
allocInfo(&(list->song->artistName), "artistName", item);
allocInfo(&(list->song->albumName), "albumName", item);
json_t *map = json_object_get(json_object_get(item, "audioUrlMap"), "mediumQuality");
allocInfo(&(list->song->audioUrl), "audioUrl", map);
}
void PrintSong(Playlist *list)
{
while(list != NULL)
{
printf("%s -- %s (%s)\n", list->song->songName, list->song->artistName, list->song->albumName);
list = list->next;
}
}
void ParsePlaylist(json_t *root)
{
json_t *items = json_object_get(json_object_get(root, "result"), "items");
int ii;
int count = json_array_size(items);
for(ii = 0; ii < count; ii++)
{
json_t *curItem = json_array_get(items, ii);
if(json_string_value(json_object_get(curItem, "songName")) != NULL)
{
if(!plist)
{
plist = malloc(sizeof(Playlist));
plist->next = NULL;
plist->prev = NULL;
first = plist;
}
else
{
plist->next = malloc(sizeof(Playlist));
plist->next->prev = plist;
plist = plist->next;
plist->next = NULL;
}
GetSongInfo(plist, json_array_get(items, ii));
}
}
PrintSong(first);
DownloadSong(first);
freeSongs(first);
}
void GetPlaylistReturn(char *response)
{
json_error_t error;
json_t *root = json_loads(response, 0, &error);
if(!root)
{
printf("Error: %s\n", error.text);
exit(EXIT_FAILURE);
}
json_t *stat = json_object_get(root, "stat");
if(strcmp(json_string_value(stat),"ok") != 0)
{
printf("OK was not returned while getting station list.\n");
printf("%s\n", response);
exit(EXIT_FAILURE);
}
return ParsePlaylist(root);
}
void GetPlaylist(struct Auth *auth, struct Station station)
{
char url[200] = "http://tuner.pandora.com/services/json/?method=station.getPlaylist&partner_id=";
strcat(url, auth->partnerId);
strcat(url, "&auth_token=");
strcat(url, auth->escapedToken);
strcat(url, "&user_id=");
strcat(url, auth->userId);
char body[500] = "{\"stationToken\": \"";
strcat(body, station.id);
strcat(body, "\",\"userAuthToken\":\"");
strcat(body, auth->token);
strcat(body, "\",\"syncTime\":");
sprintf(body, "%s%ld", body, GetCurrentSyncTime(auth->sync));
strcat(body, "}");
int encLen = Encrypt(body);
char encBody[encLen * 2 + 1];
BinaryToHex(body, encBody, encLen);
char *response = malloc(1);
response[0] = '\0';
SendCurlRequest(url, encBody, &response);
GetPlaylistReturn(response);
}
struct Station GetStationData(json_t *json)
{
struct Station *station = malloc(sizeof(struct Station));
json_t *name = json_object_get(json, "stationName");
json_t *id = json_object_get(json, "stationId");
station->name = malloc(json_string_length(name));
station->id = malloc(json_string_length(id));
strcpy(station->name, json_string_value(name));
strcpy(station->id, json_string_value(id));
return *station;
}
struct Station ParseStationList(json_t *root)
{
json_t *stations = json_object_get(json_object_get(root, "result"), "stations");
int ii;
int count = json_array_size(stations);
printf("Select a station (1-%i):\n", count - 1);
for(ii = 0; ii < count - 1; ii++)
{
json_t *curStation = json_array_get(stations, ii + 1);
printf("%i: %s\n", ii + 1, json_string_value(json_object_get(curStation, "stationName")));
}
int selection;
scanf("%i", &selection);
return GetStationData(json_array_get(stations, selection));
}
struct Station GetStationListReturn(char *response)
{
json_error_t error;
json_t *root = json_loads(response, 0, &error);
if(!root)
{
printf("Error: %s\n", error.text);
exit(EXIT_FAILURE);
}
json_t *stat = json_object_get(root, "stat");
if(strcmp(json_string_value(stat),"ok") != 0)
{
printf("OK was not returned while getting station list.\n");
printf("%s\n", response);
exit(EXIT_FAILURE);
}
return ParseStationList(root);
}
void GetStationList(struct Auth *auth)
{
char url[200] = "http://tuner.pandora.com/services/json/?method=user.getStationList&partner_id=";
strcat(url, auth->partnerId);
strcat(url, "&auth_token=");
strcat(url, auth->escapedToken);
strcat(url, "&user_id=");
strcat(url, auth->userId);
char body[500] = "{\"includeStationArtUrl\": true,\"userAuthToken\":\"";
strcat(body, auth->token);
strcat(body, "\",\"syncTime\":");
sprintf(body, "%s%ld", body, GetCurrentSyncTime(auth->sync));
strcat(body, "}");
int encLen = Encrypt(body);
char encBody[encLen * 2 + 1];
BinaryToHex(body, encBody, encLen);
char *response = malloc(1);
response[0] = '\0';
SendCurlRequest(url, encBody, &response);
GetPlaylist(auth, GetStationListReturn(response));
}