210 lines
5.8 KiB
Dart
210 lines
5.8 KiB
Dart
// Author: fengshengxiong
|
|
// Date: 2024/6/18
|
|
// Description: 音乐播放器Api
|
|
|
|
import 'package:dio/dio.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:tone_snap/data/models/next_model.dart';
|
|
import 'package:tone_snap/data/models/player_model.dart';
|
|
import 'package:tone_snap/data/models/search_suggestions_model.dart';
|
|
import 'package:tone_snap/data/network/base_error.dart';
|
|
import 'package:tone_snap/data/network/dio_client.dart';
|
|
import 'package:tone_snap/data/storage/music_box.dart';
|
|
import 'package:tone_snap/global/app_config.dart';
|
|
|
|
class MusicApi {
|
|
static const String baseUrl = 'https://music.youtube.com/youtubei/v1/';
|
|
|
|
/// browse
|
|
static Future<T?> browse<T>({
|
|
String? visitorData,
|
|
Map<String, dynamic>? queryParameters,
|
|
T Function(Map<String, dynamic>)? formJson,
|
|
Function(BaseError baseError)? fail,
|
|
bool showLoading = false,
|
|
bool showToast = false,
|
|
}) async {
|
|
String clientVersion = MusicBox().getClientVersion();
|
|
String preferredLanguages = WidgetsBinding.instance.platformDispatcher.locale.languageCode;
|
|
final body = {
|
|
"context": {
|
|
"client": {
|
|
"visitorData": visitorData,
|
|
"clientName": "WEB_REMIX",
|
|
"clientVersion": clientVersion,
|
|
"platform": "DESKTOP",
|
|
"hl": preferredLanguages,
|
|
"gl": AppConfig.isoCode
|
|
}
|
|
}
|
|
};
|
|
T? model;
|
|
await DioClient().request<T>(
|
|
'browse',
|
|
showLoading: showLoading,
|
|
showToast: showToast,
|
|
requestMethod: RequestMethod.post,
|
|
data: body,
|
|
queryParameters: queryParameters,
|
|
formJson: formJson,
|
|
success: (data) => model = data,
|
|
fail: fail
|
|
);
|
|
return model;
|
|
}
|
|
|
|
/// next
|
|
static Future<NextModel?> next({
|
|
String? playlistId,
|
|
String? videoId,
|
|
bool showLoading = false,
|
|
bool showToast = true,
|
|
}) async {
|
|
String clientVersion = MusicBox().getClientVersion();
|
|
String preferredLanguages = WidgetsBinding.instance.platformDispatcher.locale.languageCode;
|
|
final body = {
|
|
"context": {
|
|
"client": {
|
|
"clientName": "WEB_REMIX",
|
|
"clientVersion": clientVersion,
|
|
"platform": "DESKTOP",
|
|
"hl": preferredLanguages,
|
|
"gl": AppConfig.isoCode
|
|
}
|
|
}
|
|
};
|
|
Map<String, dynamic>? queryParameters = {
|
|
'prettyPrint': false,
|
|
'playlistId': playlistId,
|
|
'videoId': videoId
|
|
};
|
|
NextModel? model;
|
|
await DioClient().request<NextModel>(
|
|
'next',
|
|
showLoading: showLoading,
|
|
showToast: showToast,
|
|
requestMethod: RequestMethod.post,
|
|
data: body,
|
|
queryParameters: queryParameters,
|
|
formJson: NextModel.fromMap,
|
|
success: (data) => model = data,
|
|
);
|
|
return model;
|
|
}
|
|
|
|
/// player
|
|
static Future<PlayerModel?> player({
|
|
String? videoId,
|
|
CancelToken? cancelToken,
|
|
Function(BaseError baseError)? fail,
|
|
bool showLoading = false,
|
|
bool showToast = true,
|
|
}) async {
|
|
String playerVersion = MusicBox().getPlayerVersion();
|
|
final body = {
|
|
"context": {
|
|
"client": {
|
|
"clientName": "ANDROID_MUSIC",
|
|
"clientVersion": playerVersion,
|
|
"platform": "MOBILE",
|
|
"browserVersion":"125.0.0.0"
|
|
}
|
|
}
|
|
};
|
|
Map<String, dynamic>? queryParameters = {
|
|
'prettyPrint': false,
|
|
'playlistId': null,
|
|
'videoId': videoId
|
|
};
|
|
PlayerModel? model;
|
|
await DioClient().request<PlayerModel>(
|
|
'player',
|
|
showLoading: showLoading,
|
|
showToast: showToast,
|
|
requestMethod: RequestMethod.post,
|
|
data: body,
|
|
queryParameters: queryParameters,
|
|
cancelToken: cancelToken,
|
|
formJson: PlayerModel.fromMap,
|
|
success: (data) => model = data,
|
|
fail: fail,
|
|
);
|
|
return model;
|
|
}
|
|
|
|
/// 搜索建议
|
|
static Future<SearchSuggestionsModel?> searchSuggestions({
|
|
String? input,
|
|
CancelToken? cancelToken,
|
|
bool showLoading = false,
|
|
bool showToast = false,
|
|
}) async {
|
|
String clientVersion = MusicBox().getClientVersion();
|
|
String preferredLanguages = WidgetsBinding.instance.platformDispatcher.locale.languageCode;
|
|
final body = {
|
|
"context": {
|
|
"client": {
|
|
"clientName": "WEB_REMIX",
|
|
"clientVersion": clientVersion,
|
|
"platform": "DESKTOP",
|
|
"hl": preferredLanguages,
|
|
"gl": AppConfig.isoCode
|
|
}
|
|
}
|
|
};
|
|
Map<String, dynamic>? queryParameters = {
|
|
'prettyPrint': false,
|
|
'input': input,
|
|
};
|
|
SearchSuggestionsModel? model;
|
|
await DioClient().request<SearchSuggestionsModel>(
|
|
'music/get_search_suggestions',
|
|
showLoading: showLoading,
|
|
showToast: showToast,
|
|
requestMethod: RequestMethod.post,
|
|
data: body,
|
|
queryParameters: queryParameters,
|
|
cancelToken: cancelToken,
|
|
formJson: SearchSuggestionsModel.fromMap,
|
|
success: (data) => model = data,
|
|
);
|
|
return model;
|
|
}
|
|
|
|
/// 搜索
|
|
static Future<T?> search<T>({
|
|
Map<String, dynamic>? queryParameters,
|
|
T Function(Map<String, dynamic>)? formJson,
|
|
Function(BaseError baseError)? fail,
|
|
bool showLoading = false,
|
|
bool showToast = true,
|
|
}) async {
|
|
String clientVersion = MusicBox().getClientVersion();
|
|
String preferredLanguages = WidgetsBinding.instance.platformDispatcher.locale.languageCode;
|
|
final body = {
|
|
"context": {
|
|
"client": {
|
|
"clientName": "WEB_REMIX",
|
|
"clientVersion": clientVersion,
|
|
"platform": "DESKTOP",
|
|
"hl": preferredLanguages,
|
|
"gl": AppConfig.isoCode
|
|
}
|
|
}
|
|
};
|
|
T? model;
|
|
await DioClient().request<T>(
|
|
'search',
|
|
showLoading: showLoading,
|
|
showToast: showToast,
|
|
requestMethod: RequestMethod.post,
|
|
data: body,
|
|
queryParameters: queryParameters,
|
|
formJson: formJson,
|
|
success: (data) => model = data,
|
|
fail: fail,
|
|
);
|
|
return model;
|
|
}
|
|
}
|