んです。プログラマに転職して1週間、確信したことはそこんとこです。
小慣れてくればなんとかなるものなんでしょうか?才能的なもの?連想配列のソートとかすげーーーーーー苦手。焦ってるかって言われるとそこまででもないんだけど、このままだとまずいなとは切に思う。
ということで、今日はみっちり基礎から勉強してました。PHPの。最近英語の勉強のモチベーション上がりすぎてプログラミングを家でやってなかったのでものすごくお尻痛い。せっかくなので何かアウトプットをと思ったけどまったく思いつかなかったので、Last.fm のAPI 使って最近ホットなアーティストを取得するプログラムを書いてみた。
ブラウザからLast.fm のユーザIDを入力すると、最近3ヶ月で聞いたアーティストTOP50を表示。左肩に鳥のタトゥーが入っているという小沢仁志ですら笑ってしまうであろうってぐらいそれだけです。

ソースを公開
musicloud.php
<?php
class Musicloud{
const API_KEY = 'YOUR_API_KEY';
const URL = 'http://ws.audioscrobbler.com/2.0/?';
/**
* getTopArtists
*
* @param string $user_id
* @param string $period
* @access public
* @return array - top artist list
*/
function getTopArtists($user_id, $period = '3month')
{
$result = array();
if (is_null($user_id)) {
return false;
}
$request_url = self::URL.'method=user.gettopartists'.
'&user='.$user_id.
'&api_key='.self::API_KEY;
if (!is_null($period)) {
$request_url = $request_url.'&period='.$period;
}
if (@$xml = simplexml_load_file($request_url)) {
foreach ($xml->xpath('topartists/artist') as $artist) {
array_push($result, (array)$artist);
}
return $result;
} else {
return false;
}
}
/**
* getWeeklyChartList
*
* @param string $user_id
* @param string $from (format:'20100101')
* @param string $to (format; '20100101')
* @access public
* @return array - weekly chart list
*/
function getWeeklyChartList($user_id, $from = null, $to = null)
{
$result = array();
if (is_null($user_id)) {
return false;
}
$request_url = self::URL.'method=user.getweeklytrackchart'.
'&user='.$user_id.
'&api_key='.self::API_KEY;
if (!is_null($from) && !is_null($to)) {
$request_url = $request_url.'&from='.$from.'&to='.$to;
}
if ($xml = simplexml_load_file($request_url)) {
foreach ($xml->xpath('weeklytrackchart/track') as $track) {
array_push($result, (array)$track);
}
return $result;
} else {
return false;
}
}
}
?>
lastdotfm.php
<?php
require_once 'musicloud.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
<head>
<title>MusicloUd</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script src="jquery-1.4.2.min.js"></script>
<script src="jquery.dimensions.min.js"></script>
<script src="jquery.tooltip.js"></script>
<script>
$( function(){
$.tooltip.defaults = $.extend( $.tooltip.defaults, {
delay : 1,
showURL : false,
showBody : "-",
track : true
});
$('a').tooltip();
});
</script>
<style type="text/css">
body {
font-family:Georgia,Arial,serif;
font-size: 16px;
margin:0;
padding:0;
color:#888888;
}
#titleimage {
float: left;
}
#myform {
float: left;
margin:100px auto auto 100px;
}
#myform td {
line-height:0;
}
#myform #user_id {
width:150px;
height:18px;
font-size:16px;
}
#myform #submit_button{
font-size:16px;
}
#tooltip {
position: absolute;
z-index: 3000;
border: 1px solid #666;
background: #999;
color: #FFF;
padding: 10px;
opacity: 0.9;
}
#tooltip h3, #tooltip div {
margin: 0;
}
table a {border:none;}
table img {border:none;}
table#top_five {
margin:0 auto;
}
table#others {
margin:0 auto;
}
table#others tr td {
padding:0 5px;
}
.login_mes {
margin-left:250px;
}
.erro_mes {
margin-left:250px;
color:#FF1248;
}
</style>
</head>
<body>
<div id="titleimage">
<img src="bk.png" alt="title" />
</div>
<div id="myform">
<form action="lastdotfm.php" method="POST">
<table><tr>
<td><input id="user_id" type="text" name="user" size="15" value="<?php echo($_POST['user']); ?>" /></td>
<td><input id="submit_button" type="submit" value="Get" /></td>
</tr></table>
</form>
</div>
<div style="clear:both;"></div>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
echo('<table id="top_five">');
$result = Musicloud::getTopArtists($_POST['user']);
if ($result) {
echo '<tr>';
for ($count=0; $count<5; $count++) {
echo '<td><a href="'.htmlspecialchars($result[$count]['url']).'" title="'.htmlspecialchars($result[$count]['name']).'-> '.htmlspecialchars($result[$count]['playcount']).' plays!"><image src="'.htmlspecialchars($result[$count]['image'][2]).'" alt="'.htmlspecialchars($result[$count]['name']).'"></a></td>';
}
echo '</tr></table><table id="others">';
for ($count=5; $count<count($result); $count++) {
if ($count==5) {
echo '<tr>';
} elseif (($count-5)%9 == 0 and $count != count($result) -1) {
echo '</tr><tr>';
}
echo '<td><a href="'.htmlspecialchars($result[$count]['url']).'" title="'.htmlspecialchars($result[$count]['name']).'-> '.htmlspecialchars($result[$count]['playcount']).' plays!"><image src="'.htmlspecialchars($result[$count]['image'][1]).'" alt="'.htmlspecialchars($result[$count]['name']).'"></a></td>';
}
echo '</tr>';
} else {
echo '<span class="erro_mes">Could not get lists!!! Please confirm LoginID.</span>';
}
} else {
echo '<span class="login_mes">Last.fm のユーザIDを入力してください。</span>';
}
?>
</table>
</body>
</html>
musicloud.php の方は最初WeeklyChartList をリストを取得して表示ようと思ってたんですが、取得してみたら150件分のホットな曲がまったく知りたくなかったのでホットなアーティスト(TopArtists)を取得するようにしました。引数を変えれば取得できるデータも多少変わってくると思います。
んーーーー、ライブラリとか使ったりAPI使ったりとかはできるんだよなー。なんかこう、プログラムを書くときのバイブル的な本を買いたい。
| Author | Rasmus Lerdorf Kevin Tatroe Peter MacIntyre |
|---|---|
| Manufacturer | オライリー・ジャパン |
| Release Date | 2007年10月23日 |
| Price | 3,990yen |



