-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
155 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
app/src/main/java/pl/lambada/songsync/data/remote/lyrics_providers/others/AppleAPI.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package pl.lambada.songsync.data.remote.lyrics_providers.others | ||
|
||
import io.ktor.client.request.get | ||
import io.ktor.client.statement.bodyAsText | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import pl.lambada.songsync.data.EmptyQueryException | ||
import pl.lambada.songsync.domain.model.SongInfo | ||
import pl.lambada.songsync.domain.model.lyrics_providers.others.AppleLyricsResponse | ||
import pl.lambada.songsync.domain.model.lyrics_providers.others.AppleSearchResponse | ||
import pl.lambada.songsync.util.ext.toLrcTimestamp | ||
import pl.lambada.songsync.util.networking.Ktor.client | ||
import pl.lambada.songsync.util.networking.Ktor.json | ||
import java.net.URLEncoder | ||
|
||
class AppleAPI { | ||
private val baseURL = "https://paxsenix.alwaysdata.net/" | ||
|
||
suspend fun getSongInfo(query: SongInfo): SongInfo? { | ||
val search = withContext(Dispatchers.IO) { | ||
URLEncoder.encode( | ||
"${query.songName} ${query.artistName}", | ||
Charsets.UTF_8.toString() | ||
) | ||
} | ||
|
||
if (search == " ") | ||
throw EmptyQueryException() | ||
|
||
val response = client.get( | ||
baseURL + "searchAppleMusic.php?q=$search" | ||
) | ||
val responseBody = response.bodyAsText(Charsets.UTF_8) | ||
|
||
if (response.status.value !in 200..299) | ||
return null | ||
|
||
val json = json.decodeFromString<List<AppleSearchResponse>>(responseBody) | ||
|
||
val result = json[0] | ||
|
||
return SongInfo( | ||
songName = result.songName, | ||
artistName = result.artistName, | ||
songLink = result.url, | ||
albumCoverLink = result.artwork.replace("{w}", "100").replace("{h}", "100").replace("{f}", "png"), | ||
appleID = result.id | ||
) | ||
} | ||
|
||
suspend fun getSyncedLyrics(id: Long): String? { | ||
val response = client.get( | ||
baseURL + "getAppleMusicLyrics.php?id=$id" | ||
) | ||
val responseBody = response.bodyAsText(Charsets.UTF_8) | ||
|
||
if (response.status.value !in 200..299 || responseBody.isEmpty()) | ||
return null | ||
|
||
val json = json.decodeFromString<AppleLyricsResponse>(responseBody) | ||
|
||
if (json.content!!.isEmpty()) | ||
return null | ||
|
||
val syncedLyrics = StringBuilder() | ||
val lines = json.content | ||
|
||
when (json.type) { | ||
"Syllable" -> { | ||
for (line in lines) { | ||
syncedLyrics.append("[${line.timestamp.toLrcTimestamp()}]") | ||
|
||
for (syllable in line.text) { | ||
syncedLyrics.append("<${syllable.timestamp!!.toLrcTimestamp()}>${syllable.text} ") | ||
} | ||
|
||
syncedLyrics.append("<${line.endtime.toLrcTimestamp()}>\n") | ||
} | ||
} | ||
"Line" -> { | ||
for (line in lines) { | ||
syncedLyrics.append("[${line.timestamp.toLrcTimestamp()}]${line.text[0].text}\n") | ||
} | ||
} | ||
else -> return null | ||
} | ||
|
||
return syncedLyrics.toString().dropLast(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
app/src/main/java/pl/lambada/songsync/domain/model/lyrics_providers/others/Apple.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package pl.lambada.songsync.domain.model.lyrics_providers.others | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class AppleSearchResponse( | ||
val id: Long, | ||
val songName: String, | ||
val artistName: String, | ||
val albumName: String, | ||
val artwork: String, | ||
val releaseDate: String, | ||
val duration: Int, | ||
val isrc: String, | ||
val url: String, | ||
val contentRating: String?, | ||
val albumId: String | ||
) | ||
|
||
@Serializable | ||
data class AppleLyricsResponse( | ||
val type: String, | ||
val content: List<AppleLyrics>? | ||
) | ||
|
||
@Serializable | ||
data class AppleLyrics( | ||
val text: List<AppleLyricsLineDetails>, | ||
val timestamp: Int, | ||
val endtime: Int | ||
) | ||
|
||
@Serializable | ||
data class AppleLyricsLineDetails( | ||
val text: String, | ||
val timestamp: Int? | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package pl.lambada.songsync.util.ext | ||
|
||
fun Int.toLrcTimestamp(): String { | ||
val minutes = this / 60000 | ||
val seconds = (this % 60000) / 1000 | ||
val milliseconds = this % 1000 | ||
|
||
val leadingZeros: Array<String> = arrayOf( | ||
if (minutes < 10) "0" else "", | ||
if (seconds < 10) "0" else "", | ||
if (milliseconds < 10) "00" else if (milliseconds < 100) "0" else "" | ||
) | ||
|
||
return "${leadingZeros[0]}$minutes:${leadingZeros[1]}$seconds.${leadingZeros[2]}$milliseconds" | ||
} |