]> git.sur5r.net Git - minitube/blob - src/httputils.cpp
fe007cbb00df0c48ad7e65109bf49fe8c83fe9f6
[minitube] / src / httputils.cpp
1 #include "httputils.h"
2 #include "constants.h"
3 #include "http.h"
4 #include "throttledhttp.h"
5 #include "cachedhttp.h"
6 #include "localcache.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 void HttpUtils::clearCaches() {
44     LocalCache::instance("yt")->clear();
45     LocalCache::instance("http")->clear();
46 }
47
48 const QByteArray &HttpUtils::userAgent() {
49     static const QByteArray ua = [] {
50         return QString(QLatin1String(Constants::NAME)
51                        + QLatin1Char('/') + QLatin1String(Constants::VERSION)
52                        + QLatin1String(" ( ") + Constants::WEBSITE + QLatin1String(" )")).toUtf8();
53     }();
54     return ua;
55 }
56
57 const QByteArray &HttpUtils::stealthUserAgent() {
58     static const QByteArray ua = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36";
59     return ua;
60 }