Page 1 of 1

Script AllMusic

Posted: 2006-10-29 20:31:42
by frenchfrog_2
Hi,
I updated the script AllGame from scorpion7552 for the AllMusic.com site.
he only problem which can appear sometimes is for the country field as they never put the same format in it (it is the "born" or "formed" part of the web page).
It is my first attempt to write a script so don't scream too loud ;)
Please note that it works best with the Eyael mod of the language file to have a music catalog.
And I haven't try the batch mode yet, so I'm not sure if it is working.
So here it is :

Code: Select all

(***************************************************

Ant Movie Catalog importation script
www.antp.be/software/moviecatalog/

[Infos]
Authors=Frenchfrog2
Title=AllMusic
Description=import artist and/or album 
Site=www.allmusic.com
Language=EN
Version=1.0
Requires=3.5.0
Comments=this script is based on the script AllGame from Scorpion7552.
GetInfo=1

[Options]
albumopt=1|2|1=search for artist|2=search for album|3=search for song
Mode=0|0|0=normal mode|1=batch mode (url)

***************************************************)

// needs the following units
// StringUtils1.pas, StringUtils7552.pas
//

program AllMusic;
uses
	StringUtils1, StringUtils7552;
	
const
	AllMusicUrl = 'http://www.allmusic.com';       			// base url
	batchlogfic = 'c:\amc_AllGame_batchlog.txt'; 			// log for batch mode
	debug = true;                               			// debug mode on/off
	debugrep = 'd:\perso\';                       			// directory where to save files

var
	AlbumName, dfltAlbumc, firstcall, abort, msgano, name: String;
	BatchMode, dfltAlbum: Integer;
	batchlog, confbatch, memo: TstringList;
	albumok: boolean;
	

//------------------------------------------------------------------------------
// list of albums or artist 
//------------------------------------------------------------------------------
procedure GetList;   
var
	Address, Table, Line, Page, urlmusic, year, album, albumtest, label, genre, Linet: String;
	found: boolean;
	i: integer;

begin
	PickTreeClear;                                     					// clear list
	found := False;
	PickTreeAdd('List of albums or artists', '');
	Page := PostPage(AllMusicUrl+'/cg/agg.dll', 'sql='+UrlEncode(AlbumName)+'&OPT1='+dfltAlbumc+'&Submit=Go&P=amg');
	if debug then
		DumpPage(debugrep+'AllMusicList.txt', Page);    				// debug
	if Pos('Album Search Results for:', Page) = 0 then
	begin
		LogMessage('Error while reading selection page');
		exit;
	end;
// selection table
// note: the results are displayed on one page and are sorted by relevance
	Table := TextBetween(Page, '<!--Begin Search Results-->', 'End of List');
	if debug then
			DumpPage(debugrep+'AllMusicListShort.txt', Table);    		// debug
	Table := TextAfter(Table, 'Year');
	memo := TStringList.Create;
	memo.Text := StringReplace(Table, '</tr>', crlf);            		// separate lines
	if debug then
	begin
		Linet := StringReplace(Table, '</tr>', crlf);
		DumpPage(debugrep+'AllMusicStringList.txt', Linet);    			// debug
	end;
	urlmusic := 'href="/cg/amg.dll';             						// url to search (first occurence)
	for i := 0 to memo.Count-1 do                  						// list of albums
	begin 
		Line := memo.GetString(i);                     					// extract current line
		Address := GetUrl(Line, urlmusic, AllMusicUrl);  				// get url of album page
		if Address = '' then continue;                 					// no url: header or empty line
		Line := TextAfter(Line, '<td class="cell">');                  
		Line := StringReplace(Line, '</TD>', sepchar1);    				// separate fields with </TD>
		year := Trim(TextBefore(Line, sepchar1, ''));      				// year of the album
		Line := RemainingText;   
		Line := TextAfter(Line, '>');                      
		name := TranslateSpecial(Trim(TextBefore(Line, sepchar1, ''))); // name of the artist
		Line := RemainingText;         
		Line := TextAfter(Line, urlmusic);                  
		Line := TextAfter(Line, '>');                      
		albumtest := Trim(TextBefore(Line, 'src', ''));
		if albumtest = '<img' then
		begin
			Address := GetUrl(Line, urlmusic, AllMusicUrl);  			// get url of album page
			if Address = '' then continue;                 				// no url: header or empty line
			Line := TextAfter(Line, urlmusic);                  
			Line := TextAfter(Line, '>');                      
		end;
		album := TranslateSpecial(Trim(TextBefore(Line, '</a>', '')));  // name of the album
		Line := RemainingText;         
		Line := TextAfter(Line, sepchar1);                  
		Line := TextAfter(Line, sepchar1);                  
		Line := TextAfter(Line, '>');                      
		label := Trim(TextBefore(Line, sepchar1, ''));      			// label of the album
		Line := RemainingText;         
		Line := TextAfter(Line, urlmusic);            
		Line := TextAfter(Line, '>');                      
		genre := Trim(TextBefore(Line, '</a>', ''));      				// genre of the album
		if year <> '' then
		begin
			PickTreeAdd(name+' - '+album+' ('+year+')', Address);
			found := True;
		end;
	end;         {for i}
	memo.Free;
	if not found then
	begin
		LogMessage('No album found for '+AlbumName);
		exit;
	end;
	PickTreeSort;                                  						// sort the list 
	if PickTreeExec(Address) then
		AnalyzeMusicPage(Address)                     					// music page
	else
		LogMessage('No album selected');
end;

//------------------------------------------------------------------------------
// ANALYZE MUSIC PAGE
//------------------------------------------------------------------------------
procedure AnalyzeMusicPage(Address: string);

var
	Page, Line, Linet, urlmusic, artisturl, Value, cover, Track, Lenght, Minu, Seco, Pays, usastates: String;
	i, TMinu, TSeco: Integer;
	j: Real;

begin
	Page := GetPage(Address);         
	if debug then
		DumpPage(debugrep+'AllMusicPageDetail.txt', Page);    			// debug
	if Pos('Artist', Page) = 0 then   									// may be this test is OK?
	Begin 
		LogMessage('Error while reading album page');
		exit;
	end;

	SetField(fieldURL, Address);
	albumok := True;

//*** artist url
	urlmusic := 'href="/cg/amg.dll';             						// url to search (first occurence)
	Value := TextBetween(Page, '<span class="title">', 'class="subtitle');	
	artisturl := AllMusicUrl+TranslateSpecial(TextBetween(Value, '<a href="', ' "'));	
	
//*** artist name (=TranslatedTitle)
	Value := TextBetween(Page, 'class="subtitle"">', '</a>');
	SetField(fieldTranslatedTitle, FormatText(Value));

//*** album title (=originalTitle)
	Value := TextBetween(Page, '<span class="title">', '</span>');
	SetField(fieldOriginalTitle, FormatText(Value));

//*** rating  (<img src="/img/stars/st_rX.gif"  with X = rating: 1=1 star to 9=5 stars)
// X=1 (1) x=2 (1.5) ... x=9 (5) ==> rating = (x+1) for 0 to 10
	Value := TextBetween(Page, '<img src="/i/pages/site/stars/st_r', '.gif"'); 
  if Value <> '' then
  begin 
    i := StrToInt(Value, 0);
    j := (i + 1);                                         				// rating from 0 to 10
// uncomment next line if you want a rating from 0 to 5 (by half point)
//    j := (i + 1) /2;
  	SetField(fieldRating, FloatToStr(j));
	end;

//*** release date (year only)  ('month year' 'month day, year' or 'year')
	Value := TextBetween(Page, '<span>Release Date</span>', '</table>'); 
	Value := FormatText(Value);
	i := LastPos(' ', Value);                                 			// extract year only
	if i > 0 then Value := Copy(Value, i+1, length(Value)); 
	SetField(fieldYear, Value);

//*** label (= director)
	Value := TextBetween(Page,'<span>Label</span>', '</table>');
	SetField(fieldDirector, FormatText(Value));

//*** type (= country)
	Value := FormatText(TextBetween(Page, '<span>Type</span>', '</table>'));
	if Value = '' then
		SetField(fieldCountry,'Standard');
	if Value <> '' then	
		SetField(fieldCountry, Value);

//*** genre (= category)
	Value := TextBetween(Page, '<span>Styles</span>', '</li>');
	SetField(fieldCategory, FormatText(Value)); 

//*** cover
	cover := TextBetween(Page, '<!--Begin Album Photo-->', '</table>');  
	cover := TextBetween(cover, 'img src="', '"'); 
	if CanSetPicture then
		GetPicture(cover);

//*** album review (= comments)
	Value := FormatText(TextBetween(Page, '<!--Begin Center Content-->', '</table>'));
	Value := TextAfter(Value, 'Reviewby');
	if Value = '' then
		SetField(fieldComments, 'No Review Available'); 
	if Value <> '' then	
		SetField(fieldComments, 'Review by'+Value); 

//*** tracks list (= description)
	Value := TextBetween(Page, '<!--Begin Center Content-->', '<!--End Center Content-->');
	if debug then
		DumpPage(debugrep+'AllMusicPagetracks.txt', Value);    			// debug
	Value := TextAfter(Value, '>Time');
	Value := TextAfter(Value, '</table>');
	Value := TextAfter(Value, '</TD>');
	Value := TextAfter(Value, '</TD>');
	Value := StringReplace(Value, crlf, '');            			// separate lines
	memo := TStringList.Create;
	memo.Text := StringReplace(Value, '</tr>', crlf);            			// separate lines
	if debug then
	begin
		Linet := StringReplace(Value, '</tr>', crlf);
		DumpPage(debugrep+'AllMusicPageDetailList.txt', Linet);    			// debug
	end;
	Value := '';
	Minu := '';
	Seco := '';
	TMinu := 0;
	TSeco := 0;
	for i := 0 to memo.Count-1 do                  							// list of tracks
	begin 
		Line := memo.GetString(i);                     						// extract current line
		Line := TextAfter(Line, '<TD class="cell">');                  
		Line := StringReplace(Line, '</TD>', sepchar1);    					// separate fields with </TD>
		Track := Trim(TextBefore(Line, sepchar1, ''));
		if Track <> '' then
		begin
			Value := Value+Track;							      				// number of track
			Line := RemainingText;   
			Value := Value+'. '+FormatText(Trim(TextBefore(Line, sepchar1, '')));	// title of track
			Line := RemainingText;   
			Line := TextAfter(Line, sepchar1);      							// not usefull info
			Lenght := FormatText(Trim(TextBefore(Line, sepchar1, '')));			// lenght of track
			Minu := Trim(TextBefore(Lenght, ':', ''));
			Seco := Trim(TextAfter(Lenght, ':'));
			TMinu := TMinu + StrToInt(Minu, 0);									// extract the minutes
			TSeco := TSeco + StrToInt(Seco, 0);									// extract the seconds
			if TSeco > 60 then
			begin
				TMinu := TMinu + 1;
				TSeco := TSeco - 60;
			end;
			Value := Value+' ('+Lenght+')';
			Value := Value+crlf;
		end;
	end;
	SetField(fieldDescription, Value);

//*** length (only show the minutes)
	if TSeco > 30 then
	TMinu := Tminu + 1;
	SetField(fieldLength, IntToStr(TMinu));

//*** media name (= artist - title) default setting
	SetField(fieldMedia, GetField(fieldTranslatedTitle)+' - '+GetField(fieldOriginalTitle));

//*** media type (= JewelCase) default setting
	SetField(fieldMediaType, 'JewelCase');

//*** audio format (= VideoFormat = CD) default setting
	SetField(fieldVideoFormat, 'CD');
	
//*** source (= AudioFormat = Achat) default setting
	SetField(fieldAudioFormat, 'Achat');

//*** number of disks (= VideoBitrate = 1) default setting
	SetField(fieldVideoBitrate, '1');

//*** country of the artist
	Page := GetPage(artisturl);         
	if debug then
		DumpPage(debugrep+'AllMusicArtistDetail.txt', Page);    			// debug
	if pos('<!--Begin Formed-->', Page) > 0 then	
		Value := TextBetween(Page, '<!--Begin Formed-->', '<!--End Formed-->');
	if pos('<!--Begin Born-->', Page) > 0 then
		Value := TextBetween(Page, '<!--Begin Born-->', '<!--End Born-->');
	if debug then
		DumpPage(debugrep+'AllMusicArtistDetailshort.txt', Value);    			// debug
	Value := TextAfter(Value, '<a href=');
	Value := TextAfter(Value, '<a href=');
	Value := TextBetween(Value, '>', '<');
	Pays := TextAfter(Value, ', ');
	SetField(fieldSource, Pays);
	SetField(fieldProducer, 'English');
	if Pays = 'England' then SetField(fieldProducer, 'English');
	if Pays = 'France' then SetField(fieldProducer, 'French');
	if Pays = 'Spain' then SetField(fieldProducer, 'Spanish');
	if Pays = 'Italia' then SetField(fieldProducer, 'Italian');
	if Pays = 'Switzerland' then SetField(fieldProducer, 'Swiss');
	usastates := 'WA,MT,ND,MN,WI,MI,YT,NH,ME,NY,RI,MA,CT,NJ,DE,MD,DC,PA,OH,IN,IL,IA,SD,NE,WY,ID,OR,CA,NV,UT,AZ,CO,NM,KS,OK,TX,MO,AR,LA,KY,TN,MS,AL,FL,GA,SC,NC,VA,WV,HI,AK';
	if pos(Pays,usastates) > 0 then
	begin
		SetField(fieldSource, 'United States Of America');
		SetField(fieldProducer, 'English');
	end;	
end;

//------------------------------------------------------------------------------
// extract 'various infos'
//------------------------------------------------------------------------------
function ExtrInfo(str1, str2: string) :string;

begin
	str1 := FormatText(TextBetween(str1, str2, '</table>'));
	if str1 <> '' then str1 := crlf+str2+': '+str1;
	result := str1;
end;

//------------------------------------------------------------------------------
// set msgano (normal mode) or add to log (batch mode) 
//------------------------------------------------------------------------------
procedure LogMessage(m: string);
begin
	if BatchMode > 0 then 
		AddToLog('item '+GetField(fieldNumber)+': '+m)
	else
		msgano := m;
end;

//------------------------------------------------------------------------------
// add a message in the batch log and save to disk
// (because I don't know when it's finished...)
//------------------------------------------------------------------------------
procedure AddToLog(m: string);
begin
	batchlog.Add(m);
	batchlog.SaveToFile(batchlogfic);
end;

//------------------------------------------------------------------------------
// process batch mode
//------------------------------------------------------------------------------
procedure AllMusicBatch;
begin
	AlbumName := GetField(fieldUrl);       					// if no url or another site then ignore
	if (AlbumName <> '') and (Pos(AllAlbumUrl, AlbumName) > 0) then
		AnalyzeMusicPage(AlbumName)
	else
		LogMessage('ignored url="'+AlbumName+'"');
end;	
	
//------------------------------------------------------------------------------
// process normal mode
//------------------------------------------------------------------------------
procedure AllMusicNorm;
begin
	albumok := False;
	if dfltAlbum = 1 then   
	begin
		LogMessage('Sorry this functionality is not operational for now');
//		dfltAlbumc := '1';
//		AlbumName := GetField(fieldTranslatedTitle);     	// get artist name
//		msgano := 'Enter the name of the artist :';
//		repeat
//		if not Input('AllMusic.com Import', msgano, AlbumName) or (AlbumName = '') then exit;
//		GetList;
//		until albumok;
	end;
	if dfltAlbum = 2 then
	begin
		dfltAlbumc := '2';
		AlbumName := GetField(fieldOriginalTitle);     		// get album name
		msgano := 'Enter the name of the album :';
		repeat
		if not Input('AllMusic.com Import', msgano, AlbumName) or (AlbumName = '') then exit;
		GetList;
		until albumok;
	end;
	if dfltAlbum = 3 then
	begin
		LogMessage('Sorry this functionality is not operational for now');
//		dfltAlbumc := '3';
//		AlbumName := GetField(fieldOriginalTitle);     		// get song name
//		msgano := 'Enter the name of the song :';
//		repeat
//		if not Input('AllMusic.com Import', msgano, AlbumName) or (AlbumName = '') then exit;
//		GetList;
//		until albumok;
	end;
end;
	
//------------------------------------------------------------------------------
//  start here
//------------------------------------------------------------------------------                                               
begin
	if abort = 'o' then exit;                                	// batch mode aborted
	if firstcall <> 'done' then
	begin                                             			// 1st call: init parameters
		firstcall := 'done';
		if not CheckVersion(3,5,0) then
		begin
			ShowMessage('This script requires a newer version of Ant Movie Catalog (at least the version 3.5.0)');		
			abort := 'o';
			exit;
		end;	
// get user's parms (used more than once)
		dfltAlbum := GetOption('albumopt');
		BatchMode := GetOption('Mode');
		if BatchMode > 0 then                      				// batch mode: confirm the choice
		begin
			batchlog := TStringList.Create;            			// init batch log
			batchlog.Add('starting batch mode');   
			batchlog.Add(StringOfChar('*',80));
			batchlog.SaveToFile(batchlogfic);
// confirmation message
			confbatch := TStringList.Create;
			confbatch.Add('You have selected the batch mode:');
			confbatch.Add('Have you saved your database?');
			confbatch.Add('');
			confbatch.Add('At the end of treatement:'); 
			confbatch.Add('- look at the file '+batchlogfic+' for errors/infos');
			confbatch.Add('- the games found will be checked, the others not (for the selection)');
	 		confbatch.Add(' (see: Tools/Preferences/Movie list/checkboxes)');
			confbatch.Add('');
			confbatch.Add('confirm your choice');	
			if not ShowWarning(confbatch.Text) then
			begin
  			AddToLog('batch mode aborted');    					// batch mode not confirmed = abort
				abort := 'o';
				exit;
			end;
		end;
	end;
// let's go
	if BatchMode = 0 then
		AllMusicNorm
	else
		AllMusicBatch; 
end.

Posted: 2006-10-29 21:48:39
by scorpion7552
Hi
I've just tested your script. Works well... but some (tiny) remarks
1) when your release a script, put back 'debug := False;'
2) review the batch confirmation text: there's still 'game' inside
3) for non yet implemented option (albumopt=1 or 3) don't use 'LogMessage' but 'ShowWarning' or better (because that's the way I've modify the LogMessage routine for a near future batchcommon unit...) in LogMessage use ShowWarning instead of valorizing msgano (normal mode) and in input's, put the string, that is don't use msgano anymore.
4) I think that it's because you use the Eyael mod for language that you valorize for example 'label support', 'origin', ... I don't know if it's a good idea, because they are typical user's fields.
5) you don't need to precise uses 'StringUtils1' because it's included in 'StringUtils7552'
6) nothing to do with that, but where do you come from with such a nickname ? Are you French like me ?

So keep on, your first attempt is not that bad :hihi:

Posted: 2006-10-30 20:34:54
by frenchfrog_2
Hi scorpion 7552,
Thanks a lot for your input, I will modify the script and publish it again.
For the remark n°4 I think it is better to use the Eyael mod otherwise the field does not mean much. Note as well that I put comments in the script, like this it is easy to just change the name of the field to the one the user might want, but it is true that most users do not want to put their hands in coding, they just want a script that work out of the box ;)
For your last point : oui je suis français, il s'agit du surnom que m'a donné ma belle-soeur (anglaise) :lol: :lol:

Posted: 2006-10-30 20:57:08
by scorpion7552
frenchfrog_2 wrote:For your last point : oui je suis français, il s'agit du surnom que m'a donné ma belle-soeur (anglaise) :lol: :lol:
Ah, ah that's what I guessed. French Powa is here :clapping:

For point 4, it's a choice. If people know that, it's OK.
[FR mode on]n'hésite pas à m'envoyer un message en PM si besoin. Je suis en vacances jusqu'à lundi prochain, mais ça n'empêche pas...[/FR mode off]

Keep on rockin' ;)

Posted: 2006-10-31 20:16:01
by frenchfrog_2
OK here is the updated script :

Code: Select all

(***************************************************

Ant Movie Catalog importation script
www.antp.be/software/moviecatalog/

[Infos]
Authors=Frenchfrog2
Title=AllMusic
Description=import artist and/or album 
Site=www.allmusic.com
Language=EN
Version=1.0
Requires=3.5.0
Comments=this script is based on the script AllGame from Scorpion7552.
GetInfo=1

[Options]
albumopt=1|2|1=search for artist|2=search for album|3=search for song
Mode=0|0|0=normal mode|1=batch mode (url)

***************************************************)

// needs the following units
// StringUtils1.pas, StringUtils7552.pas
//

program AllMusic;
uses
	StringUtils7552;
	
const
	AllMusicUrl = 'http://www.allmusic.com';       			// base url
	batchlogfic = 'c:\amc_AllGame_batchlog.txt'; 			// log for batch mode
	debug = false;                               			// debug mode on/off
	debugrep = 'd:\perso\';                       			// directory where to save files

var
	AlbumName, dfltAlbumc, firstcall, abort, name: String;
	BatchMode, dfltAlbum: Integer;
	batchlog, confbatch, memo: TstringList;
	albumok: boolean;
	

//------------------------------------------------------------------------------
// list of albums or artist 
//------------------------------------------------------------------------------
procedure GetList;   
var
	Address, Table, Line, Page, urlmusic, year, album, albumtest, label, genre, Linet: String;
	found: boolean;
	i: integer;

begin
	PickTreeClear;                                     					// clear list
	found := False;
	PickTreeAdd('List of albums or artists', '');
	Page := PostPage(AllMusicUrl+'/cg/agg.dll', 'sql='+UrlEncode(AlbumName)+'&OPT1='+dfltAlbumc+'&Submit=Go&P=amg');
	if debug then
		DumpPage(debugrep+'AllMusicList.txt', Page);    				// debug
	if Pos('Album Search Results for:', Page) = 0 then
	begin
		LogMessage('Error while reading selection page');
		exit;
	end;
// selection table
// note: the results are displayed on one page and are sorted by relevance
	Table := TextBetween(Page, '<!--Begin Search Results-->', 'End of List');
	if debug then
			DumpPage(debugrep+'AllMusicListShort.txt', Table);    		// debug
	Table := TextAfter(Table, 'Year');
	memo := TStringList.Create;
	memo.Text := StringReplace(Table, '</tr>', crlf);            		// separate lines
	if debug then
	begin
		Linet := StringReplace(Table, '</tr>', crlf);
		DumpPage(debugrep+'AllMusicStringList.txt', Linet);    			// debug
	end;
	urlmusic := 'href="/cg/amg.dll';             						// url to search (first occurence)
	for i := 0 to memo.Count-1 do                  						// list of albums
	begin 
		Line := memo.GetString(i);                     					// extract current line
		Address := GetUrl(Line, urlmusic, AllMusicUrl);  				// get url of album page
		if Address = '' then continue;                 					// no url: header or empty line
		Line := TextAfter(Line, '<td class="cell">');                  
		Line := StringReplace(Line, '</TD>', sepchar1);    				// separate fields with </TD>
		year := Trim(TextBefore(Line, sepchar1, ''));      				// year of the album
		Line := RemainingText;   
		Line := TextAfter(Line, '>');                      
		name := TranslateSpecial(Trim(TextBefore(Line, sepchar1, ''))); // name of the artist
		Line := RemainingText;         
		Line := TextAfter(Line, urlmusic);                  
		Line := TextAfter(Line, '>');                      
		albumtest := Trim(TextBefore(Line, 'src', ''));
		if albumtest = '<img' then
		begin
			Address := GetUrl(Line, urlmusic, AllMusicUrl);  			// get url of album page
			if Address = '' then continue;                 				// no url: header or empty line
			Line := TextAfter(Line, urlmusic);                  
			Line := TextAfter(Line, '>');                      
		end;
		album := TranslateSpecial(Trim(TextBefore(Line, '</a>', '')));  // name of the album
		Line := RemainingText;         
		Line := TextAfter(Line, sepchar1);                  
		Line := TextAfter(Line, sepchar1);                  
		Line := TextAfter(Line, '>');                      
		label := Trim(TextBefore(Line, sepchar1, ''));      			// label of the album
		Line := RemainingText;         
		Line := TextAfter(Line, urlmusic);            
		Line := TextAfter(Line, '>');                      
		genre := Trim(TextBefore(Line, '</a>', ''));      				// genre of the album
		if year <> '' then
		begin
			PickTreeAdd(name+' - '+album+' ('+year+')', Address);
			found := True;
		end;
	end;         {for i}
	memo.Free;
	if not found then
	begin
		LogMessage('No album found for '+AlbumName);
		exit;
	end;
	PickTreeSort;                                  						// sort the list 
	if PickTreeExec(Address) then
		AnalyzeMusicPage(Address)                     					// music page
	else
		LogMessage('No album selected');
end;

//------------------------------------------------------------------------------
// ANALYZE MUSIC PAGE
//------------------------------------------------------------------------------
procedure AnalyzeMusicPage(Address: string);

var
	Page, Line, Linet, urlmusic, artisturl, Value, cover, Track, Lenght, Minu, Seco, Pays, usastates: String;
	i, TMinu, TSeco: Integer;
	j: Real;

begin
	Page := GetPage(Address);         
	if debug then
		DumpPage(debugrep+'AllMusicPageDetail.txt', Page);    			// debug
	if Pos('Artist', Page) = 0 then   									// may be this test is OK?
	Begin 
		LogMessage('Error while reading album page');
		exit;
	end;

	SetField(fieldURL, Address);
	albumok := True;

//*** artist url
	urlmusic := 'href="/cg/amg.dll';             						// url to search (first occurence)
	Value := TextBetween(Page, '<span class="title">', 'class="subtitle');	
	artisturl := AllMusicUrl+TranslateSpecial(TextBetween(Value, '<a href="', ' "'));	
	
//*** artist name (=TranslatedTitle)
	Value := TextBetween(Page, 'class="subtitle"">', '</a>');
	SetField(fieldTranslatedTitle, FormatText(Value));

//*** album title (=originalTitle)
	Value := TextBetween(Page, '<span class="title">', '</span>');
	SetField(fieldOriginalTitle, FormatText(Value));

//*** rating  (<img src="/img/stars/st_rX.gif"  with X = rating: 1=1 star to 9=5 stars)
// X=1 (1) x=2 (1.5) ... x=9 (5) ==> rating = (x+1) for 0 to 10
	Value := TextBetween(Page, '<img src="/i/pages/site/stars/st_r', '.gif"'); 
  if Value <> '' then
  begin 
    i := StrToInt(Value, 0);
    j := (i + 1);                                         				// rating from 0 to 10
// uncomment next line if you want a rating from 0 to 5 (by half point)
//    j := (i + 1) /2;
  	SetField(fieldRating, FloatToStr(j));
	end;

//*** release date (year only)  ('month year' 'month day, year' or 'year')
	Value := TextBetween(Page, '<span>Release Date</span>', '</table>'); 
	Value := FormatText(Value);
	i := LastPos(' ', Value);                                 			// extract year only
	if i > 0 then Value := Copy(Value, i+1, length(Value)); 
	SetField(fieldYear, Value);

//*** label (= director)
	Value := TextBetween(Page,'<span>Label</span>', '</table>');
	SetField(fieldDirector, FormatText(Value));

//*** type (= country)
	Value := FormatText(TextBetween(Page, '<span>Type</span>', '</table>'));
	if Value = '' then
		SetField(fieldCountry,'Standard');
	if Value <> '' then	
		SetField(fieldCountry, Value);
	if Value = 'Explicit Lyrics' then	
		SetField(fieldCountry,'Standard');

//*** genre (= category)
	Value := TextBetween(Page, '<span>Styles</span>', '</li>');
	SetField(fieldCategory, FormatText(Value)); 

//*** cover
	cover := TextBetween(Page, '<!--Begin Album Photo-->', '</table>');  
	cover := TextBetween(cover, 'img src="', '"'); 
	if CanSetPicture then
		GetPicture(cover);

//*** album review (= comments)
	Value := FormatText(TextBetween(Page, '<!--Begin Center Content-->', '</table>'));
	Value := TextAfter(Value, 'Reviewby');
	if Value = '' then
		SetField(fieldComments, 'No Review Available'); 
	if Value <> '' then	
		SetField(fieldComments, 'Review by'+Value); 

//*** tracks list (= description)
	Value := TextBetween(Page, '<!--Begin Center Content-->', '<!--End Center Content-->');
	if debug then
		DumpPage(debugrep+'AllMusicPagetracks.txt', Value);    			// debug
	Value := TextAfter(Value, '>Time');
	Value := TextAfter(Value, '</table>');
	Value := TextAfter(Value, '</TD>');
	Value := TextAfter(Value, '</TD>');
	Value := StringReplace(Value, crlf, '');            			// separate lines
	memo := TStringList.Create;
	memo.Text := StringReplace(Value, '</tr>', crlf);            			// separate lines
	if debug then
	begin
		Linet := StringReplace(Value, '</tr>', crlf);
		DumpPage(debugrep+'AllMusicPageDetailList.txt', Linet);    			// debug
	end;
	Value := '';
	Minu := '';
	Seco := '';
	TMinu := 0;
	TSeco := 0;
	for i := 0 to memo.Count-1 do                  							// list of tracks
	begin 
		Line := memo.GetString(i);                     						// extract current line
		Line := TextAfter(Line, '<TD class="cell">');                  
		Line := StringReplace(Line, '</TD>', sepchar1);    					// separate fields with </TD>
		Track := Trim(TextBefore(Line, sepchar1, ''));
		if Track <> '' then
		begin
			Value := Value+Track;							      				// number of track
			Line := RemainingText;   
			Value := Value+'. '+FormatText(Trim(TextBefore(Line, sepchar1, '')));	// title of track
			Line := RemainingText;   
			Line := TextAfter(Line, sepchar1);      							// not usefull info
			Lenght := FormatText(Trim(TextBefore(Line, sepchar1, '')));			// lenght of track
			Minu := Trim(TextBefore(Lenght, ':', ''));
			Seco := Trim(TextAfter(Lenght, ':'));
			TMinu := TMinu + StrToInt(Minu, 0);									// extract the minutes
			TSeco := TSeco + StrToInt(Seco, 0);									// extract the seconds
			if TSeco > 60 then
			begin
				TMinu := TMinu + 1;
				TSeco := TSeco - 60;
			end;
			Value := Value+' ('+Lenght+')';
			Value := Value+crlf;
		end;
	end;
	SetField(fieldDescription, Value);

//*** length (only show the minutes)
	if TSeco > 30 then
	TMinu := Tminu + 1;
	SetField(fieldLength, IntToStr(TMinu));

//*** media name (= artist - title) default setting
	SetField(fieldMedia, GetField(fieldTranslatedTitle)+' - '+GetField(fieldOriginalTitle));

//*** media type (= JewelCase) default setting
	SetField(fieldMediaType, 'JewelCase');

//*** audio format (= VideoFormat = CD) default setting
	SetField(fieldVideoFormat, 'CD');
	
//*** source (= AudioFormat = Achat) default setting
	SetField(fieldAudioFormat, 'Achat');

//*** number of disks (= VideoBitrate = 1) default setting
	SetField(fieldVideoBitrate, '1');

//*** country of the artist
	Page := GetPage(artisturl);         
	if debug then
		DumpPage(debugrep+'AllMusicArtistDetail.txt', Page);    			// debug
	if pos('<!--Begin Formed-->', Page) > 0 then	
		Value := TextBetween(Page, '<!--Begin Formed-->', '<!--End Formed-->');
	if pos('<!--Begin Born-->', Page) > 0 then
		Value := TextBetween(Page, '<!--Begin Born-->', '<!--End Born-->');
	if debug then
		DumpPage(debugrep+'AllMusicArtistDetailshort.txt', Value);    			// debug
	Value := TextAfter(Value, '<a href=');
	Value := TextAfter(Value, '<a href=');
	Value := TextBetween(Value, '>', '<');
	Pays := TextAfter(Value, ', ');
	SetField(fieldSource, Pays);
	SetField(fieldProducer, 'English');
	if Pays = 'France' then SetField(fieldProducer, 'French');
	if Pays = 'Spain' then SetField(fieldProducer, 'Spanish');
	if Pays = 'Italia' then SetField(fieldProducer, 'Italian');
	usastates := 'WA,MT,ND,MN,WI,MI,YT,NH,ME,NY,RI,MA,CT,NJ,DE,MD,DC,PA,OH,IN,IL,IA,SD,NE,WY,ID,OR,CA,NV,UT,AZ,CO,NM,KS,OK,TX,MO,AR,LA,KY,TN,MS,AL,FL,GA,SC,NC,VA,WV,HI,AK';
	if pos(Pays,usastates) > 0 then
	begin
		SetField(fieldSource, 'United States Of America');
		SetField(fieldProducer, 'English');
	end;	
end;

//------------------------------------------------------------------------------
// extract 'various infos'
//------------------------------------------------------------------------------
function ExtrInfo(str1, str2: string) :string;

begin
	str1 := FormatText(TextBetween(str1, str2, '</table>'));
	if str1 <> '' then str1 := crlf+str2+': '+str1;
	result := str1;
end;

//------------------------------------------------------------------------------
// set show warning (normal mode) or add to log (batch mode) 
//------------------------------------------------------------------------------
procedure LogMessage(m: string);
begin
	if BatchMode > 0 then 
		AddToLog('item '+GetField(fieldNumber)+': '+m)
	else
		ShowWarning(m);
end;

//------------------------------------------------------------------------------
// add a message in the batch log and save to disk
// (because I don't know when it's finished...)
//------------------------------------------------------------------------------
procedure AddToLog(m: string);
begin
	batchlog.Add(m);
	batchlog.SaveToFile(batchlogfic);
end;

//------------------------------------------------------------------------------
// process batch mode
//------------------------------------------------------------------------------
procedure AllMusicBatch;
begin
	AlbumName := GetField(fieldUrl);       					// if no url or another site then ignore
	if (AlbumName <> '') and (Pos(AllAlbumUrl, AlbumName) > 0) then
		AnalyzeMusicPage(AlbumName)
	else
		LogMessage('ignored url="'+AlbumName+'"');
end;	
	
//------------------------------------------------------------------------------
// process normal mode
//------------------------------------------------------------------------------
procedure AllMusicNorm;
begin
	albumok := False;
	if dfltAlbum = 1 then   
	begin
		LogMessage('Sorry this functionality is not operational for now');
//		dfltAlbumc := '1';
//		AlbumName := GetField(fieldTranslatedTitle);     	// get artist name
//		repeat
//		if not Input('AllMusic.com Import', 'Enter the name of the artist :', AlbumName) or (AlbumName = '') then exit;
//		GetList;
//		until albumok;
	end;
	if dfltAlbum = 2 then
	begin
		dfltAlbumc := '2';
		AlbumName := GetField(fieldOriginalTitle);     		// get album name
		repeat
		if not Input('AllMusic.com Import', 'Enter the name of the album :', AlbumName) or (AlbumName = '') then exit;
		GetList;
		until albumok;
	end;
	if dfltAlbum = 3 then
	begin
		LogMessage('Sorry this functionality is not operational for now');
//		dfltAlbumc := '3';
//		AlbumName := GetField(fieldOriginalTitle);     		// get song name
//		repeat
//		if not Input('AllMusic.com Import', 'Enter the name of the song :', AlbumName) or (AlbumName = '') then exit;
//		GetList;
//		until albumok;
	end;
end;
	
//------------------------------------------------------------------------------
//  start here
//------------------------------------------------------------------------------                                               
begin
	if abort = 'o' then exit;                                	// batch mode aborted
	if firstcall <> 'done' then
	begin                                             			// 1st call: init parameters
		firstcall := 'done';
		if not CheckVersion(3,5,0) then
		begin
			ShowMessage('This script requires a newer version of Ant Movie Catalog (at least the version 3.5.0)');		
			abort := 'o';
			exit;
		end;	
// get user's parms (used more than once)
		dfltAlbum := GetOption('albumopt');
		BatchMode := GetOption('Mode');
		if BatchMode > 0 then                      				// batch mode: confirm the choice
		begin
			batchlog := TStringList.Create;            			// init batch log
			batchlog.Add('starting batch mode');   
			batchlog.Add(StringOfChar('*',80));
			batchlog.SaveToFile(batchlogfic);
// confirmation message
			confbatch := TStringList.Create;
			confbatch.Add('You have selected the batch mode:');
			confbatch.Add('Have you saved your database?');
			confbatch.Add('');
			confbatch.Add('At the end of treatement:'); 
			confbatch.Add('- look at the file '+batchlogfic+' for errors/infos');
			confbatch.Add('- the albums found will be checked, the others not (for the selection)');
	 		confbatch.Add(' (see: Tools/Preferences/Movie list/checkboxes)');
			confbatch.Add('');
			confbatch.Add('confirm your choice');	
			if not ShowWarning(confbatch.Text) then
			begin
  			AddToLog('batch mode aborted');    					// batch mode not confirmed = abort
				abort := 'o';
				exit;
			end;
		end;
	end;
// let's go
	if BatchMode = 0 then
		AllMusicNorm
	else
		AllMusicBatch; 
end.

Italian Power and errors

Posted: 2007-08-25 06:41:28
by fulvio53s03
dear Friends, there' still an error an error in the script published, so the artist name is non correct and the "media Label" is wrong:
"subtitle" , not "subtitle"" in the extraction of the artist name.

by.
(Italian Power)
:hihi: :hihi: