last.fm apk_Last.fm javascript API实践

last.fm apk

Last.fm javascript API practice
Last.fm javascript API practice

Last.fm javascript API practice Today I would like to give you something new, we have already talked about different social networks like facebook, google or twitter, but we haven’t tried other services, as example – musical services. I’ve prepared for you an example of work with Last.fm. As we know, this is huge musical website with rich api. In our tutorial we are going to create a script with next functionality: In the beginning it will obtain the top weekly artists (chart), then, we will remember a first one artist (as a top artist), and in the next step we will obtain information about him. And finally, in the third step we will get the top tracks by him. I sure that such script will be very useful for any music website.

Last.fm javascript API实践今天,我想给您一些新的东西,我们已经讨论过不同的社交网络,例如facebook,google或twitter,但我们还没有尝试其他服务,例如音乐服务。 我为您准备了使用Last.fm的示例。 众所周知,这是一个拥有丰富API的巨大音乐网站。 在我们的教程中,我们将创建一个具有下一个功能的脚本:首先,它将获取每周的顶级艺术家(图表),然后,我们将记住第一个艺术家(作为顶级艺术家),然后在下一步中,将获得有关他的信息。 最后,在第三步中,我们将获得他的关注。 我相信这样的脚本对于任何音乐网站都将非常有用。

Before we begin, we must do the following:

在开始之前,我们必须执行以下操作:

1. Last.fm: 在此处申请API帐户 (1. Last.fm: Apply for an API Account here)

In order to use their API we should join them and obtain our own API keys (as usual: key and secret). During the registration you have to select your account type (free one or paid), after, you have to add your own application name, description and homepage, and after they will give you your api keys, something like that:

为了使用它们的API,我们应该加入它们并获取我们自己的API密钥(通常:密钥和机密)。 注册期间,您必须选择帐户类型(免费或付费),之后,您必须添加自己的应用程序名称,描述和首页,然后它们将为您提供api密钥,如下所示:

Last.fm - step 1

Last.fm-步骤1

2. 在此处下载必要的JS库 (2. Download necessary JS libraries here)

We are going to use this ‘lastfm.api.js’ library in our project. Now we are ready, lets start!

我们将在项目中使用此“ lastfm.api.js”库。 现在我们准备好了,让我们开始吧!

现场演示

[sociallocker]

[社交储物柜]

打包下载

[/sociallocker]

[/ sociallocker]

步骤1. HTML (Step 1. HTML)

This is our HTML markup:

这是我们HTML标记:

index.html (index.html)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Last.fm javascript API practice | Script Tutorials</title>
    <link href="css/main.css" rel="stylesheet" type="text/css" />
    <!-- load jquery and jsrender libraries -->
    <script type="text/javascript" src="js/jquery.js"></script>
    <script type="text/javascript" src="js/jsrender.js"></script>
    <!-- and new libraries to use lastfm -->
    <script type="text/javascript" src="js/lastfm.api.md5.js"></script>
    <script type="text/javascript" src="js/lastfm.api.js"></script>
    <script type="text/javascript" src="js/lastfm.api.cache.js"></script>
    <!-- define three templates for our units -->
    <script id="lastfmTemplateArtists" type="text/x-jsrender">
        <div id="{{:mbid}}" class="artist">
            <a href="{{:url}}" rel="nofollow" target="_blank"><img src="{{:image[2]["#text"]}}" alt="{{:name}}" /><b>{{:name}}:</b></a>
        </div>
    </script>
    <script id="lastfmTemplateArtistInfo" type="text/x-jsrender">
        <div class="artist_info">
            <a href="{{:url}}" rel="nofollow" target="_blank"><b>{{:name}}:</b><img src="{{:image[3]["#text"]}}" alt="{{:name}}" /></a>
            <div><noindex>
                {{:bio.content}}
            </noindex></div>
        </div>
    </script>
    <script id="lastfmTemplateTracks" type="text/x-jsrender">
        {{if image}}
        <div id="{{:mbid}}" class="track">
            <a href="{{:url}}" rel="nofollow" target="_blank"><img src="{{:image[0]["#text"]}}" /><b>{{:name}}:</b></a>
        </div>
        {{/if}}
    </script>
    <script type="text/javascript" src="js/main.js"></script>
</head>
<body>
    <div class="content-main">
        <div class="content-body">
            <h2>Top artists</h2>
            <div class="content-body-inner" id="top_artists"></div>
        </div>
        <div class="content-body">
            <h2>Top artist</h2>
            <div class="content-body-inner" id="top_artist"></div>
        </div>
        <div class="content-body">
            <h2>Top tracks</h2>
            <div class="content-body-inner" id="top_tracks"></div>
        </div>
    </div>
</body>
</html>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Last.fm javascript API practice | Script Tutorials</title>
    <link href="css/main.css" rel="stylesheet" type="text/css" />
    <!-- load jquery and jsrender libraries -->
    <script type="text/javascript" src="js/jquery.js"></script>
    <script type="text/javascript" src="js/jsrender.js"></script>
    <!-- and new libraries to use lastfm -->
    <script type="text/javascript" src="js/lastfm.api.md5.js"></script>
    <script type="text/javascript" src="js/lastfm.api.js"></script>
    <script type="text/javascript" src="js/lastfm.api.cache.js"></script>
    <!-- define three templates for our units -->
    <script id="lastfmTemplateArtists" type="text/x-jsrender">
        <div id="{{:mbid}}" class="artist">
            <a href="{{:url}}" rel="nofollow" target="_blank"><img src="{{:image[2]["#text"]}}" alt="{{:name}}" /><b>{{:name}}:</b></a>
        </div>
    </script>
    <script id="lastfmTemplateArtistInfo" type="text/x-jsrender">
        <div class="artist_info">
            <a href="{{:url}}" rel="nofollow" target="_blank"><b>{{:name}}:</b><img src="{{:image[3]["#text"]}}" alt="{{:name}}" /></a>
            <div><noindex>
                {{:bio.content}}
            </noindex></div>
        </div>
    </script>
    <script id="lastfmTemplateTracks" type="text/x-jsrender">
        {{if image}}
        <div id="{{:mbid}}" class="track">
            <a href="{{:url}}" rel="nofollow" target="_blank"><img src="{{:image[0]["#text"]}}" /><b>{{:name}}:</b></a>
        </div>
        {{/if}}
    </script>
    <script type="text/javascript" src="js/main.js"></script>
</head>
<body>
    <div class="content-main">
        <div class="content-body">
            <h2>Top artists</h2>
            <div class="content-body-inner" id="top_artists"></div>
        </div>
        <div class="content-body">
            <h2>Top artist</h2>
            <div class="content-body-inner" id="top_artist"></div>
        </div>
        <div class="content-body">
            <h2>Top tracks</h2>
            <div class="content-body-inner" id="top_tracks"></div>
        </div>
    </div>
</body>
</html>

There are three different JS-based templates (lastfmTemplateArtists, lastfmTemplateArtistInfo and lastfmTemplateTracks) which we are going to use to generate our objects (the list of artists, single artist block and the top tracks by that artist). Each group of objects will be placed into separated design box.

我们将使用三种不同的基于JS的模板(lastfmTemplateArtists,lastfmTemplateArtistInfo和lastfmTemplateTracks)来生成对象(艺术家列表,单个艺术家块以及该艺术家的热门曲目)。 每组对象将放置在单独的设计框中。

步骤2. CSS (Step 2. CSS)

Now, we have to prepare CSS styles for our project, in the beginning we have to define styles for our main layout and our design blocks:

现在,我们必须为项目准备CSS样式,在开始时,我们必须为主要布局和设计块定义样式:

css / main.css (css/main.css)

*{
    margin:0;
    padding:0;
}
body {
    background-color:#E3E3E3;
    font:14px/1.3 Arial,sans-serif;
}
.content-main {
    background-color: #FFFFFF;
    border: 1px solid #CCCCCC;
    margin: 20px auto;
    max-width: 1002px;
    min-height: 500px;
    min-width: 930px;
    overflow: visible;
    padding: 10px;
    position: relative;
}
.content-body {
    background: none repeat scroll 0 0 #EEEEEE;
    margin: 0 0 15px;
    padding: 10px 10px 5px;
    position: relative;
}
.content-body h2 {
    display: block;
    font-size: 18px;
    line-height: 20px;
    margin: 0;
    padding: 0 20px;
}
.content-body .content-body-inner {
    border-bottom: 1px solid #E8E8E8;
    min-height: 51px;
    overflow: hidden;
    padding: 9px 12px;
    position: relative;
}

*{
    margin:0;
    padding:0;
}
body {
    background-color:#E3E3E3;
    font:14px/1.3 Arial,sans-serif;
}
.content-main {
    background-color: #FFFFFF;
    border: 1px solid #CCCCCC;
    margin: 20px auto;
    max-width: 1002px;
    min-height: 500px;
    min-width: 930px;
    overflow: visible;
    padding: 10px;
    position: relative;
}
.content-body {
    background: none repeat scroll 0 0 #EEEEEE;
    margin: 0 0 15px;
    padding: 10px 10px 5px;
    position: relative;
}
.content-body h2 {
    display: block;
    font-size: 18px;
    line-height: 20px;
    margin: 0;
    padding: 0 20px;
}
.content-body .content-body-inner {
    border-bottom: 1px solid #E8E8E8;
    min-height: 51px;
    overflow: hidden;
    padding: 9px 12px;
    position: relative;
}

This are usual boxes, nothing is especial. Next – styles for the list of artists:

这是通常的盒子,没什么特别的。 下一步–艺术家名单的样式:


/* list of artists */
.artist {
    display: block;
    float: left;
    margin: 10px;
    width: 128px;
}
.artist a {
    text-decoration: none;
}
.artist b {
    color: #1B1B1B;
    display: block;
    font-size: 14px;
    text-align: center;
}

/* list of artists */
.artist {
    display: block;
    float: left;
    margin: 10px;
    width: 128px;
}
.artist a {
    text-decoration: none;
}
.artist b {
    color: #1B1B1B;
    display: block;
    font-size: 14px;
    text-align: center;
}

Next – styles for the single artist info box and his tracks:

下一步–单个艺术家信息框及其曲目的样式:


/* single artist info */
.artist_info {
    display: block;
    margin: 10px;
}
.artist_info > a {
    display: block;
    float: left;
    text-decoration: none;
}
.artist_info b {
    color: #1B1B1B;
    display: block;
    font-size: 24px;
    text-align: center;
}
.artist_info div {
    color: #1B1B1B;
    float: right;
    font-size: 12px;
    padding-top: 30px;
    text-align: justify;
    width: 630px;
}
/* list of tracks */
.track {
    display: block;
    float: left;
    margin: 9px;
    width: 300px;
}
.track a {
    text-decoration: none;
    vertical-align: center;
}
.track img {
    margin-right: 10px;
    vertical-align: middle;
}
.track b {
    color: #1B1B1B;
    font-size: 13px;
}

/* single artist info */
.artist_info {
    display: block;
    margin: 10px;
}
.artist_info > a {
    display: block;
    float: left;
    text-decoration: none;
}
.artist_info b {
    color: #1B1B1B;
    display: block;
    font-size: 24px;
    text-align: center;
}
.artist_info div {
    color: #1B1B1B;
    float: right;
    font-size: 12px;
    padding-top: 30px;
    text-align: justify;
    width: 630px;
}
/* list of tracks */
.track {
    display: block;
    float: left;
    margin: 9px;
    width: 300px;
}
.track a {
    text-decoration: none;
    vertical-align: center;
}
.track img {
    margin-right: 10px;
    vertical-align: middle;
}
.track b {
    color: #1B1B1B;
    font-size: 13px;
}

I hope that it is very easy at the moment. Because the next step is the most important step

我希望目前这很容易。 因为下一步是最重要的步骤

步骤3. JavaScript (Step 3. JavaScript)

js / main.js (js/main.js)

// on page load
 $(window).load(function() {
    // define api keys
    var apiKey = 'YOUR_API_KEY';
    var apiSecret = 'YOUR_API_SECRET';
    // create a Cache object
    var cache = new LastFMCache();
    // create a LastFM object
    var lastfm = new LastFM({
        apiKey    : apiKey,
        apiSecret : apiSecret,
        cache     : cache
    });
    var topArtistName = '';
    // get weekly artist chart by tag 'trance'
    lastfm.tag.getWeeklyArtistChart({tag: 'trance', limit: 6}, {success: function(data){
        // render top weekly artist using 'lastfmTemplateArtists' template
        $('#top_artists').html(
            $('#lastfmTemplateArtists').render(data.weeklyartistchart.artist)
        );
        // define top artist name
        topArtistName = data.weeklyartistchart.artist[0].name;
        // load details of the artist
        lastfm.artist.getInfo({artist: topArtistName}, {success: function(data){
            // render the single artist info using 'lastfmTemplateArtistInfo' template
            $('#top_artist').html(
                $('#lastfmTemplateArtistInfo').render(data.artist)
            );
            // load the artis's top tracks
            lastfm.artist.getTopTracks({artist: topArtistName, limit: 9}, {success: function(data){
                // render the tracks using 'lastfmTemplateTracks' template
                $('#top_tracks').html(
                    $('#lastfmTemplateTracks').render(data.toptracks.track)
                );
            }});
        }, error: function(code, message){
            alert('Error #'+code+': '+message);
        }});
    }});
});

// on page load
 $(window).load(function() {
    // define api keys
    var apiKey = 'YOUR_API_KEY';
    var apiSecret = 'YOUR_API_SECRET';
    // create a Cache object
    var cache = new LastFMCache();
    // create a LastFM object
    var lastfm = new LastFM({
        apiKey    : apiKey,
        apiSecret : apiSecret,
        cache     : cache
    });
    var topArtistName = '';
    // get weekly artist chart by tag 'trance'
    lastfm.tag.getWeeklyArtistChart({tag: 'trance', limit: 6}, {success: function(data){
        // render top weekly artist using 'lastfmTemplateArtists' template
        $('#top_artists').html(
            $('#lastfmTemplateArtists').render(data.weeklyartistchart.artist)
        );
        // define top artist name
        topArtistName = data.weeklyartistchart.artist[0].name;
        // load details of the artist
        lastfm.artist.getInfo({artist: topArtistName}, {success: function(data){
            // render the single artist info using 'lastfmTemplateArtistInfo' template
            $('#top_artist').html(
                $('#lastfmTemplateArtistInfo').render(data.artist)
            );
            // load the artis's top tracks
            lastfm.artist.getTopTracks({artist: topArtistName, limit: 9}, {success: function(data){
                // render the tracks using 'lastfmTemplateTracks' template
                $('#top_tracks').html(
                    $('#lastfmTemplateTracks').render(data.toptracks.track)
                );
            }});
        }, error: function(code, message){
            alert('Error #'+code+': '+message);
        }});
    }});
});

I’ve already commented quite every line of my javascript code. Firstly – don’t forget to replace YOUR_API_KEY and YOUR_API_SECRET with your actual api keys. So, in the beginning, we create the instances of LastFMCache and LastFM classes, only in this case we will be able to use LastFM API. Our first function to obtain top weekly artists is: ‘tag.getWeeklyArtistChart’. As parameters – this are tag and limit. Once we have obtained the server response – we generate the HTML result with ‘render’ function. The second function (to obtain information) of single artist is: ‘artist.getInfo’ (with single param: artist). And finally, we can get the top tracks using ‘artist.getTopTracks’ function. As params we can pass the artist name (artist) and amount of results (limit).

我已经注释了我的javascript代码的每一行。 首先–不要忘记用您的实际api密钥替换YOUR_API_KEY和YOUR_API_SECRET。 因此,一开始,我们将创建LastFMCache和LastFM类的实例,只有在这种情况下,我们才能使用LastFM API。 我们获取顶级每周艺术家的第一个功能是:“ tag.getWeeklyArtistChart”。 作为参数–这是标记和限制。 一旦获得服务器响应,我们将使用“ render”功能生成HTML结果。 单个艺术家的第二个功能(获取信息)是:“ artist.getInfo”(带有单个参数:艺术家)。 最后,我们可以使用“ artist.getTopTracks”功能获得热门曲目。 作为参数,我们可以传递艺术家名称(艺术家)和结果数量(限制)。

现场演示

结论 (Conclusion)

I hope that everything is clean in today’s code. If you have any suggestions about further ideas for articles – you are welcome to share them with us. Good luck in your work!

我希望今天的代码中的所有内容都是干净的。 如果您对文章的进一步建议有任何建议,欢迎与我们分享。 祝您工作顺利!

翻译自: https://www.script-tutorials.com/last-fm-javascript-api-practice/

last.fm apk

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
系统要求:Android2.1及以上 大家都说-来电通CallMaster号称同行三最:“小体积-速度快-全数据”,常驻内存—显示来电归属地,系统服务技术佳,不会偷偷的在后台运行给机油们省点电,小编认为还是很… 应用简介 三大爆点,来电通给你全新的通讯体验。 爆点一:免费电话,免费短信,让你不再为话费所烦恼。 1、全新免费电话功能想打就打,好友不便接听时更可灵活转为免费语音留言,让你和好友随时随地轻松沟通,从此再无话费困扰。 2、全新免费短信功能,可与好友便捷语音聊天、分享图片、互刷搞怪表情,更支持多达50人的群聊,让你畅享海侃乐趣的同时免除短信费用负担; 3、智能短信,…... 展开三大爆点,来电通给你全新的通讯体验。 爆点一:免费电话,免费短信,让你不再为话费所烦恼。 1、全新免费电话功能想打就打,好友不便接听时更可灵活转为免费语音留言,让你和好友随时随地轻松沟通,从此再无话费困扰。 2、全新免费短信功能,可与好友便捷语音聊天、分享图片、互刷搞怪表情,更支持多达50人的群聊,让你畅享海侃乐趣的同时免除短信费用负担; 3、智能短信,让免费短信更加保险。 我们全新的智能短信功能,一如iPhone的iMessage,可根据好友在线状态智能选择使用普通或免费短信发送信息;暂未送达的免费短信更可设置自动或手动转用普通短信重发,确保你的每条消息及时送达。让你的Android手机,也能体验iPhone的独有功能。 爆点二:云拦截,让垃圾短信骚扰电话无处可藏。 1、不断强大的 云拦截 功能,借助云端特征库,可疑号码使用特定的红色头像来标识,从此疑似骚扰或诈骗号码的来电便一目了然,不再上当,来电通全面保护你的通讯安全。 你只需通过更新同步云端防骚扰版本库,就可以避免最新的垃圾电话和骚扰短信哦。 来电通独家对类似+020110的冒警电话进行了骚扰来电提醒。 如果没被拦截的骚扰来电,骚扰短信,也无需烦恼哦,轻轻一个举报,它便不会再骚扰你啦。 爆点三:全新界面UI,绝对让你眼前一亮。 全新的UI,全新的体验,各种有趣的东西等待你来挖掘。 1、全新UI全新体验,黑色简约大气风格,绝对让你眼前一亮 2、底部的功能栏,让友友寻找自己想要的功能,更加的快捷方便; 3、全新引入账号及头像体系。 来电通Android4.0Beta版本,提供了免费短信的功能,每个人都有自己的来电通账号,自然也可以上传漂亮的、个性化的头像咯。 4、工具箱中,有我们来电通原有的各大招牌功能。全新的按钮图标,全新的展示方式,不变的是经典实用的功能。 在骚扰拦截的模块图标上,更有小数字标签提示你拦截到多少骚扰的短信和电话哦。 5、趣味搜索,带给你全新的搜索体验 功能优化,体验更佳 1、信息会话界面,字体大小随心换 信息设置界面新增 信息字体大小 设置项,大字还是小字任你调。 2、拦截记录列表加载性能大幅提升,滑动更流畅; 信息拦截中被拦短信很多?每次进入都感觉很卡?最新版本对加载性能做了大幅提升,不但进入列表飞快,滑动更是非一般流畅哦。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值