Skip to content

Commit 1560caf

Browse files
committed
download playlist
1 parent 65533eb commit 1560caf

4 files changed

Lines changed: 49 additions & 11 deletions

File tree

README.MD

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ _*Этот бот только для персонального использ
1111
1. Любой текст отправляемый боту выполняет поиск по яндекс музыке.
1212
2. Отправив команду "/daily" можно получить свой персональный плейлист.
1313
3. Отправив команду "/playlists" можно получить список персональных плейлистов.
14-
4. Отправив команду "/login с параметрам" происходит обновление сессии яндекс музыки
14+
4. Отправив команду "/login" можно обновить сессию яндекс музыки без рестарта бота.
15+
5. Отправив команду "/download" можно скачать предыдущий найденный плейлист по частям.
1516

1617
### Соберите проект командой, используйте Java 8 и выше.
1718

src/main/java/bot/common/UtilClasses.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package bot.common
22

33
import bot.telegram.callback.Callback
4+
import bot.telegram.repository.model.UserStorageData
45
import bot.yandex.dto.domain.CaptchaInfo
56
import java.net.URLDecoder
67
import java.net.URLEncoder
@@ -30,4 +31,14 @@ inline fun <reified T> ResultOf<T>.returnNok(block: (ResultOf<Nothing>) -> Unit)
3031

3132

3233
fun String.urlEncode(): String = URLEncoder.encode(this, java.nio.charset.StandardCharsets.UTF_8.toString())
33-
fun String.urlDecode(): String = URLDecoder.decode(this, java.nio.charset.StandardCharsets.UTF_8.toString())
34+
fun String.urlDecode(): String = URLDecoder.decode(this, java.nio.charset.StandardCharsets.UTF_8.toString())
35+
36+
inline fun UserStorageData?.isDownloadProcess(block: () -> Unit) {
37+
if (this == null) {
38+
return
39+
}
40+
val inProgress: Boolean = !((this.loaded.find{ it == false }) ?: true)
41+
if (inProgress) {
42+
return block()
43+
}
44+
}

src/main/java/bot/common/Utils.kt

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
11
package bot.common
22

3-
import org.telegram.telegrambots.meta.api.objects.Message
43
import org.telegram.telegrambots.meta.api.objects.User
54

65
object Utils {
7-
/**
8-
* Формирование имени пользователя
9-
* @param msg сообщение
10-
*/
11-
fun getUserName(msg: Message): String {
12-
return getUserName(msg.from)
13-
}
14-
156
/**
167
* Формирование имени пользователя. Если заполнен никнейм, используем его. Если нет - используем фамилию и имя
178
* @param user пользователь

src/main/java/bot/telegram/callback/PlaylistActionsImpl.kt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ class PlaylistActionsImpl(val yandexMusic: YandexMusic, val messageSource: Messa
130130
}
131131

132132
override fun playlist(userId: Long, chatId: String, callback: PlaylistCallback): ResultOf<SendMessage> {
133+
userStorage.getUserStorageData(userId).isDownloadProcess {
134+
return ResultOf.Failure(
135+
ErrorBuilder.newBuilder(ErrorKind.APP_INTERNAL)
136+
.withCode(403)
137+
.withDescription(messageSource.getMessage("errors.latest.playlists.position.using", null, Locale.getDefault())))
138+
}
139+
133140
val playlistResult = yandexMusic.getPlaylist(callback.kind, callback.owner)
134141
playlistResult.returnNok { return it }
135142
val playlist = (playlistResult as ResultOf.Success).value
@@ -151,6 +158,13 @@ class PlaylistActionsImpl(val yandexMusic: YandexMusic, val messageSource: Messa
151158
* Daily playlist for current yandex user
152159
* */
153160
override fun dailyPlaylist(userId: Long, chatId: String): ResultOf<SendMessage> {
161+
userStorage.getUserStorageData(userId).isDownloadProcess {
162+
return ResultOf.Failure(
163+
ErrorBuilder.newBuilder(ErrorKind.APP_INTERNAL)
164+
.withCode(403)
165+
.withDescription(messageSource.getMessage("errors.latest.playlists.position.using", null, Locale.getDefault())))
166+
}
167+
154168
val dailyIdResult = yandexMusic.getPlaylists()
155169
dailyIdResult.returnNok { return it }
156170
val dailyPlaylists = (dailyIdResult as ResultOf.Success).value
@@ -180,6 +194,13 @@ class PlaylistActionsImpl(val yandexMusic: YandexMusic, val messageSource: Messa
180194
* Show search tracks to user
181195
* */
182196
override fun searchByString(userId: Long, chatId: String, text: String): ResultOf<SendMessage> {
197+
userStorage.getUserStorageData(userId).isDownloadProcess {
198+
return ResultOf.Failure(
199+
ErrorBuilder.newBuilder(ErrorKind.APP_INTERNAL)
200+
.withCode(403)
201+
.withDescription(messageSource.getMessage("errors.latest.playlists.position.using", null, Locale.getDefault())))
202+
}
203+
183204
// TODO cut string to limit
184205
val searchText = URLEncoder.encode(text.trimIndent(), "utf-8")
185206
val searchResult = yandexMusic.search(searchText)
@@ -209,6 +230,13 @@ class PlaylistActionsImpl(val yandexMusic: YandexMusic, val messageSource: Messa
209230
* Limit for json, when more than 150 we should request by ids
210231
* */
211232
override fun artistWithPagesMsg(userId: Long, chatId: String, callback: ArtistTrackWithPagesCallback): ResultOf<SendMessage> {
233+
userStorage.getUserStorageData(userId).isDownloadProcess {
234+
return ResultOf.Failure(
235+
ErrorBuilder.newBuilder(ErrorKind.APP_INTERNAL)
236+
.withCode(403)
237+
.withDescription(messageSource.getMessage("errors.latest.playlists.position.using", null, Locale.getDefault())))
238+
}
239+
212240
val page = callback.page
213241
val searchResult = yandexMusic.searchTrack(callback.artistId)
214242
searchResult.returnNok { return it }
@@ -237,6 +265,13 @@ class PlaylistActionsImpl(val yandexMusic: YandexMusic, val messageSource: Messa
237265
* Pagination for searched tracks
238266
* */
239267
override fun searchWithPagesMsg(userId: Long, chatId: String, callback: SearchTrackWithPagesCallback): ResultOf<SendMessage> {
268+
userStorage.getUserStorageData(userId).isDownloadProcess {
269+
return ResultOf.Failure(
270+
ErrorBuilder.newBuilder(ErrorKind.APP_INTERNAL)
271+
.withCode(403)
272+
.withDescription(messageSource.getMessage("errors.latest.playlists.position.using", null, Locale.getDefault())))
273+
}
274+
240275
//logger.info("searchWithPagesMsg callback = {}", callback)
241276
val page = callback.page
242277
val realPageForRequest = ((page - 1) * TRACKS_PER_PAGE) / 100

0 commit comments

Comments
 (0)