summaryrefslogtreecommitdiffstats
path: root/api/logic/modplatform/flame/UrlResolvingTask.cpp
blob: 068b6a3425efb8da2e483c841d4aec419dd61fb5 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "UrlResolvingTask.h"
#include <QtXml>

/*
namespace {
const char * metabase = "https://cursemeta.dries007.net";
}
*/

Flame::UrlResolvingTask::UrlResolvingTask(const QString& toProcess)
    : m_url(toProcess)
{
}

void Flame::UrlResolvingTask::executeTask()
{
    setStatus(tr("Resolving URL..."));
    setProgress(0, 1);
    m_dljob.reset(new NetJob("URL resolver"));

    weAreDigging = false;
    needle = QString();

    if(m_url.startsWith("https://")) {
        if(m_url.endsWith("?client=y")) {
            // https://www.curseforge.com/minecraft/modpacks/ftb-sky-odyssey/download?client=y
            // https://www.curseforge.com/minecraft/modpacks/ftb-sky-odyssey/download/2697088?client=y
            m_url.chop(9);
            // https://www.curseforge.com/minecraft/modpacks/ftb-sky-odyssey/download
            // https://www.curseforge.com/minecraft/modpacks/ftb-sky-odyssey/download/2697088
        }
        if(m_url.endsWith("/download")) {
            // https://www.curseforge.com/minecraft/modpacks/ftb-sky-odyssey/download -> need to dig inside html...
            weAreDigging = true;
            needle = m_url;
            needle.replace("https://", "twitch://");
            needle.replace("/download", "/download-client/");
            m_url.append("?client=y");
        } else if (m_url.contains("/download/")) {
            // https://www.curseforge.com/minecraft/modpacks/ftb-sky-odyssey/download/2697088
            m_url.replace("/download/", "/download-client/");
        }
    }
    else if(m_url.startsWith("twitch://")) {
        // twitch://www.curseforge.com/minecraft/modpacks/ftb-sky-odyssey/download-client/2697088
        m_url.replace(0, 9, "https://");
        // https://www.curseforge.com/minecraft/modpacks/ftb-sky-odyssey/download-client/2697088
    }
    auto dl = Net::Download::makeByteArray(QUrl(m_url), &results);
    m_dljob->addNetAction(dl);
    connect(m_dljob.get(), &NetJob::finished, this, &Flame::UrlResolvingTask::netJobFinished);
    m_dljob->start();
}

void Flame::UrlResolvingTask::netJobFinished()
{
    if(weAreDigging) {
        processHTML();
    } else {
        processCCIP();
    }
}

void Flame::UrlResolvingTask::processHTML()
{
    QString htmlDoc = QString::fromUtf8(results);
    auto index = htmlDoc.indexOf(needle);
    if(index < 0) {
        emitFailed(tr("Couldn't find the needle in the haystack..."));
        return;
    }
    auto indexStart = index;
    int indexEnd = -1;
    while((index + 1) < htmlDoc.size() && htmlDoc[index] != '"') {
        index ++;
        if(htmlDoc[index] == '"') {
            indexEnd = index;
            break;
        }
    }
    if(indexEnd > 0) {
        QString found = htmlDoc.mid(indexStart, indexEnd - indexStart);
        qDebug() << "Found needle: " << found;
        // twitch://www.curseforge.com/minecraft/modpacks/ftb-sky-odyssey/download-client/2697088
        m_url = found;
        executeTask();
        return;
    }
    emitFailed(tr("Couldn't find the end of the needle in the haystack..."));
    return;
}

void Flame::UrlResolvingTask::processCCIP()
{
    QDomDocument doc;
    if (!doc.setContent(results)) {
        qDebug() << results;
        emitFailed(tr("Resolving failed."));
        return;
    }
    auto packageNode = doc.namedItem("package");
    if(!packageNode.isElement()) {
        emitFailed(tr("Resolving failed: missing package root element."));
        return;
    }
    auto projectNode = packageNode.namedItem("project");
    if(!projectNode.isElement()) {
        emitFailed(tr("Resolving failed: missing project element."));
        return;
    }
    auto attribs = projectNode.attributes();

    auto projectIdNode = attribs.namedItem("id");
    if(!projectIdNode.isAttr()) {
        emitFailed(tr("Resolving failed: missing id attribute."));
        return;
    }
    auto fileIdNode = attribs.namedItem("file");
    if(!fileIdNode.isAttr()) {
        emitFailed(tr("Resolving failed: missing file attribute."));
        return;
    }

    auto projectId = projectIdNode.nodeValue();
    auto fileId = fileIdNode.nodeValue();
    bool success = true;
    m_result.projectId = projectId.toInt(&success);
    if(!success) {
        emitFailed(tr("Failed to resove projectId as a number."));
        return;
    }
    m_result.fileId = fileId.toInt(&success);
    if(!success) {
        emitFailed(tr("Failed to resove fileId as a number."));
        return;
    }
    qDebug() << "Resolved" << m_url << "as" << m_result.projectId << "/" << m_result.fileId;
    emitSucceeded();
}