-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollection.cpp
More file actions
109 lines (89 loc) · 2.51 KB
/
collection.cpp
File metadata and controls
109 lines (89 loc) · 2.51 KB
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
#include "collection.h"
#include <iostream>
using json = nlohmann::json;
using namespace std;
const std::set<std::string> Collection::sort_keys = {
"label", "artist", "title", "catno", "format", "rating", "added", "year" };
bool Collection::init()
{
/* ID 0, the `All` folder, which cannot have releases added to it, and
ID 1, the `Uncategorized` folder which requires a token to access.
*/
auto folder = mToken.empty() ? "0" : "1";
if (!mClient.init(mToken))
return false;
auto url = "https://api.discogs.com/users/"+mUser+"/collection/folders/"+folder+"/releases?sort="+mCriteria;
if (!getNextJson(url))
return false;
std::cout << "Collection of " << mUser << " listed by " << mCriteria << ":\n";
/* Parse JSON as descibed in:
* www.discogs.com/developers#page:user-collection,header:user-collection-collection-items-by-folder
*/
try
{
std::cout << "\titems = " << mJson["pagination"]["items"] << std::endl;
std::cout << "\tpages = " << mJson["pagination"]["pages"] << std::endl;
std::cout << "\tper_page = " << mJson["pagination"]["per_page"] << std::endl;
std::cout << "\tnext = " << mJson["pagination"]["urls"]["next"] << std::endl;
}
catch (json::exception &e)
{
std::cerr << __func__ <<": Error: " << e.what() << std::endl;
return false;
}
return true;
}
bool Collection::getNextJson(const string &url)
{
auto response = mClient.getNext(url);
if (response.empty())
return false;
try
{
mJson = json::parse(response);
return (!mJson.is_null());
}
catch (json::exception &e)
{
std::cerr << __func__ <<": Error: " << e.what() << std::endl;
return false;
}
return false;
}
bool Collection::downloadNextPage()
{
auto url = ""s;
try
{
auto next = mJson["pagination"]["urls"]["next"];
if (next.is_null())
return false;
url = next.get<string>();
}
catch (json::exception &e)
{
std::cerr << __func__ <<": Error: " << e.what() << std::endl;
return false;
}
return getNextJson(url);
}
std::vector<string> Collection::listReleases() const
{
std::vector<string> titles;
try
{
for (auto &item : mJson["releases"])
{
string artist = item["basic_information"]["artists"][0]["name"];
string title = item["basic_information"]["title"];
string label = item["basic_information"]["labels"][0]["name"];
int year = item["basic_information"]["year"];
titles.push_back(artist + " - " + title + " (" + label + ", " + std::to_string(year) + ")");
}
}
catch (json::exception &e)
{
std::cerr << __func__ <<": Error: " << e.what() << std::endl;
}
return titles;
}