summaryrefslogtreecommitdiffstats
path: root/stream/resolve/resolve_quvi.c
blob: 837496081a7fd6cd6c31018c247bc400a97dc023 (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
/*
 * This file is part of mpv.
 *
 * mpv is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * mpv is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with mpv; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <quvi/quvi.h>

#include "talloc.h"
#include "common/global.h"
#include "common/msg.h"
#include "options/options.h"
#include "resolve.h"

static void add_source(struct mp_resolve_result *res, const char *url,
                       const char *encid)
{
    struct mp_resolve_src *src = talloc_ptrtype(res, src);
    *src = (struct mp_resolve_src) {
        .url = talloc_strdup(src, url),
        .encid = talloc_strdup(src, encid),
    };
    MP_TARRAY_APPEND(res, res->srcs, res->num_srcs, src);
}

struct mp_resolve_result *mp_resolve_quvi(const char *url,
                                          struct mpv_global *global)
{
    QUVIcode rc;
    bool mp_url = false;

    quvi_t q;
    rc = quvi_init(&q);
    if (rc != QUVI_OK)
        return NULL;

    if (!strncmp(url, "mp_", 3)) {
        url += 3;
        mp_url = true;
    }

    // Don't try to use quvi on an URL that's not directly supported, since
    // quvi will do a network access anyway in order to check for HTTP
    // redirections etc.
    // The documentation says this will fail on "shortened" URLs.
    if (quvi_supported(q, (char *)url) != QUVI_OK) {
        quvi_close(&q);
        return NULL;
    }

    struct MPOpts *opts = global->opts;
    struct mp_log *log = mp_log_new(NULL, global->log, "quvi");

    mp_info(log, "Checking URL...\n");

    const char *req_format = opts->quvi_format ? opts->quvi_format : "best";

    // Can use quvi_query_formats() to get a list of formats like this:
    // "fmt05_240p|fmt18_360p|fmt34_360p|fmt35_480p|fmt43_360p|fmt44_480p"
    // (This example is youtube specific.)
    // That call requires an extra net access. quvi_next_media_url() doesn't
    // seem to do anything useful. So we can't really do anything useful
    // except pass through the user's format setting.
    quvi_setopt(q, QUVIOPT_FORMAT, req_format);

    quvi_media_t m;
    rc = quvi_parse(q, (char *)url, &m);
    if (rc != QUVI_OK) {
        mp_err(log, "%s\n", quvi_strerror(q, rc));
        quvi_close(&q);
        talloc_free(log);
        return NULL;
    }

    struct mp_resolve_result *result
        = talloc_zero(NULL, struct mp_resolve_result);

    char *val;

    if (quvi_getprop(m, QUVIPROP_MEDIAURL, &val) == QUVI_OK) {
        if (mp_url)
            result->url = talloc_asprintf(result, "mp_%s", val);
        else
            result->url = talloc_strdup(result, val);
    }

    if (quvi_getprop(m, QUVIPROP_PAGETITLE, &val) == QUVI_OK)
        result->title = talloc_strdup(result, val);

    quvi_parse_close(&m);
    quvi_close(&q);

    if (!result->url) {
        talloc_free(result);
        result = NULL;
    }

    // Useful for quvi-format cycling
    add_source(result, NULL, "default");
    add_source(result, NULL, "best");
    if (strcmp(req_format, "best") != 0 && strcmp(req_format, "default") != 0)
        add_source(result, NULL, req_format);

    talloc_free(log);
    return result;
}