~elementary-apps/noise/trunk

« back to all changes in this revision

Viewing changes to LastFMAlbumInfo.vala

  • Committer: Scott Ringwelski
  • Date: 2011-02-10 21:30:53 UTC
  • Revision ID: sgringwe@mtu.edu-20110210213053-d3c7mnexeref3cwj
sexy icons, sexy waf

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
using Xml;
2
 
 
3
 
public class LastFM.AlbumInfo : Object {
4
 
        static const string api = "a40ea1720028bd40c66b17d7146b3f3b";
5
 
        BeatBox.FileOperator fo;
6
 
        
7
 
        private string _name;
8
 
        private string _artist;
9
 
        private string _mbid;
10
 
        private string _url;
11
 
        private string _releasedate;
12
 
        
13
 
        private int _listeners;
14
 
        private int _playcount;
15
 
        
16
 
        private LastFM.Image _url_image;
17
 
        
18
 
        private Gee.ArrayList<LastFM.Tag> _tags;
19
 
        
20
 
        private LastFM.Tag tagToAdd;
21
 
        
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();
27
 
        }
28
 
        
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);
32
 
                
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);
37
 
        }
38
 
        
39
 
        public AlbumInfo.with_doc( Xml.Doc* doc) {
40
 
                AlbumInfo.basic();
41
 
                
42
 
                tagToAdd = null;
43
 
                
44
 
        if (doc == null) {
45
 
            stderr.printf ("Could not get album info. \n");
46
 
            return;
47
 
        }
48
 
 
49
 
        // Get the root node. notice the dereferencing operator -> instead of .
50
 
        Xml.Node* root = doc->get_root_element ();
51
 
        if (root == null) {
52
 
            // Free the document manually before returning
53
 
            delete doc;
54
 
            stderr.printf ("The xml file is empty. \n");
55
 
            return;
56
 
        }
57
 
        
58
 
        // Let's parse those nodes
59
 
        parse_node (root, "");
60
 
 
61
 
        // Free the document
62
 
        delete doc;
63
 
        }
64
 
        
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
68
 
         */
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) {
74
 
                continue;
75
 
            }
76
 
 
77
 
            string node_name = iter->name;
78
 
            string node_content = iter->get_content ();
79
 
                       
80
 
            if(parent == "album") {
81
 
                                if(node_name == "name")
82
 
                                        _name = node_content;
83
 
                                else if(node_name == "artist")
84
 
                                        _artist = node_content;
85
 
                                else if(node_name == "mbid")
86
 
                                        _mbid = node_content;
87
 
                                else if(node_name == "url")
88
 
                                        _url = node_content;
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);
99
 
                                        }
100
 
                                }
101
 
                        }
102
 
                        else if(parent == "albumtoptagstag") {
103
 
                                if(node_name == "name") {
104
 
                                        if(tagToAdd != null)
105
 
                                                _tags.add(tagToAdd);
106
 
                                        
107
 
                                        tagToAdd = new LastFM.Tag();
108
 
                                        tagToAdd.tag = node_content;
109
 
                                }
110
 
                                else if(node_name == "url")
111
 
                                        tagToAdd.url = node_content;
112
 
                        }
113
 
 
114
 
            // Followed by its children nodes
115
 
            parse_node (iter, parent + node_name);
116
 
        }
117
 
    }
118
 
    
119
 
    public string name {
120
 
                get { return _name; }
121
 
                set { _name = value; }
122
 
        }
123
 
        
124
 
        public string artist {
125
 
                get { return _artist; }
126
 
                set { _artist = value; }
127
 
        }
128
 
        
129
 
        public string mbid {
130
 
                get { return _mbid; }
131
 
                set { _mbid = value; }
132
 
        }
133
 
        
134
 
        public string url {
135
 
                get { return _url; }
136
 
                set { _url = value; }
137
 
        }
138
 
        
139
 
        public string releasedate {
140
 
                get { return _releasedate; }
141
 
                set { _releasedate = value; }
142
 
        }
143
 
        
144
 
        public int listeners {
145
 
                get { return _listeners; }
146
 
                set { _listeners = value; }
147
 
        }
148
 
        
149
 
        public int playcount {
150
 
                get { return _playcount; }
151
 
                set { _playcount = value; }
152
 
        }
153
 
    
154
 
    public LastFM.Image url_image {
155
 
                get { return _url_image; }
156
 
                set { _url_image = value; }
157
 
        }
158
 
        
159
 
        public void addTag(Tag t) {
160
 
                _tags.add(t);
161
 
        }
162
 
        
163
 
        public void addTagString(string t) {
164
 
                _tags.add(new LastFM.Tag.with_string(t));
165
 
        }
166
 
        
167
 
        public Gee.ArrayList<LastFM.Tag> tags() {
168
 
                return _tags;
169
 
        }
170
 
        
171
 
        public Gee.ArrayList<string> tagStrings() {
172
 
                var tags = new Gee.ArrayList<string>();
173
 
                
174
 
                foreach(LastFM.Tag t in _tags) {
175
 
                        tags.add(t.tag);
176
 
                }
177
 
                
178
 
                return tags;
179
 
        }
180
 
}