Skip to content

Commit

Permalink
Add support for fetching music charts
Browse files Browse the repository at this point in the history
  • Loading branch information
stevesoltys committed Dec 14, 2022
1 parent 4394944 commit 7c26bab
Show file tree
Hide file tree
Showing 15 changed files with 270 additions and 25 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Build and test
on:
- pull_request
- push

jobs:
build:
name: Build and test

runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1

- name: Set up JDK 11
uses: actions/setup-java@v1
with:
java-version: 11.0.4

- name: Build and test
run: |
./gradlew :build --stacktrace
env:
TEAM_ID: ${{ secrets.TEAM_ID }}
KEY_ID: ${{ secrets.KEY_ID }}
PRIVATE_KEY_BASE64: ${{ secrets.PRIVATE_KEY_BASE64 }}
4 changes: 0 additions & 4 deletions .travis.yml

This file was deleted.

19 changes: 13 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,22 @@ version "0.1.0"

repositories {
mavenCentral()
jcenter()
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"

implementation 'com.fasterxml.jackson.module:jackson-module-kotlin:2.14.1'

implementation "com.squareup.retrofit2:retrofit:2.9.0"
implementation "com.squareup.retrofit2:converter-jackson:2.1.0"
implementation 'io.jsonwebtoken:jjwt-api:0.11.2'
implementation 'io.jsonwebtoken:jjwt-impl:0.11.2'
implementation 'io.jsonwebtoken:jjwt-jackson:0.11.2'
implementation "com.squareup.retrofit2:converter-jackson:2.9.0"

implementation "io.jsonwebtoken:jjwt-api:0.11.5"
implementation "io.jsonwebtoken:jjwt-impl:0.11.5"
implementation "io.jsonwebtoken:jjwt-jackson:0.11.5"

testImplementation 'io.kotest:kotest-runner-junit5:5.5.4'
testImplementation "io.kotest:kotest-assertions-core:5.5.4"
}

compileKotlin {
Expand All @@ -29,4 +34,6 @@ compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}

mainClassName = 'com.stevesoltys.applemusic.Main'
test {
useJUnitPlatform()
}
40 changes: 31 additions & 9 deletions src/main/kotlin/com/stevesoltys/applemusic/AppleMusic.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package com.stevesoltys.applemusic

import com.stevesoltys.applemusic.model.album.AlbumResponse
import com.stevesoltys.applemusic.model.artist.ArtistResponse
import com.stevesoltys.applemusic.model.chart.ChartResponse
import com.stevesoltys.applemusic.model.chart.ChartResultType
import com.stevesoltys.applemusic.model.chart.ChartType
import com.stevesoltys.applemusic.model.search.SearchResponse
import com.stevesoltys.applemusic.model.search.SearchResultType
import com.stevesoltys.applemusic.net.AppleMusicHttpException
Expand Down Expand Up @@ -66,6 +69,31 @@ class AppleMusic(
)
}

/**
* Get catalog charts.
*/
fun getCatalogCharts(
types: Set<ChartResultType>,
localization: String? = null,
chart: String? = null,
offset: String? = null,
limit: Int? = null,
genre: String? = null,
with: Set<ChartType>? = null
): ChartResponse {
return call(
appleMusicService.getCatalogCharts(
types = types.map { it.identifier }.toTypedArray(),
localization = localization,
chart = chart,
offset = offset,
limit = limit,
genre = genre,
with = with?.map { it.identifier }?.toTypedArray()
)
)
}

/**
* Get an artist.
*/
Expand Down Expand Up @@ -104,23 +132,17 @@ class AppleMusic(
val limit = 100
var offset = 0

var currentResponse =
call(appleMusicService.getAlbumsByArtistId(id, offset.toString(), limit))

var currentResponse = call(appleMusicService.getAlbumsByArtistId(id, 0.toString(), limit))
val result = currentResponse.data.toCollection(ArrayList())

while (currentResponse.next != null) {
offset += currentResponse.data.size

currentResponse =
call(appleMusicService.getAlbumsByArtistId(id, offset.toString(), limit))

currentResponse = call(appleMusicService.getAlbumsByArtistId(id, offset.toString(), limit))
result.addAll(currentResponse.data)
}

val response = AlbumResponse()
response.data = result.toTypedArray()
return response
return AlbumResponse(result.toTypedArray())
}

private fun <T> call(call: Call<T>): T {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.stevesoltys.applemusic.model.album

class AlbumChartResponse(
val chart: String,
val name: String,
data: Array<Album> = emptyArray()
) : AlbumResponse(data)
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import com.stevesoltys.applemusic.model.ResponseRoot
/**
* @author Steve Soltys
*/
class AlbumResponse : ResponseRoot() {

lateinit var data: Array<Album>
}
open class AlbumResponse(
val data: Array<Album> = emptyArray()
) : ResponseRoot()
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.stevesoltys.applemusic.model.chart

/**
* @author Steve Soltys
*/
class ChartResponse(
val results: ChartResults
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.stevesoltys.applemusic.model.chart

/**
* @author Steve Soltys
*/
enum class ChartResultType(val identifier: String) {
ALBUMS("albums"),
MUSIC_VIDEOS("music-videos"),
PLAYLISTS("playlists"),
SONGS("songs")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.stevesoltys.applemusic.model.chart

import com.stevesoltys.applemusic.model.album.AlbumChartResponse
import com.stevesoltys.applemusic.model.artist.ArtistResponse
import com.stevesoltys.applemusic.model.track.song.SongChartResponse

/**
* @author Steve Soltys
*/
class ChartResults(
val albums: Array<AlbumChartResponse> = emptyArray(),
val songs: Array<SongChartResponse> = emptyArray()
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.stevesoltys.applemusic.model.chart

/**
* @author Steve Soltys
*/
enum class ChartType(val identifier: String) {
CITY_CHARTS("cityCharts"),
DAILY_GLOBAL_TOP_CHARTS("dailyGlobalTopCharts")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.stevesoltys.applemusic.model.track.song

class SongChartResponse(
val chart: String,
val name: String,
data: Array<Song> = emptyArray()
) : SongResponse(data)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.stevesoltys.applemusic.model.track.song

import com.stevesoltys.applemusic.model.ResponseRoot

/**
* @author Steve Soltys
*/
open class SongResponse(
val data: Array<Song> = emptyArray()
) : ResponseRoot()
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ package com.stevesoltys.applemusic.net

import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.KotlinModule

/**
* @author Steve Soltys
*/
class AppleMusicObjectMapper : ObjectMapper() {
init {
configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)

registerModule(
KotlinModule.Builder()
.build()
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.stevesoltys.applemusic.net

import com.stevesoltys.applemusic.model.album.AlbumResponse
import com.stevesoltys.applemusic.model.artist.ArtistResponse
import com.stevesoltys.applemusic.model.chart.ChartResponse
import com.stevesoltys.applemusic.model.search.SearchResponse
import retrofit2.Call
import retrofit2.http.GET
Expand All @@ -21,14 +22,27 @@ interface AppleMusicService {
@Query("include") types: Array<String>? = null
): Call<SearchResponse>

@GET("charts")
fun getCatalogCharts(
@Query("types") types: Array<String>,
@Query("l") localization: String? = null,
@Query("chart") chart: String? = null,
@Query("offset") offset: String? = null,
@Query("limit") limit: Int? = null,
@Query("genre") genre: String? = null,
@Query("with") with: Array<String>? = null
): Call<ChartResponse>

@GET("artists/{id}")
fun getArtistById(
@Path("id") id: String,
@Query("include") types: Array<String>? = null
): Call<ArtistResponse>

@GET("artists")
fun getArtistsById(@Query("ids") ids: Array<String>): Call<ArtistResponse>
fun getArtistsById(
@Query("ids") ids: Array<String>
): Call<ArtistResponse>

@GET("albums/{id}")
fun getAlbumById(
Expand All @@ -37,7 +51,9 @@ interface AppleMusicService {
): Call<AlbumResponse>

@GET("albums")
fun getAlbumsById(@Query("ids") ids: Array<String>): Call<AlbumResponse>
fun getAlbumsById(
@Query("ids") ids: Array<String>
): Call<AlbumResponse>

@GET("artists/{id}/albums")
fun getAlbumsByArtistId(
Expand Down
Loading

0 comments on commit 7c26bab

Please sign in to comment.