3
public class LastFM.AlbumInfo : Object {
4
static const string api = "a40ea1720028bd40c66b17d7146b3f3b";
5
BeatBox.FileOperator fo;
8
private string _artist;
11
private string _releasedate;
13
private int _listeners;
14
private int _playcount;
16
private LastFM.Image _url_image;
18
private Gee.ArrayList<LastFM.Tag> _tags;
20
private LastFM.Tag tagToAdd;
22
public AlbumInfo.basic() {
23
_name = "Unkown Album";
24
_artist = "Unkown Artist";
25
_tags = new Gee.ArrayList<LastFM.Tag>();
26
url_image = new LastFM.Image.basic();
29
public AlbumInfo.with_info(string artist, string album) {
30
string album_fixed = LastFM.Core.fix_for_url(album);
31
string artist_fixed = LastFM.Core.fix_for_url(artist);
33
var url = "http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=" + api + "&artist=" + artist_fixed + "&album=" + album_fixed;
34
stdout.printf("Parsing album info.\n");
35
Xml.Doc* doc = Parser.parse_file (url);
36
AlbumInfo.with_doc(doc);
39
public AlbumInfo.with_doc( Xml.Doc* doc) {
45
stderr.printf ("Could not get album info. \n");
49
// Get the root node. notice the dereferencing operator -> instead of .
50
Xml.Node* root = doc->get_root_element ();
52
// Free the document manually before returning
54
stderr.printf ("The xml file is empty. \n");
58
// Let's parse those nodes
59
parse_node (root, "");
65
/** recursively parses the nodes in a xml doc and also calls parse_properties
66
* @param node The node to parse
67
* @param parent the parent node
69
private void parse_node (Xml.Node* node, string parent) {
70
// Loop over the passed node's children
71
for (Xml.Node* iter = node->children; iter != null; iter = iter->next) {
72
// Spaces between tags are also nodes, discard them
73
if (iter->type != ElementType.ELEMENT_NODE) {
77
string node_name = iter->name;
78
string node_content = iter->get_content ();
80
if(parent == "album") {
81
if(node_name == "name")
83
else if(node_name == "artist")
84
_artist = node_content;
85
else if(node_name == "mbid")
87
else if(node_name == "url")
89
else if(node_name == "releasedate")
90
_releasedate = node_content;
91
else if(node_name == "playcount")
92
_playcount = node_content.to_int();
93
else if(node_name == "listeners")
94
_listeners = node_content.to_int();
95
else if(node_name == "image") {
96
if(iter->get_prop("size") == "large") {
97
url_image = new LastFM.Image.with_url(node_content, true);
98
url_image.set_size(200, 200);
102
else if(parent == "albumtoptagstag") {
103
if(node_name == "name") {
107
tagToAdd = new LastFM.Tag();
108
tagToAdd.tag = node_content;
110
else if(node_name == "url")
111
tagToAdd.url = node_content;
114
// Followed by its children nodes
115
parse_node (iter, parent + node_name);
120
get { return _name; }
121
set { _name = value; }
124
public string artist {
125
get { return _artist; }
126
set { _artist = value; }
130
get { return _mbid; }
131
set { _mbid = value; }
136
set { _url = value; }
139
public string releasedate {
140
get { return _releasedate; }
141
set { _releasedate = value; }
144
public int listeners {
145
get { return _listeners; }
146
set { _listeners = value; }
149
public int playcount {
150
get { return _playcount; }
151
set { _playcount = value; }
154
public LastFM.Image url_image {
155
get { return _url_image; }
156
set { _url_image = value; }
159
public void addTag(Tag t) {
163
public void addTagString(string t) {
164
_tags.add(new LastFM.Tag.with_string(t));
167
public Gee.ArrayList<LastFM.Tag> tags() {
171
public Gee.ArrayList<string> tagStrings() {
172
var tags = new Gee.ArrayList<string>();
174
foreach(LastFM.Tag t in _tags) {