Permalink
Browse files

Added scripts to get tags for songs from last.fm and then add those t…

…ags to the comments section of a song in iTunes
  • Loading branch information...
NeilCrosby committed Aug 15, 2010
1 parent 2860727 commit b94cf1f2c28bdc2b83129b458e0e6bc42573afdb
Showing with 141 additions and 0 deletions.
  1. +34 −0 playlist/lastfm/localtags.applescript
  2. +7 −0 playlist/lastfm/localtags.sh
  3. +100 −0 playlist/lastfm/updatelocaltags.php
@@ -0,0 +1,34 @@
on run argv
set tracks_list to {}
tell application "iTunes"
if (count of argv) > 1 then
set command_type to item 1 of argv
set argv to rest of argv
set track_id to item 1 of argv
set argv to rest of argv
set old_delim to AppleScript's text item delimiters
set AppleScript's text item delimiters to " "
set tag_info to argv as text
set AppleScript's text item delimiters to old_delim
else
return
end if
-- now that we've done the setup to get track_id and tag_info we need
-- to see if this track already has any tag info
set track_reference to first track of playlist 1 whose persistent ID is track_id
-- if we're doing a get command then return the track's comment
if (command_type is equal to "get") then
return comment of track_reference
end if
-- if we get here then we're doing a set - lets set the data
set comment of track_reference to tag_info
end tell
end run
@@ -0,0 +1,7 @@
#!/bin/sh
export DYLD_LIBRARY_PATH=""
BASEDIR=`dirname $0`
osascript -s s $BASEDIR/localtags.applescript $@
@@ -0,0 +1,100 @@
<?php
require_once('phplastfmapi-0.7.1/lastfmapi/lastfmapi.php');
$playlist = isset($_GET['playlist']) ? $_GET['playlist'] : '';
$maxTags = 5;
$playlist = '';
$info = pathinfo(__FILE__);
exec($info['dirname']."/../playlist.sh '$playlist'", $json);
$json = implode($json);
$json = str_replace('{', '[', $json);
$json = str_replace('}', ']', $json);
$data = json_decode($json);
$file = fopen('auth.txt', 'r');
$authVars = array(
'apiKey' => trim(fgets($file)),
'secret' => trim(fgets($file)),
'username' => trim(fgets($file)),
'sessionKey' => trim(fgets($file)),
'subscriber' => trim(fgets($file))
);
$config = array(
'enabled' => true,
'path' => 'phplastfmapi-0.7.1/lastfmapi/',
'cache_length' => 1800
);
// Pass the array to the auth class to eturn a valid auth
$auth = new lastfmApiAuth('setsession', $authVars);
$apiClass = new lastfmApi();
$trackClass = $apiClass->getPackage($auth, 'track', $config);
$count = sizeof($data[0]);
for ($i=0; $i < $count; $i++) {
$id = $data[0][$i];
$track = $data[1][$i];
$artist = $data[2][$i];
$album = $data[3][$i];
// Setup the variables
$methodVars = array(
'artist' => $artist,
'track' => $track
);
$tags = $trackClass->getTopTags($methodVars);
if ( $tags ) {
$ourTags = array();
foreach ($tags['tags'] as $tag) {
if ($tag['count'] > 0) {
$ourTags[] = $tag['name'];
if (sizeof($ourTags) >= $maxTags) {
break;
}
}
}
array_walk($ourTags, 'normaliseTag');
echo "<p><b>Done!</b> - $id - $track - $artist</p>";
$commentData = '';
exec($info['dirname']."/localtags.sh get $id", $commentData);
$previousInfo = '';
foreach ($commentData as $commentItem) {
$previousInfo .= trim($commentItem, '"')."\n";
}
$previousInfo = trim($previousInfo);
echo "<p>Previous: $previousInfo</p>";
if ( preg_match('/^(.*?)\[lfm:[^]]*](.*)$/', $previousInfo, $matches) ) {
$previousInfo = $matches[1].' '.$matches[2];
echo "<p>Filtered: $previousInfo</p>";
}
$newInfo = trim($previousInfo). ' [lfm: '.implode(' ', $ourTags).' ]';
echo "<p>New: $newInfo</p>";
exec($info['dirname']."/localtags.sh set $id $newInfo", $commentData);
} else {
echo ("<li><b>Error $track - $artist: ".$trackClass->error['code'].' - </b><i>'.$trackClass->error['desc'].'</i></li>');
}
}
function normaliseTag(&$tag, $key) {
$tag = strtolower($tag);
$tag = str_replace(' ', '-', $tag);
$tag = str_replace('[', '-', $tag);
$tag = str_replace(']', '-', $tag);
}
?>

0 comments on commit b94cf1f

Please sign in to comment.