92
90
/* Additional settings from LUA table */
94
if (lua_istable(l, 2)) {
95
fetch_type = lua_get_field_s(l, "fetch_type");
97
if (strcmp(fetch_type, "config") == 0)
98
fetch_type_n = QUVISTATUSTYPE_CONFIG;
99
else if (strcmp(fetch_type, "playlist") == 0)
100
fetch_type_n = QUVISTATUSTYPE_PLAYLIST;
102
arbitrary_cookie = lua_get_field_s(l, "arbitrary_cookie");
103
user_agent = lua_get_field_s(l, "user_agent");
106
if (quvi->status_func) {
107
if (quvi->status_func(makelong(QUVISTATUS_FETCH, fetch_type_n),
108
(void *)url) != QUVI_OK) {
109
return (QUVI_ABORTEDBYCALLBACK);
113
csetopt(CURLOPT_URL, url);
114
csetopt(CURLOPT_ENCODING, "");
92
if (lua_istable(l, 2))
94
fetch_type = lua_get_field_s(l, "fetch_type");
97
if (strcmp(fetch_type, "config") == 0)
98
fetch_type_n = QUVISTATUSTYPE_CONFIG;
99
else if (strcmp(fetch_type, "playlist") == 0)
100
fetch_type_n = QUVISTATUSTYPE_PLAYLIST;
102
arbitrary_cookie = lua_get_field_s(l, "arbitrary_cookie");
103
user_agent = lua_get_field_s(l, "user_agent");
106
if (video->quvi->status_func)
109
status_func(makelong(QUVISTATUS_FETCH, fetch_type_n),
110
(void *)url) != QUVI_OK)
112
return (QUVI_ABORTEDBYCALLBACK);
116
curl_easy_setopt(video->quvi->curl, CURLOPT_URL, url);
117
curl_easy_setopt(video->quvi->curl, CURLOPT_ENCODING, "");
116
119
memset(&mem, 0, sizeof(mem));
118
csetopt(CURLOPT_WRITEDATA, &mem);
120
if (quvi->write_func)
121
csetopt(CURLOPT_WRITEFUNCTION, (curl_write_callback) quvi->write_func);
123
csetopt(CURLOPT_WRITEFUNCTION, quvi_write_callback_default);
120
curl_easy_setopt(video->quvi->curl, CURLOPT_WRITEDATA, &mem);
122
curl_easy_setopt(video->quvi->curl, CURLOPT_WRITEFUNCTION,
123
video->quvi->write_func
124
? (curl_write_callback) video->quvi->write_func
125
: (curl_write_callback) quvi_write_callback_default);
125
127
if (arbitrary_cookie != NULL && *arbitrary_cookie != '\0')
126
csetopt(CURLOPT_COOKIE, arbitrary_cookie);
129
curl_easy_setopt(video->quvi->curl, CURLOPT_COOKIE,
128
133
if (user_agent != NULL && *user_agent != '\0')
129
csetopt(CURLOPT_USERAGENT, user_agent);
135
curl_easy_setopt(video->quvi->curl, CURLOPT_USERAGENT, user_agent);
131
curlcode = curl_easy_perform(quvi->curl);
138
curlcode = curl_easy_perform(video->quvi->curl);
136
curl_easy_getinfo(quvi->curl, CURLINFO_RESPONSE_CODE, &respcode);
138
curl_easy_getinfo(quvi->curl, CURLINFO_HTTP_CONNECTCODE, &conncode);
140
if (curlcode == CURLE_OK && respcode == 200) {
142
if (quvi->status_func) {
144
if (quvi->status_func(makelong(QUVISTATUS_FETCH, QUVISTATUSTYPE_DONE),
146
rc = QUVI_ABORTEDBYCALLBACK;
155
if (curlcode == CURLE_OK) {
156
seterr("server response code %ld (conncode=%ld)", respcode, conncode);
160
seterr("%s (curlcode=%d, conncode=%ld)",
161
curl_easy_strerror(curlcode), curlcode, conncode);
169
if (rc == QUVI_OK && !video->charset) /* charset */
170
run_lua_charset_func(video, mem.p);
143
curl_easy_getinfo(video->quvi->curl, CURLINFO_RESPONSE_CODE,
145
curl_easy_getinfo(video->quvi->curl, CURLINFO_HTTP_CONNECTCODE,
148
if (curlcode == CURLE_OK && respcode == 200)
150
if (video->quvi->status_func)
152
if (video->quvi->status_func(makelong(QUVISTATUS_FETCH,
153
QUVISTATUSTYPE_DONE),
156
rc = QUVI_ABORTEDBYCALLBACK;
162
if (curlcode == CURLE_OK)
164
freprintf(&video->quvi->errmsg,
165
"server response code %ld (conncode=%ld)", respcode,
170
freprintf(&video->quvi->errmsg,
171
"%s (curlcode=%d, conncode=%ld)",
172
curl_easy_strerror(curlcode), curlcode, conncode);
181
if (rc == QUVI_OK && !video->charset) /* charset */
182
run_lua_charset_func(video, mem.p);
173
185
video->quvi->httpcode = respcode;
174
186
video->quvi->curlcode = curlcode;
205
217
if (strcmp(strncpy(buf, qvl->url, strlen(scheme)), scheme) != 0)
206
218
return (QUVI_OK); /* Skip video URL verification discreetly. */
208
if (quvi->status_func) {
209
if (quvi->status_func(makelong(QUVISTATUS_VERIFY, 0), 0) != QUVI_OK) {
210
return (QUVI_ABORTEDBYCALLBACK);
220
if (quvi->status_func)
222
if (quvi->status_func(makelong(QUVISTATUS_VERIFY, 0), 0) != QUVI_OK)
224
return (QUVI_ABORTEDBYCALLBACK);
214
csetopt(CURLOPT_URL, qvl->url);
215
csetopt(CURLOPT_NOBODY, 1L); /* get -> head */
228
curl_easy_setopt(quvi->curl, CURLOPT_URL, qvl->url);
229
curl_easy_setopt(quvi->curl, CURLOPT_NOBODY, 1L); /* get -> head */
217
231
memset(&mem, 0, sizeof(mem));
218
csetopt(CURLOPT_WRITEDATA, &mem);
232
curl_easy_setopt(quvi->curl, CURLOPT_WRITEDATA, &mem);
220
if (quvi->write_func)
221
csetopt(CURLOPT_WRITEFUNCTION, (curl_write_callback) quvi->write_func);
223
csetopt(CURLOPT_WRITEFUNCTION, quvi_write_callback_default);
234
curl_easy_setopt(quvi->curl, CURLOPT_WRITEFUNCTION, (quvi->write_func)
235
? (curl_write_callback) quvi->write_func
236
: (curl_write_callback) quvi_write_callback_default);
225
238
curlcode = curl_easy_perform(quvi->curl);
227
csetopt(CURLOPT_HTTPGET, 1L); /* reset: head -> get */
240
curl_easy_setopt(quvi->curl, CURLOPT_HTTPGET, 1L); /* reset: head -> get */
233
246
curl_easy_getinfo(quvi->curl, CURLINFO_RESPONSE_CODE, &respcode);
235
247
curl_easy_getinfo(quvi->curl, CURLINFO_HTTP_CONNECTCODE, &conncode);
237
if (curlcode == CURLE_OK) {
239
if (respcode == 200 || respcode == 206) {
243
curl_easy_getinfo(quvi->curl, CURLINFO_CONTENT_TYPE, &ct);
245
_free(qvl->content_type);
246
asprintf(&qvl->content_type, "%s", ct);
248
curl_easy_getinfo(quvi->curl,
249
CURLINFO_CONTENT_LENGTH_DOWNLOAD, &qvl->length);
251
if (quvi->status_func) {
252
if (quvi->status_func(makelong(QUVISTATUS_VERIFY, QUVISTATUSTYPE_DONE),
254
rc = QUVI_ABORTEDBYCALLBACK;
259
/* Content-Type -> suffix. */
260
rc = run_lua_suffix_func(quvi, qvl);
263
seterr("server response code %ld (conncode=%ld)", respcode, conncode);
249
if (curlcode == CURLE_OK)
251
if (respcode == 200 || respcode == 206)
255
curl_easy_getinfo(quvi->curl, CURLINFO_CONTENT_TYPE, &ct);
257
_free(qvl->content_type);
258
asprintf(&qvl->content_type, "%s", ct);
260
curl_easy_getinfo(quvi->curl,
261
CURLINFO_CONTENT_LENGTH_DOWNLOAD, &qvl->length);
263
if (quvi->status_func)
265
if (quvi->status_func(makelong
266
(QUVISTATUS_VERIFY, QUVISTATUSTYPE_DONE),
269
rc = QUVI_ABORTEDBYCALLBACK;
275
/* Content-Type -> suffix. */
276
rc = run_lua_suffix_func(quvi, qvl);
281
freprintf(&quvi->errmsg,
282
"server response code %ld (conncode=%ld)", respcode,
289
freprintf(&quvi->errmsg, "%s (curlcode=%d, conncode=%ld)",
290
curl_easy_strerror(curlcode), curlcode, conncode);
267
seterr("%s (curlcode=%d, conncode=%ld)",
268
curl_easy_strerror(curlcode), curlcode, conncode);
272
294
quvi->httpcode = respcode;
273
295
quvi->curlcode = curlcode;
282
304
* Check if URL is a shortened URL (e.g. dai.ly, goo.gl, etc.) and
283
305
* replace the (shortened) video page URL with the redirect URL.
285
QUVIcode is_shortened_url(_quvi_video_t video)
307
QUVIcode is_shortened_url(_quvi_media_t video)
287
309
long respcode, conncode;
288
310
CURLcode curlcode;
289
311
struct mem_s mem;
293
314
assert(video != NULL);
295
assert(quvi != NULL);
297
if (quvi->status_func) {
299
const QUVIcode r = quvi->status_func(makelong(QUVISTATUS_SHORTENED, 0), 0);
302
return (QUVI_ABORTEDBYCALLBACK);
316
if (video->quvi->status_func)
318
if (video->quvi->status_func(QUVISTATUS_SHORTENED, 0) != QUVI_OK)
319
return (QUVI_ABORTEDBYCALLBACK);
306
322
memset(&mem, 0, sizeof(mem));
308
csetopt(CURLOPT_WRITEDATA, &mem);
310
if (quvi->write_func)
311
csetopt(CURLOPT_WRITEFUNCTION, (curl_write_callback) quvi->write_func);
313
csetopt(CURLOPT_WRITEFUNCTION, quvi_write_callback_default);
315
csetopt(CURLOPT_URL, video->page_link);
316
csetopt(CURLOPT_FOLLOWLOCATION, 0L);
317
csetopt(CURLOPT_NOBODY, 1L); /* get -> head */
319
curlcode = curl_easy_perform(quvi->curl);
321
csetopt(CURLOPT_FOLLOWLOCATION, 1L); /* reset */
322
csetopt(CURLOPT_HTTPGET, 1L); /* reset: head -> get */
323
curl_easy_setopt(video->quvi->curl, CURLOPT_WRITEDATA, &mem);
325
curl_easy_setopt(video->quvi->curl, CURLOPT_WRITEFUNCTION,
326
(video->quvi->write_func)
327
? (curl_write_callback) video->quvi->write_func
328
: (curl_write_callback) quvi_write_callback_default);
330
curl_easy_setopt(video->quvi->curl, CURLOPT_URL, video->page_link);
331
curl_easy_setopt(video->quvi->curl, CURLOPT_FOLLOWLOCATION, 0L);
332
curl_easy_setopt(video->quvi->curl, CURLOPT_NOBODY, 1L); /* get -> head */
334
curlcode = curl_easy_perform(video->quvi->curl);
336
curl_easy_setopt(video->quvi->curl, CURLOPT_FOLLOWLOCATION, 1L); /* reset */
338
curl_easy_setopt(video->quvi->curl, CURLOPT_HTTPGET, 1L); /* reset: head -> get */
328
curl_easy_getinfo(quvi->curl, CURLINFO_RESPONSE_CODE, &respcode);
329
curl_easy_getinfo(quvi->curl, CURLINFO_HTTP_CONNECTCODE, &conncode);
331
if (curlcode == CURLE_OK) {
333
if (respcode >= 301 && respcode <= 303) {
339
curl_easy_getinfo(quvi->curl, CURLINFO_REDIRECT_URL, &url);
340
setvid(video->page_link, "%s", url);
345
/* respcode >= 301 && respcode <= 303 */
347
/* Most likely not a shortened URL redirect. Let it pass. */
351
if (quvi->status_func) {
353
rc = quvi->status_func(makelong
354
(QUVISTATUS_SHORTENED, QUVISTATUSTYPE_DONE), 0);
360
seterr("%s (curlcode=%d, conncode=%ld)",
361
curl_easy_strerror(curlcode), curlcode, conncode);
344
curl_easy_getinfo(video->quvi->curl, CURLINFO_RESPONSE_CODE,
346
curl_easy_getinfo(video->quvi->curl, CURLINFO_HTTP_CONNECTCODE,
349
if (curlcode == CURLE_OK)
351
if (respcode >= 301 && respcode <= 303)
357
curl_easy_getinfo(video->quvi->curl, CURLINFO_REDIRECT_URL, &url);
358
freprintf(&video->page_link, "%s", url);
362
/* respcode >= 301 && respcode <= 303 */
365
/* Most likely not a shortened URL redirect. Let it pass. */
369
if (video->quvi->status_func)
372
makelong(QUVISTATUS_SHORTENED, QUVISTATUSTYPE_DONE);
374
rc = video->quvi->status_func(param, 0);
379
freprintf(&video->quvi->errmsg, "%s (curlcode=%d, conncode=%ld)",
380
curl_easy_strerror(curlcode), curlcode, conncode);
369
quvi->httpcode = respcode;
370
quvi->curlcode = curlcode;
388
video->quvi->httpcode = respcode;
389
video->quvi->curlcode = curlcode;
394
/* vim: set ts=2 sw=2 tw=72 expandtab: */