forked from peterkvt80/vbit2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspoteletext.h
94 lines (77 loc) · 2.18 KB
/
spoteletext.h
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
#pragma once
#include <curl/curl.h>
#include <jpeglib.h>
#include <chrono>
#include <memory>
#include <string>
#include "image.h"
extern "C" {
#include <jq.h>
}
namespace teletext {
struct NowPlaying {
std::string track_id;
std::string context_href;
std::string context;
std::string title;
std::string artist;
std::string image;
std::string uri;
std::chrono::milliseconds progress;
std::chrono::milliseconds duration;
};
struct Scannable {
uint64_t id0;
uint64_t id1;
};
class Spoteletext {
private:
enum AuthResult {
kAuthSuccess = 0,
kAuthError,
};
enum PollResult {
kPollSuccess = 0,
kPollWait,
kPollError,
};
public:
Spoteletext(CURL *curl, jq_state *jq, const std::string &page_dir);
~Spoteletext() = default;
int run();
private:
// spotify device code auth flow
void authenticate();
AuthResult authenticateCode(const std::string &device_code,
const std::string &user_code,
const std::string &verification_url,
const std::chrono::seconds &expires_in,
const std::chrono::seconds &interval);
AuthResult getAuthCode(const std::string &device_code,
const std::chrono::seconds &expires_in,
const std::chrono::seconds &interval,
std::string &auth_code);
PollResult pollAuthCode(const std::string &device_code, std::string &auth_code);
bool fetchTokens(const std::string &auth_code);
bool refreshToken();
// update loop
void loop();
bool fetchNowPlaying(bool retry);
void fetchContext(const std::string &url);
void fetchImage(const std::string &url);
void fetchScannable(const std::string &uri);
void displayCode(const std::string &code, const std::string &verification_url);
void displayNPV();
void displayScannable();
CURL *_curl{nullptr};
jq_state *_jq{nullptr};
std::string _access_token;
std::string _refresh_token;
bool _has_played{false};
std::string _out_file;
std::string _scannable_file;
NowPlaying _now_playing;
std::unique_ptr<teletext::Image> _image;
Scannable _scannable{};
};
} // namespace teletext