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.