]> git.sur5r.net Git - minitube/blob - src/httputils.cpp
Upload 3.9.3-2 to unstable
[minitube] / src / httputils.cpp
1 #include "httputils.h"
2 #include "cachedhttp.h"
3 #include "constants.h"
4 #include "http.h"
5 #include "localcache.h"
6 #include "throttledhttp.h"
7
8 Http &HttpUtils::notCached() {
9     static Http *h = [] {
10         Http *http = new Http;
11         http->addRequestHeader("User-Agent", userAgent());
12
13         return http;
14     }();
15     return *h;
16 }
17
18 Http &HttpUtils::cached() {
19     static Http *h = [] {
20         Http *http = new Http;
21         http->addRequestHeader("User-Agent", userAgent());
22
23         CachedHttp *cachedHttp = new CachedHttp(*http, "http");
24
25         return cachedHttp;
26     }();
27     return *h;
28 }
29
30 Http &HttpUtils::yt() {
31     static Http *h = [] {
32         Http *http = new Http;
33         http->addRequestHeader("User-Agent", stealthUserAgent());
34
35         CachedHttp *cachedHttp = new CachedHttp(*http, "yt");
36         cachedHttp->setMaxSeconds(3600);
37
38         return cachedHttp;
39     }();
40     return *h;
41 }
42
43 Http &HttpUtils::stealthAndNotCached() {
44     static Http *h = [] {
45         Http *http = new Http;
46         http->addRequestHeader("User-Agent", stealthUserAgent());
47
48         return http;
49     }();
50     return *h;
51 }
52
53 void HttpUtils::clearCaches() {
54     LocalCache::instance("yt")->clear();
55     LocalCache::instance("http")->clear();
56 }
57
58 const QByteArray &HttpUtils::userAgent() {
59     static const QByteArray ua = [] {
60         return QString(QLatin1String(Constants::NAME) + QLatin1Char('/') +
61                        QLatin1String(Constants::VERSION) + QLatin1String(" ( ") +
62                        Constants::WEBSITE + QLatin1String(" )"))
63                 .toUtf8();
64     }();
65     return ua;
66 }
67
68 const QByteArray &HttpUtils::stealthUserAgent() {
69     static const QByteArray ua =
70             "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like "
71             "Gecko) Chrome/79.0.3945.79 Safari/537.36";
72     return ua;
73 }