no message
[xbmc:xbmc-antiquated.git] / xbmc / utils / MusicAlbumInfo.cpp
1 #include ".\musicalbuminfo.h"
2 #include ".\htmltable.h"
3 #include ".\htmlutil.h"
4 #include ".\http.h"
5 #include "../util.h"
6
7
8
9 using namespace MUSIC_GRABBER;
10 using namespace HTML;
11 CMusicAlbumInfo::CMusicAlbumInfo(void)
12 {
13         m_strArtist="";
14         m_strTitle="";
15         m_strDateOfRelease="";
16         m_strGenre="";
17         m_strTones="";
18         m_strStyles="";
19         m_strReview="";
20         m_strImageURL="";
21         m_strTitle2="";
22         m_iRating=0;
23         m_bLoaded=false;
24 }
25
26 CMusicAlbumInfo::~CMusicAlbumInfo(void)
27 {
28 }
29
30
31 CMusicAlbumInfo::CMusicAlbumInfo(const CStdString& strAlbumInfo, const CStdString& strAlbumURL)
32 {
33         m_strTitle2             = strAlbumInfo;
34         m_strAlbumURL   = strAlbumURL;
35 }
36
37 const CStdString& CMusicAlbumInfo::GetAlbumURL() const
38 {
39         return m_strAlbumURL;
40 }
41 const CStdString& CMusicAlbumInfo::GetArtist() const
42 {
43         return m_strArtist;
44 }
45
46 const CStdString& CMusicAlbumInfo::GetTitle() const
47 {
48         return m_strTitle;
49 }
50 const CStdString& CMusicAlbumInfo::GetTitle2() const
51 {
52         return m_strTitle2;
53 }
54
55
56 const CStdString& CMusicAlbumInfo::GetDateOfRelease() const
57 {
58         return m_strDateOfRelease;
59 }
60
61
62 const CStdString& CMusicAlbumInfo::GetGenre() const
63 {
64         return m_strGenre;
65 }
66
67 const CStdString& CMusicAlbumInfo::GetTones() const
68 {
69         return m_strTones;
70 }
71
72 const CStdString& CMusicAlbumInfo::GetStyles() const
73 {
74         return m_strStyles;
75 }
76
77 const CStdString& CMusicAlbumInfo::GetReview() const
78 {
79         return m_strReview;
80 }
81
82 const CStdString& CMusicAlbumInfo::GetImageURL() const
83 {
84         return m_strImageURL;
85 }
86
87 int     CMusicAlbumInfo::GetRating() const
88 {
89         return m_iRating;
90 }
91
92 int     CMusicAlbumInfo::GetNumberOfSongs() const
93 {
94         return (int)m_vecSongs.size();
95 }
96
97 const CMusicSong& CMusicAlbumInfo::GetSong(int iSong)
98 {
99         return m_vecSongs[iSong];
100 }
101
102 bool    CMusicAlbumInfo::Parse(const CStdString& strHTML)
103 {
104         m_vecSongs.erase(m_vecSongs.begin(),m_vecSongs.end());
105         CStdString strHTMLLow=strHTML;
106         strHTMLLow.MakeLower();
107         int iStartOfTable=strHTMLLow.Find("artist");
108         if (iStartOfTable< 0) return false;
109         iStartOfTable=strHTMLLow.ReverseFind("<table",iStartOfTable);
110         if (iStartOfTable < 0) return false;
111
112         CHTMLUtil  util;
113         CHTMLTable table;
114         CStdString strTable=strHTML.Right((int)strHTML.size()-iStartOfTable);
115         table.Parse(strTable);
116         for (int iRow=0; iRow < table.GetRows(); iRow++)
117         {
118                 const CHTMLRow& row=table.GetRow(iRow);
119                 int iColums=row.GetColumns();
120                 if (iColums>1)
121                 {
122                         const CStdString strColum1=row.GetColumValue(0);
123                         const CStdString strValue =row.GetColumValue(1);
124                         if (strColum1.Find("Artist") >=0)
125                         {
126                                 util.getValueOfTag(     strValue,m_strArtist);                  
127                         }
128                         if (strColum1.Find("Date of Release") >=0)
129                         {
130                                 util.getValueOfTag(     strValue,m_strDateOfRelease);                   
131                         }
132                         if (strColum1.Find("Genre") >=0)
133                         {
134                                 util.getValueOfTag(     strValue,m_strGenre);                   
135                         }
136                         if (strColum1.Find("Tones") >=0)
137                         {
138                                 util.getValueOfTag(     strValue,m_strTones);                   
139                         }
140                         if (strColum1.Find("Styles") >=0)
141                         {
142                                 util.getValueOfTag(     strValue,m_strStyles);                  
143                         }
144                         if (strColum1.Find("AMG Rating") >=0)
145                         {
146                                 CStdString strRating;
147                                 util.getValueOfTag(     strValue,strRating);                    
148                         }
149                         if (strColum1.Find("Album Title") >=0)
150                         {
151                                 util.getValueOfTag(     strValue,m_strTitle);                   
152                         }
153                 }
154         }
155
156         // parse review/image
157         iStartOfTable=strHTMLLow.Find("review",0);
158         if (iStartOfTable < 0) return false;
159         strTable=strHTML.Right((int)strHTML.size()-iStartOfTable);
160         table.Parse(strTable);
161         int iRows=table.GetRows();
162         if (iRows > 0)
163         {
164                 const CHTMLRow& row=table.GetRow(0);
165                 int iColums=row.GetColumns();
166                 if (iColums>=1)
167                 {
168                         const CStdString strReviewAndImage=row.GetColumValue(0);
169                         
170                         util.getAttributeOfTag(strReviewAndImage,"src=",m_strImageURL);
171                         m_strReview=strReviewAndImage;
172                         util.RemoveTags(m_strReview);
173                         util.ConvertHTMLToAnsi(m_strReview, m_strReview);
174                 }
175         }
176
177         // parse songs...
178         iStartOfTable=strHTMLLow.Find("htrk1.gif",0);
179         if (iStartOfTable < 0) return false;
180         iStartOfTable=strHTMLLow.ReverseFind("<table",iStartOfTable);
181         if (iStartOfTable < 0) return false;
182         strTable=strHTML.Right((int)strHTML.size()-iStartOfTable);
183         table.Parse(strTable);
184         for (int iRow=0; iRow < table.GetRows(); iRow++)
185         {
186                 const CHTMLRow& row=table.GetRow(iRow);
187                 int iCols=row.GetColumns();
188                 if (iCols >=5)
189                 {
190                         CStdString strTrack;
191                         for (int i=0; i < iCols;i++)
192                         {
193                                 strTrack=row.GetColumValue(i);
194                                 int ipos=strTrack.Find(".");
195                                 if (ipos>=0)
196                                 {
197                                         bool bok=true;
198                                         for (int x=0; x <ipos; x++)
199                                         {
200                                                 if (!isdigit( strTrack[x] ) )
201                                                 {
202                                                         bok=false;
203                                                         break;
204                                                 }
205                                         }
206                                         if (bok) break;
207                                 }
208                         }
209                         CStdString strNameAndDuration=row.GetColumValue(4);
210
211                         
212                         CStdString strName,strDuration="0";
213                         int iDuration=0;
214                         util.getValueOfTag(strNameAndDuration,strName);
215                         int iPos=strNameAndDuration.ReverseFind("-");
216                         if (iPos > 0)
217                         {
218                                 iPos+=2;
219                                 strDuration=strNameAndDuration.Right( (int)strNameAndDuration.size()-iPos);
220                                 CStdString strMin="0", strSec="0";
221                                 iPos=strDuration.Find(":");
222                                 if (iPos>=0)
223                                 {
224                                         strMin=strDuration.Left(iPos);
225                                         iPos++;
226                                         strSec=strDuration.Right((int)strDuration.size()-iPos);
227                                         int iMin=atoi(strMin.c_str());
228                                         int iSec=atoi(strSec.c_str());
229                                         iDuration=iMin*60+iSec;
230                                 }
231                         }
232                         iPos=strTrack.Find(".");
233                         if (iPos > 0) strTrack = strTrack.Left(iPos);
234
235                         int iTrack=atoi(strTrack.c_str());
236                         CStdString strStripped;
237                         util.ConvertHTMLToAnsi(strName, strStripped);
238                         CMusicSong newSong(iTrack, strStripped, iDuration);
239                         m_vecSongs.push_back(newSong);
240                 }
241         }
242         if (m_strTitle2="") m_strTitle2=m_strTitle;
243         SetLoaded(true);
244         return true;
245 }
246
247
248 bool CMusicAlbumInfo::Load()
249 {
250         CStdString strHTML;
251         CHTTP http;
252         if ( !http.Get(m_strAlbumURL, strHTML)) return false;
253         return Parse(strHTML);
254 }
255
256
257
258 void CMusicAlbumInfo::Save(CStdString& strFileName)
259 {
260         
261         FILE* fd = fopen(strFileName.c_str(), "wb+");
262         if (fd)
263         {
264                 CUtil::SaveString(m_strArtist,fd);
265                 CUtil::SaveString(m_strTitle,fd);
266                 CUtil::SaveString(m_strDateOfRelease,fd);
267                 CUtil::SaveString(m_strGenre,fd);
268                 CUtil::SaveString(m_strTones,fd);
269                 CUtil::SaveString(m_strStyles,fd);
270                 CUtil::SaveString(m_strReview,fd);
271                 CUtil::SaveString(m_strImageURL,fd);
272                 CUtil::SaveString(m_strTitle2,fd);
273                 CUtil::SaveInt(m_iRating,fd);
274                 CUtil::SaveInt(m_vecSongs.size(),fd);
275                 for (int iSong=0; iSong < (int)m_vecSongs.size(); iSong++)
276                 {
277                         CMusicSong& song=m_vecSongs[iSong];
278                         song.Save(fd);
279                 }
280                 fclose(fd);
281         }
282 }
283
284 bool CMusicAlbumInfo::Load(CStdString& strFileName)
285 {
286         m_vecSongs.erase(m_vecSongs.begin(),m_vecSongs.end());
287         FILE* fd = fopen(strFileName.c_str(), "rb");
288         if (fd)
289         {
290                 CUtil::LoadString(m_strArtist,fd);
291                 CUtil::LoadString(m_strTitle,fd);
292                 CUtil::LoadString(m_strDateOfRelease,fd);
293                 CUtil::LoadString(m_strGenre,fd);
294                 CUtil::LoadString(m_strTones,fd);
295                 CUtil::LoadString(m_strStyles,fd);
296                 CUtil::LoadString(m_strReview,fd);
297                 CUtil::LoadString(m_strImageURL,fd);
298                 CUtil::LoadString(m_strTitle2,fd);
299                 m_iRating=CUtil::LoadInt(fd);
300                 int iSongs=CUtil::LoadInt(fd);
301                 for (int iSong=0; iSong < iSongs; iSong++)
302                 {
303                         CMusicSong song;
304                         song.Load(fd);
305                         m_vecSongs.push_back(song);
306                 }
307                 
308                 SetLoaded(true);
309                 fclose(fd);
310                 return true;
311         }
312         return false;
313 }
314
315 void CMusicAlbumInfo::SetLoaded(bool bOnOff)
316 {
317         m_bLoaded=bOnOff;
318 }
319 bool CMusicAlbumInfo::Loaded() const
320 {
321         return m_bLoaded;
322 }