Skip to content

Instantly share code, notes, and snippets.

@Lapotor
Last active January 27, 2020 22:30
Show Gist options
  • Save Lapotor/dfe09f0ad571792e6b340875497aa674 to your computer and use it in GitHub Desktop.
Save Lapotor/dfe09f0ad571792e6b340875497aa674 to your computer and use it in GitHub Desktop.
iTunes Badge

Script to show iTunes Badge

This very simple script can be used to show the iTunes Buy now or Pre-order Badge.

Created by me Lapotor.

Requirements

You need jQuery to run this. You can choose a version from https://code.jquery.com/ or use the following:

<script src="https://code.jquery.com/jquery-3.4.1.min.js"
        integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
        crossorigin="anonymous"></script>

Of course you need to import the functions.js.
And you need to add the following below the jQuery and functions import: Edit the variable STATION_NAME at the beginning where it says <<LAUT.FM STATION NAME>> with your Laut.fm station name.

/* === EDIT BELOW === */
let STATION_NAME = "<<LAUT.FM STATION NAME>>";

/* === DON'T EDIT === */
let song_url = "https://api.laut.fm/station/" + STATION_NAME + "/current_song";
let song_str;

iTunesBadge(song_url);
badgeLoop(song_url);

The test.html file shows the Structure how it needed to get implemented into your page.
It's very simple, this is needed more or less:

<div id="iTunes">
    <a href="" target="_top _blank">
        <img src="images/buy_now.svg" width="140" height="41" alt="Erhältlich im iTunes Store">
    </a>
</div>

If needed change the src attribute of the <img> to your path for the buy now badge.

Get the badges

To get the badges in your language few this page:
https://www.apple.com/itunes/marketing-on-itunes/identity-guidelines.html#itunes-store-badges

License

To be sure for legal reasons this work is licensed under the MIT License:
https://opensource.org/licenses/MIT

/* Copyright (C) 2020 Lapotor <https://github.com/lapotor>
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/* === EDIT BELOW === */
let pre_order = "images/pre_order.svg";
let buy_now = "images/buy_now.svg";
/* === DON'T EDIT === */
function getITunesUrl(song, artist, german) {
let url = "https://itunes.apple.com/search?term=" + encodeURIComponent(artist) + "+" + encodeURIComponent(song) + "&entity=album&entity=musicArtist&entity=musicTrack&limit=1";
if (german) {
url += "&country=de"
}
return url;
}
function getTrackViewUrl(iTunesJson) {
return iTunesJson.results[0].trackViewUrl;
}
function getReleaseDate(iTunesJson) {
return iTunesJson.results[0].releaseDate;
}
function badgeLoop(song_url) {
setTimeout(function () {
iTunesBadge(song_url);
badgeLoop(song_url);
}, 2000)
}
function iTunesBadge(song_url) {
$.getJSON(song_url, function (current_song) {
if (song_str != (current_song.title + " " + current_song.artist.name)) {
song_str = current_song.title + " " + current_song.artist.name;
console.info("New Song: " + current_song.title + " - " + current_song.artist.name);
$.getJSON(getITunesUrl(current_song.title, current_song.artist.name, true), function (iTunesData) {
console.log(iTunesData.resultCount);
if (iTunesData.resultCount === 0) {
$("#iTunes").css("display", "none");
console.log("hide")
} else {
$("#iTunes").removeProp("display")
}
if (new Date(getReleaseDate(iTunesData)) > new Date()) {
$("#iTunes img").attr("src", pre_order).attr("alt", "Vorbestellen im iTunes Store");
} else {
$("#iTunes img").attr("src", buy_now).attr("alt", "Erhältlich im iTunes Store");
}
$("#iTunes a").attr("href", getTrackViewUrl(iTunesData));
})
}
})
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!-- Important structure -->
<div id="iTunes">
<a href="" target="_top _blank">
<img src="images/buy_now.svg" width="140" height="41" alt="Erhältlich im iTunes Store">
</a>
</div>
<!-- /Important structure -->
<script src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>
<script src="functions.js"></script>
<script>
/* === EDIT BELOW === */
let STATION_NAME = "ac-plus";
/* === DON'T EDIT === */
let song_url = "https://api.laut.fm/station/" + STATION_NAME + "/current_song";
let song_str;
iTunesBadge(song_url);
badgeLoop(song_url);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment