/
nicodown.c
377 lines (344 loc) · 10.4 KB
/
nicodown.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
//#define CURL_STATICLIB
#include <stdlib.h>
#include <memory.h>
#include <curl/curl.h>
#ifdef USE_LIBXML
#include <libxml/parser.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
#endif
#define HEX_DIGITS "0123456789ABCDEF"
#define IS_QUOTED(x) (*x == '%' && strchr(HEX_DIGITS, *(x+1)) && strchr(HEX_DIGITS, *(x+2)))
#define WATCH_URL_BASE "http://www.nicovideo.jp/watch/"
typedef struct {
char* data; // response data from server
size_t size; // response size of data
} MEMFILE;
MEMFILE*
memfopen() {
MEMFILE* mf = (MEMFILE*) malloc(sizeof(MEMFILE));
mf->data = NULL;
mf->size = 0;
return mf;
}
void
memfclose(MEMFILE* mf) {
if (mf->data) free(mf->data);
free(mf);
}
size_t
memfwrite(char* ptr, size_t size, size_t nmemb, void* stream) {
MEMFILE* mf = (MEMFILE*) stream;
int block = size * nmemb;
if (!mf->data)
mf->data = (char*) malloc(block);
else
mf->data = (char*) realloc(mf->data, mf->size + block);
if (mf->data) {
memcpy(mf->data + mf->size, ptr, block);
mf->size += block;
}
return block;
}
char*
memfstrdup(MEMFILE* mf) {
char* buf = malloc(mf->size + 1);
memcpy(buf, mf->data, mf->size);
buf[mf->size] = 0;
return buf;
}
int
progress(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow) {
double val;
if (dltotal == 0) dltotal = 1.0;
val = dlnow / dltotal * 100;
printf("\r[%s] %3.0f%% ", (char*) clientp, val);
fflush(stdout);
return 0;
}
int
main(int argc, char* argv[]) {
CURL* curl = NULL;
CURLcode res;
int status = 0;
char error[CURL_ERROR_SIZE];
char fname[256];
char line[256];
char cookie[2048];
char query[2048];
char* buf = NULL;
char* ptr = NULL;
char* tmp = NULL;
char* id;
FILE* fp = NULL;
MEMFILE* mf; // mem file
MEMFILE* hf; // mem file for header
// usage
if (argc != 2) {
fputs("usage: nicodown [video_id]\n", stderr);
goto leave;
}
if (!(ptr = getenv("HOME"))) {
#ifdef _WIN32
if (!(ptr = getenv("USERPROFILE"))) {
fprintf(stderr, "failed to get HOME directory\n");
goto leave;
}
#else
fprintf(stderr, "failed to get HOME directory\n");
goto leave;
#endif
}
sprintf(fname, "%s/.nicodownrc", ptr);
fp = fopen(fname, "r");
if (!fp || !fgets(line, sizeof(line), fp)) {
if (fp) fclose(fp);
fprintf(stderr, "failed to read ~/.nicodownrc\n");
goto leave;
}
fclose(fp);
tmp = strpbrk(line, "\r\n");
if (tmp) *tmp = 0;
ptr = strchr(line, ':');
if (!ptr) {
fprintf(stderr, "failed to parse ~/.nicodownrc\n");
goto leave;
}
*ptr++ = 0;
id = argv[1];
if (!strncmp(id, WATCH_URL_BASE, strlen(WATCH_URL_BASE))) {
id += strlen(WATCH_URL_BASE);
}
sprintf(query, "mail_tel=%s&password=%s&next_url=/watch/%s", line, ptr, id);
// default filename
sprintf(fname, "%s.flv", id);
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, memfwrite);
// login
mf = memfopen();
hf = memfopen();
curl_easy_setopt(curl, CURLOPT_URL, "https://secure.nicovideo.jp/secure/login?site=niconico");
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, query);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, mf);
curl_easy_setopt(curl, CURLOPT_HEADERDATA, hf);
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, memfwrite);
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
fputs(error, stderr);
memfclose(mf);
memfclose(hf);
goto leave;
}
// parse cookie
memset(cookie, 0, sizeof(cookie));
buf = memfstrdup(hf);
tmp = buf;
while (tmp = strstr(tmp, "Set-Cookie: ")) {
ptr = tmp;
tmp = strpbrk(ptr, "\r\n;");
if (tmp) *tmp = 0;
strcpy(cookie, ptr + 12);
if (strncmp(cookie, "user_session=user", 17) == 0) {
curl_easy_setopt(curl, CURLOPT_COOKIE, cookie);
break;
}
tmp++;
}
free(buf);
memfclose(mf);
memfclose(hf);
mf = memfopen();
hf = memfopen();
curl_easy_setopt(curl, CURLOPT_WRITEDATA, mf);
curl_easy_setopt(curl, CURLOPT_HEADERDATA, hf);
// redirect
sprintf(query, "http://www.nicovideo.jp/watch/%s", id);
curl_easy_setopt(curl, CURLOPT_URL, query);
curl_easy_setopt(curl, CURLOPT_POST, 0);
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
fputs(error, stderr);
memfclose(mf);
memfclose(hf);
goto leave;
}
// parse cookie
buf = memfstrdup(hf);
ptr = NULL;
tmp = buf;
while (tmp = strstr(tmp, "Set-Cookie: ")) {
ptr = tmp + 12;
tmp = strpbrk(ptr, "\r\n;");
if (tmp) *tmp = 0;
strcat(cookie, ";");
strcat(cookie, ptr);
tmp++;
}
curl_easy_setopt(curl, CURLOPT_COOKIE, cookie);
free(buf);
// parse response body
buf = memfstrdup(mf);
if (buf && strstr(buf, "id=\"login_bar\"")) {
free(buf);
fprintf(stderr, "failed to login\n");
memfclose(mf);
memfclose(hf);
goto leave;
}
free(buf);
memfclose(hf);
memfclose(mf);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, NULL);
curl_easy_setopt(curl, CURLOPT_HEADERDATA, NULL);
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
// get video query, and get filename
sprintf(query, "http://www.nicovideo.jp/api/getthumbinfo?v=%s", id);
mf = memfopen();
curl_easy_setopt(curl, CURLOPT_URL, query);
curl_easy_setopt(curl, CURLOPT_POST, 0);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, mf);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, memfwrite);
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
fputs(error, stderr);
memfclose(mf);
goto leave;
}
#ifdef USE_LIBXML
// parse title node
{
xmlDocPtr doc = NULL;
xmlXPathContextPtr xpathctx;
xmlXPathObjectPtr xpathobj;
doc = xmlParseMemory(mf->data, mf->size);
xpathctx = doc ? xmlXPathNewContext(doc) : NULL;
xpathobj = xpathctx ? xmlXPathEvalExpression((xmlChar*) "//title", xpathctx) : NULL;
if (xpathobj) {
int n;
xmlNodeSetPtr nodes = xpathobj->nodesetval;
for(n = 0; nodes && n < xmlXPathNodeSetGetLength(nodes); n++) {
xmlNodePtr node = nodes->nodeTab[n];
if(node->type != XML_ATTRIBUTE_NODE && node->type != XML_ELEMENT_NODE && node->type != XML_CDATA_SECTION_NODE) continue;
if (node->type == XML_CDATA_SECTION_NODE)
sprintf(fname, "%s.flv", (char*) node->content);
else
if (node->children)
sprintf(fname, "%s.flv", (char*) node->children->content);
break;
}
}
if (xpathobj ) xmlXPathFreeObject(xpathobj);
if (xpathctx) xmlXPathFreeContext(xpathctx);
if (doc) xmlFreeDoc(doc);
}
#else
buf = memfstrdup(mf);
ptr = buf ? strstr(buf, "<title>") : NULL;
if (ptr) {
ptr += 7;
tmp = strstr(ptr, "</title>");
if (*tmp) {
*tmp = 0;
sprintf(fname, "%s.flv", ptr);
}
}
if (buf) free(buf);
#endif
memfclose(mf);
#ifdef _WIN32
{
UINT codePage;
size_t wcssize;
wchar_t* wcsstr;
size_t mbssize;
char* mbsstr;
codePage = CP_UTF8;
wcssize = MultiByteToWideChar(codePage, 0, fname, -1, NULL, 0);
wcsstr = (wchar_t*) malloc(sizeof(wchar_t) * (wcssize + 1));
wcssize = MultiByteToWideChar(codePage, 0, fname, -1, wcsstr, wcssize + 1);
wcsstr[wcssize] = 0;
codePage = GetACP();
mbssize = WideCharToMultiByte(codePage, 0, (LPCWSTR) wcsstr,-1,NULL,0,NULL,NULL);
mbsstr = (char*) malloc(mbssize+1);
mbssize = WideCharToMultiByte(codePage, 0, (LPCWSTR) wcsstr, -1, mbsstr, mbssize, NULL, NULL);
mbsstr[mbssize] = 0;
strcpy(fname, mbsstr);
free(mbsstr);
free(wcsstr);
}
#endif
// get video query
sprintf(query, "http://flapi.nicovideo.jp/api/getflv?v=%s", id);
mf = memfopen();
curl_easy_setopt(curl, CURLOPT_URL, query);
curl_easy_setopt(curl, CURLOPT_POST, 0);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, mf);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, memfwrite);
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
fputs(error, stderr);
memfclose(mf);
goto leave;
}
buf = memfstrdup(mf);
ptr = strstr(buf, "url=");
if (!ptr) {
if (buf) free(buf);
fprintf(stderr, "failed to get video info\n");
memfclose(mf);
goto leave;
}
tmp = strstr(ptr, "&");
if (tmp) *tmp = 0;
tmp = ptr;
while(*tmp) {
if (IS_QUOTED(tmp)) {
unsigned int num = 0;
sscanf(tmp+1, "%02x", &num);
*tmp = (char)num;
strcpy(tmp + 1, tmp + 3);
}
tmp++;
}
strcpy(query, ptr + 4);
printf("URL: %s\n", query);
free(buf);
memfclose(mf);
// sanitize filename
ptr = fname;
while (*ptr) {
if (strchr("\\/|:<>\"?*", *ptr)) *ptr = '_';
ptr++;
}
// download video
fp = fopen(fname, "wb");
if (!fp) {
fprintf(stderr, "failed to open file: %s\n", fname);
goto leave;
}
curl_easy_setopt(curl, CURLOPT_URL, query);
curl_easy_setopt(curl, CURLOPT_POST, 0);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress);
curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, (void*)fname);
curl_easy_setopt(curl, CURLOPT_BUFFERSIZE, BUFSIZ);
/* curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); */
res = curl_easy_perform(curl);
fclose(fp);
if (res != CURLE_OK) {
fputs(error, stderr);
goto leave;
}
leave:
if (curl) curl_easy_cleanup(curl);
printf("\n");
return 0;
}