File: MusicBrainzInfo.cc

package info (click to toggle)
flactag 2.0.4-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 2,096 kB
  • ctags: 477
  • sloc: sh: 10,777; cpp: 3,283; makefile: 62; sed: 7
file content (379 lines) | stat: -rwxr-xr-x 9,322 bytes parent folder | download | duplicates (3)
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/* --------------------------------------------------------------------------

   flactag -- A tagger for single album FLAC files with embedded CUE sheets
   						using data retrieved from the MusicBrainz service

   Copyright (C) 2006-2012 Andrew Hawkins
   Copyright (C) 2011-2012 Daniel Pocock

   This file is part of flactag.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
	 the Free Software Foundation, either version 3 of the License, or
	 (at your option) any later version.

   Flactag is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

     $Id$

----------------------------------------------------------------------------*/

#include "MusicBrainzInfo.h"

#include "config.h"

#include <sstream>

#include "base64.h"

#include "DiscIDWrapper.h"
#include "ErrorLog.h"

#include "musicbrainz5/HTTPFetch.h"
#include "musicbrainz5/Query.h"
#include "musicbrainz5/Track.h"
#include "musicbrainz5/Release.h"
#include "musicbrainz5/Medium.h"
#include "musicbrainz5/ReleaseGroup.h"
#include "musicbrainz5/ArtistCredit.h"
#include "musicbrainz5/Artist.h"
#include "musicbrainz5/Recording.h"
#include "musicbrainz5/NameCredit.h"

CMusicBrainzInfo::CMusicBrainzInfo(const std::string& Server, int Port, const CCuesheet& Cuesheet)
:	m_Server(Server),
	m_Port(Port),
	m_Cuesheet(Cuesheet),
	m_DiscIDWrapper(),
	m_DiskID(""),
	m_OverrideDiskID(false)
{
	if (m_Server.empty())
		m_Server="musicbrainz.org";

	if (0==m_Port)
		m_Port=80;

	m_DiscIDWrapper.FromCuesheet(m_Cuesheet);
	m_DiskID.assign(m_DiscIDWrapper.ID());
}

CMusicBrainzInfo::CMusicBrainzInfo(const std::string& Server, int Port, const CCuesheet& Cuesheet, std::string DiskID)
:       m_Server(Server),
        m_Port(Port),
        m_Cuesheet(Cuesheet),
        m_DiscIDWrapper(),
	m_DiskID(DiskID),
	m_OverrideDiskID(true)
{
        if (m_Server.empty())
                m_Server="musicbrainz.org";

        if (0==m_Port)
                m_Port=80;

        m_DiscIDWrapper.FromCuesheet(m_Cuesheet);
}

bool CMusicBrainzInfo::LoadInfo(const std::string& FlacFile)
{
	//http://www.musicbrainz.org/ws/2/discid/arIS30RPWowvwNEqsqdDnZzDGhk-?inc=recordings+artists+artist-credits+url-rels+aliases

	bool RetVal=false;

	//std::string DiskID=DiscIDWrapper.ID();

	//Test Disk ID for album title containing extended characters
	//DiskID="5EgKduVn7sQH9JGg8JQyrPOjSqc-";

	//CErrorLog::Log("DiskID: " + DiskID);
	//CErrorLog::Log("Submit: " + DiscIDWrapper.SubmitURL());

	MusicBrainz5::CQuery MusicBrainz("flactag-" VERSION,m_Server,m_Port);

	try
	{
		MusicBrainz5::CReleaseList ReleaseList=MusicBrainz.LookupDiscID(m_DiskID);

		if (ReleaseList.NumItems())
		{
			RetVal=true;

			for (int ReleaseNum=0;ReleaseNum<ReleaseList.NumItems();ReleaseNum++)
			{
				MusicBrainz5::CRelease *Release=ReleaseList.Item(ReleaseNum);

				MusicBrainz5::CRelease FullRelease=MusicBrainz.LookupRelease(Release->ID());

				MusicBrainz5::CMediumList MediaList=FullRelease.MediaMatchingDiscID(m_DiskID);
	 			for (int MediumNumber=0;MediumNumber<MediaList.NumItems();MediumNumber++)
				{
					MusicBrainz5::CMedium *Medium=MediaList.Item(MediumNumber);

					CAlbum Album=ParseAlbum(FullRelease,Medium);

					MusicBrainz5::CTrackList *TrackList=Medium->TrackList();
					if (TrackList)
					{
						for (int TrackNumber=0;TrackNumber<TrackList->NumItems();TrackNumber++)
						{
							MusicBrainz5::CTrack *MBTrack=TrackList->Item(TrackNumber);

							CTrack Track=ParseTrack(MBTrack);

							Album.AddTrack(Track);
						}
					}

					m_Albums.push_back(Album);
				}
			}
		}
		else
		{
			std::stringstream os;
			os << "No albums found for file '" << FlacFile << "'";
			CErrorLog::Log(os.str());

			if(!m_OverrideDiskID)
			{
				CErrorLog::Log("Please submit the DiskID using the following URL:");
				CErrorLog::Log(m_DiscIDWrapper.SubmitURL());
			}
		}
	}

	catch (MusicBrainz5::CResourceNotFoundError Exception)
	{
			std::stringstream os;
			os << "No albums found for file '" << FlacFile << "'";
			CErrorLog::Log(os.str());

			CErrorLog::Log("Please submit the DiskID using the following URL:");
			CErrorLog::Log(m_DiscIDWrapper.SubmitURL());
	}

	catch (MusicBrainz5::CExceptionBase Exception)
	{
		std::cerr << FlacFile << ": Exception: " << Exception.what() << std::endl;
	}

	return RetVal;
}

std::vector<unsigned char> CMusicBrainzInfo::GetCoverArt(const CUTF8Tag& ASIN)
{
	std::vector<unsigned char> Data;

	std::string URL="/images/P/" + ASIN.DisplayValue() + ".02.LZZZZZZZ.jpg";

	MusicBrainz5::CHTTPFetch Fetch("flactag/v" VERSION, "images.amazon.com");

	int Bytes=Fetch.Fetch(URL);
	if (Bytes<1000)
	{
		URL="/images/P/" + ASIN.DisplayValue() + ".02.MZZZZZZZ.jpg";
		Bytes=Fetch.Fetch(URL);
	}

	if (Bytes>0)
	{
		if (Bytes<1000)
			CErrorLog::Log("Album art downloaded was less than 1000 bytes, ignoring");
		else
		{
			Data=Fetch.Data();
		}
	}
	else
		CErrorLog::Log(std::string("Error downloading art: ") + Fetch.ErrorMessage());

	return Data;
}

CAlbum CMusicBrainzInfo::ParseAlbum(const MusicBrainz5::CRelease& Release, const MusicBrainz5::CMedium* Medium)
{
	CAlbum Album;

	if (!Release.Title().empty())
		Album.SetName(Release.Title());
	else if (Release.ReleaseGroup() && !Release.ReleaseGroup()->Title().empty())
		Album.SetName(Release.ReleaseGroup()->Title());

	if (Release.MediumList() && Release.MediumList()->NumItems()>1)
		Album.SetDiskNumber(Medium->Position());

	Album.SetAlbumID(Release.ID());

	MusicBrainz5::CArtistCredit *ArtistCredit=Release.ArtistCredit();
	if (ArtistCredit)
	{
		std::string ArtistID;
		std::string ArtistName;
		std::string ArtistSort;

		ParseArtist(ArtistCredit,ArtistID,ArtistName,ArtistSort);

		Album.SetArtistID(ArtistID);
		Album.SetArtist(ArtistName);
		Album.SetArtistSort(ArtistSort);
	}

	Album.SetASIN(Release.ASIN());

	if (!Album.ASIN().empty())
	{
		std::vector<unsigned char> Data=GetCoverArt(Album.ASIN());
		if (Data.size()>1000)
			Album.SetCoverArt(CCoverArt(&Data[0],Data.size()));
	}

	if (Release.ReleaseGroup())
			Album.SetType(AlbumType(Release.ReleaseGroup()->PrimaryType()));

	Album.SetStatus(AlbumStatus(Release.Status()));

	std::string AlbumDate=Release.Date();

	std::string::size_type MinusPos=AlbumDate.find("-");
	if (std::string::npos!=MinusPos)
		AlbumDate=AlbumDate.substr(0,MinusPos);

	Album.SetDate(AlbumDate);

	return Album;
}

void CMusicBrainzInfo::ParseArtist(const MusicBrainz5::CArtistCredit* ArtistCredit, std::string& ArtistID, std::string& ArtistName, std::string& ArtistSort)
{
	bool FirstArtist=true;

	MusicBrainz5::CNameCreditList *NameCreditsList=ArtistCredit->NameCreditList();
	if (NameCreditsList)
	{
		for (int NameCreditNumber=0;NameCreditNumber<NameCreditsList->NumItems();NameCreditNumber++)
		{
			MusicBrainz5::CNameCredit *NameCredit=NameCreditsList->Item(NameCreditNumber);

			MusicBrainz5::CArtist *Artist=NameCredit->Artist();

			if (!NameCredit->Name().empty())
				ArtistName+=NameCredit->Name();
			else if (Artist)
				ArtistName+=Artist->Name();

			ArtistName+=NameCredit->JoinPhrase();

			if (Artist)
			{
				ArtistSort+=Artist->SortName();
				ArtistSort+=NameCredit->JoinPhrase();
			}

			if (FirstArtist)
			{
				FirstArtist=false;
				if (Artist)
					ArtistID=Artist->ID();
			}
		}
	}
}

CTrack CMusicBrainzInfo::ParseTrack(const MusicBrainz5::CTrack* MBTrack)
{
	CTrack Track;

	Track.SetNumber(MBTrack->Position());

	if (!MBTrack->Title().empty())
		Track.SetName(MBTrack->Title());
	else if (MBTrack->Recording())
		Track.SetName(MBTrack->Recording()->Title());

	if (MBTrack->Recording())
	{
		MusicBrainz5::CArtistCredit *ArtistCredit=MBTrack->Recording()->ArtistCredit();
		if (ArtistCredit)
		{
			std::string ArtistID;
			std::string ArtistName;
			std::string ArtistSort;

			ParseArtist(ArtistCredit,ArtistID,ArtistName,ArtistSort);

			Track.SetArtistID(ArtistID);
			Track.SetArtist(ArtistName);
			Track.SetArtistSort(ArtistSort);
		}

		Track.SetTrackID(MBTrack->Recording()->ID());
	}

	return Track;
}

std::vector<CAlbum> CMusicBrainzInfo::Albums() const
{
	return m_Albums;
}

std::string CMusicBrainzInfo::AlbumType(const std::string Type) const
{
	const char *AlbumTypeStrings[] =
	{
		"album", "single", "EP", "compilation", "soundtrack",
		"spokenword", "interview", "audiobook", "live", "remix", "other", "\0"
	};

	std::string Ret;

	int i=0;

	while (AlbumTypeStrings[i][0]!='\0')
	{
		if (strcasecmp(Type.c_str(),AlbumTypeStrings[i])==0)
		{
			Ret=AlbumTypeStrings[i];
			break;
		}

		++i;
	}

	return Ret;
}

std::string CMusicBrainzInfo::AlbumStatus(const std::string Status) const
{
	const char *AlbumStatusStrings[] =
	{
		"official", "promotion", "bootleg", "\0"
	};

	std::string Ret;

	int i=0;

	while (AlbumStatusStrings[i][0]!='\0')
	{
		if (strcasecmp(Status.c_str(),AlbumStatusStrings[i])==0)
		{
			Ret=AlbumStatusStrings[i];
			break;
		}

		++i;
	}

	return Ret;
}