-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathDiscogsArtist.cs
298 lines (283 loc) · 11 KB
/
DiscogsArtist.cs
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
namespace discogs.Artists
{
public class artist : IExportable
{
private static readonly Dictionary<string, string[]> CsvExportHeaders = new Dictionary<string, string[]>
{
{ "artist", "id name realname profile data_quality".Split(" ") },
{ "artist_alias", "artist_id alias_name".Split(" ") },
{ "artist_namevariation", "artist_id name".Split(" ") },
{ "artist_url", "artist_id url".Split(" ") },
{ "group_member", "group_artist_id member_artist_id member_name".Split(" ") },
{ "artist_image", "artist_id type width height".Split(" ") },
};
public image[] images { get; set; }
[XmlArrayItem("url")]
public string[] urls { get; set; }
public string id { get; set; }
public string name { get; set; }
public string realname { get; set; }
public string profile { get; set; }
public string data_quality { get; set; }
[XmlArrayItem("name")]
public string[] namevariations { get; set; }
public name[] members { get; set; }
public name[] aliases { get; set; }
// groups is not parsed in the python version
public name[] groups {get;set;}
public override string ToString() => this.id;
public IEnumerable<(string StreamName, string[] RowValues)> Export()
{
yield return ("artist", new[] { id, this.name, realname, profile, data_quality });
foreach (var a in (aliases ?? System.Array.Empty<name>()))
{
yield return ("artist_alias", new[] { id, a.value });
}
foreach (var nv in (namevariations ?? System.Array.Empty<string>()))
{
yield return ("artist_namevariation", new[] { id, nv });
}
foreach (var u in (urls ?? System.Array.Empty<string>()))
{
yield return ("artist_url", new[] { id, u });
}
foreach (var m in (members ?? System.Array.Empty<name>()))
{
yield return ("group_member", new[] { id, m.id, m.value });
}
if ((images?.Length ?? 0) > 0)
{
foreach (var image in this.images)
{
yield return ("artist_image", new[] { this.id, image.type, image.width, image.height });
}
}
}
public IReadOnlyDictionary<string, string[]> GetExportStreamsAndFields()
=> CsvExportHeaders;
public bool IsValid() => !string.IsNullOrEmpty(id);
/// <summary>
/// Populates the current object from an XMl reader.
/// </summary>
/// <param name="reader">An XML reader positioned right after the <![CDATA[<artist>]]> node.</param>
public void Populate(XmlReader reader) => Populate2(reader);
public void Populate2(XmlReader reader)
{
if (reader.Name != "artist")
{
return;
}
// <master id="123"> unlike all others
reader.Read();
while (!reader.EOF)
{
switch (reader.Name)
{
case "artist":
// it's back on a release node (EndElement); release control
return;
case "images":
this.images = image.Parse(reader);
break;
case "id":
this.id = reader.ReadElementContentAsString();
break;
case "name":
this.name = reader.ReadElementContentAsString();
break;
case "realname":
this.realname = reader.ReadElementContentAsString();
break;
case "profile":
this.profile = reader.ReadElementContentAsString();
break;
case "data_quality":
this.data_quality = reader.ReadElementContentAsString();
break;
case "urls":
this.urls = reader.ReadChildren("url");
break;
case "namevariations":
this.namevariations = reader.ReadChildren("name");
break;
case "members":
this.members = discogs.Artists.name.Parse(reader, "members");
break;
case "aliases":
this.aliases = discogs.Artists.name.Parse(reader, "aliases");
break;
case "groups":
this.groups = discogs.Artists.name.Parse(reader, "groups");
break;
default:
reader.Read();
break;
}
if (reader.NodeType == XmlNodeType.EndElement)
{
if (reader.Name == "artist")
{
return;
}
reader.Skip();
}
}
}
public void Populate1(XmlReader reader)
{
while (reader.Read())
{
if (reader.IsStartElement("artist"))
{
// that means we encountered the next node
return;
}
if (reader.IsStartElement("id"))
{
this.id = reader.ReadElementContentAsString();
}
if (reader.IsStartElement("name"))
{
this.name = reader.ReadElementContentAsString();
}
if (reader.IsStartElement("realname"))
{
this.realname = reader.ReadElementContentAsString();
}
if (reader.IsStartElement("profile"))
{
this.profile = reader.ReadElementContentAsString();
}
if (reader.IsStartElement("data_quality"))
{
this.data_quality = reader.ReadElementContentAsString();
}
if (reader.IsStartElement("namevariations"))
{
reader.Read();
var nvs = new List<string>();
while (reader.IsStartElement("name"))
{
var nv = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(nv))
nvs.Add(nv);
}
this.namevariations = nvs.ToArray();
}
if (reader.IsStartElement("members"))
{
reader.Read(); // move inside members
var members = new List<name>();
while (reader.IsStartElement("name") || reader.IsStartElement("id"))
{
if (reader.IsStartElement("id"))
{
reader.Skip();
continue;
}
var n = new name
{
id = reader.GetAttribute("id"),
value = reader.ReadElementContentAsString(),
};
members.Add(n);
}
this.members = members.ToArray();
}
if (reader.IsStartElement("aliases"))
{
reader.Read();
var aliases = new List<name>();
while (reader.IsStartElement("name"))
{
var n = new name
{
id = reader.GetAttribute("id"),
value = reader.ReadElementContentAsString(),
};
aliases.Add(n);
}
this.aliases = aliases.ToArray();
}
if (reader.IsStartElement("groups"))
{
reader.Read();
var names = new List<name>();
while (reader.IsStartElement("name"))
{
var n = new name
{
id = reader.GetAttribute("id"),
value = reader.ReadElementContentAsString(),
};
names.Add(n);
}
this.groups = names.ToArray();
}
if (reader.IsStartElement("images"))
{
var images = new List<image>();
while (reader.Read() && reader.IsStartElement("image"))
{
var image = new image { type = reader.GetAttribute("type"), width = reader.GetAttribute("width"), height = reader.GetAttribute("height") };
images.Add(image);
}
this.images = images.ToArray();
}
if (reader.IsStartElement("urls"))
{
reader.Read();
var urls = new List<string>();
while (reader.IsStartElement("url"))
{
var url = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(url))
urls.Add(url);
}
this.urls = urls.ToArray();
}
}
}
}
public class name
{
[XmlAttribute]
public string id { get; set; }
[XmlText]
public string value { get; set; }
public static name[] Parse(XmlReader reader, string parentName)
{
if (reader.IsEmptyElement) {
reader.Skip();
return System.Array.Empty<name>();
}
// expects to be on <parentName> node
reader.Read();
var list = new List<name>();
while (!reader.EOF)
{
if (reader.Name == parentName) {
break;
}
if (reader.Name == "name") {
var obj = new name {
id = reader.GetAttribute("id"),
value = reader.ReadElementContentAsString(),
};
list.Add(obj);
}
else {
reader.Skip();
}
}
if (reader.NodeType == XmlNodeType.EndElement)
{
reader.Skip();
}
return list.ToArray();
}
}
}