-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathDiscogsLabel.cs
150 lines (133 loc) · 5.16 KB
/
DiscogsLabel.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
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
namespace discogs.Labels
{
public class label : IExportable
{
private static readonly Dictionary<string, string[]> CsvExportHeaders = new Dictionary<string, string[]>
{
["label"] = new[] { "id", "name", "contact_info", "profile", "parent_name", "data_quality" },
["label_url"] = new[] { "label_id", "url" },
["label_image"] = new[] { "label_id", "type", "width", "height" },
};
public image[] images { get; set; }
public string id { get; set; }
public string name { get; set; }
private string contactinfo1;
public string Getcontactinfo()
{
return contactinfo1;
}
public void Setcontactinfo(string value)
{
contactinfo1 = value;
}
public string profile { get; set; }
public string data_quality { get; set; }
public parentLabel parentLabel { get; set; }
[XmlArrayItem("url")]
public string[] urls { get; set; }
[XmlAttribute("id")]
public string SubId { get; set; }
[XmlText]
public string SubName { get; set; }
public label[] sublabels { get; set; }
/// <summary>
/// Gets the possible export schemes for the class
/// </summary>
/// <returns>A read-only dictionary where the key is the type of export stream and the values are the headers/columns/fields exported.</returns>
public IReadOnlyDictionary<string, string[]> GetExportStreamsAndFields() => CsvExportHeaders;
/// <summary>
/// Exports instance to CSV.
/// </summary>
/// <returns>Tuples where the StreamName matches a key from <see ref="GetCsvExportScheme"> </returns>
public IEnumerable<(string StreamName, string[] RowValues)> Export()
{
yield return ("label", new[] { this.id, this.name, this.Getcontactinfo(), this.profile, this.parentLabel?.name, this.data_quality });
if ((urls?.Length ?? 0) > 0)
{
foreach (var url in urls)
{
if (string.IsNullOrEmpty(url)) continue;
yield return ("label_url", new[] { this.id, url });
}
}
if ((images?.Length ?? 0) > 0)
{
foreach (var image in this.images)
{
yield return ("label_image", new[] { this.id, image.type, image.width, image.height });
}
}
}
public void Populate(XmlReader reader)
{
while (reader.Read())
{
if (reader.IsStartElement("label"))
{
return;
}
if (reader.IsStartElement("id"))
{
this.id = reader.ReadElementContentAsString();
}
if (reader.IsStartElement("name"))
{
this.name = reader.ReadElementContentAsString();
}
if (reader.IsStartElement("contactinfo"))
{
this.Setcontactinfo(reader.ReadElementContentAsString());
}
if (reader.IsStartElement("profile"))
{
this.profile = reader.ReadElementContentAsString();
}
if (reader.IsStartElement("data_quality"))
{
this.data_quality = reader.ReadElementContentAsString();
}
if (reader.IsStartElement("parentLabel"))
{
this.parentLabel = new discogs.Labels.parentLabel
{
id = reader.GetAttribute("id"),
name = reader.ReadElementContentAsString()
};
}
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 bool IsValid() => !string.IsNullOrEmpty(id);
}
public class parentLabel
{
[XmlAttribute]
public string id { get; set; }
[XmlText]
public string name { get; set; }
}
}