Better netease music check

This commit is contained in:
LittleSheep 2024-09-06 16:37:49 +08:00
parent 252e4619f7
commit 499bca5b1c
4 changed files with 66 additions and 44 deletions

View File

@ -109,8 +109,9 @@ abstract class SourcedTrack extends Track {
_ => await YoutubeSourcedTrack.fetchFromTrack(track: track), _ => await YoutubeSourcedTrack.fetchFromTrack(track: track),
}; };
} on TrackNotFoundError catch (err) { } on TrackNotFoundError catch (err) {
Get.find<ErrorNotifier>() Get.find<ErrorNotifier>().showError(
.showError('${err.toString()} via ${preferences.audioSource.label}'); '${err.toString()} via ${preferences.audioSource.label}, querying in fallback sources...',
);
return switch (preferences.audioSource) { return switch (preferences.audioSource) {
AudioSource.piped || AudioSource.piped ||
AudioSource.youtube => AudioSource.youtube =>

View File

@ -93,6 +93,12 @@ class NeteaseSourcedTrack extends SourcedTrack {
throw TrackNotFoundError(track); throw TrackNotFoundError(track);
} }
final client = getClient();
final checkResp = await client.get(
'/check/music?id=${siblings.first.info.id}&realIP=${await lookupRealIp()}',
);
if (checkResp.body['success'] != true) throw TrackNotFoundError(track);
await db.database.into(db.database.sourceMatchTable).insert( await db.database.into(db.database.sourceMatchTable).insert(
SourceMatchTableCompanion.insert( SourceMatchTableCompanion.insert(
trackId: track.id!, trackId: track.id!,
@ -111,7 +117,14 @@ class NeteaseSourcedTrack extends SourcedTrack {
final client = getClient(); final client = getClient();
final resp = await client.get('/song/detail?ids=${cachedSource.sourceId}'); final resp = await client.get('/song/detail?ids=${cachedSource.sourceId}');
final item = resp.body['songs'][0]; final item = (resp.body['songs'] as List<dynamic>).firstOrNull;
if (item == null) throw TrackNotFoundError(track);
final checkResp = await client.get(
'/check/music?id=${item['id']}&realIP=${await lookupRealIp()}',
);
if (checkResp.body['success'] != true) throw TrackNotFoundError(track);
return NeteaseSourcedTrack( return NeteaseSourcedTrack(
siblings: [], siblings: [],
@ -201,7 +214,7 @@ class NeteaseSourcedTrack extends SourcedTrack {
final client = getClient(); final client = getClient();
final resp = await client.get('/song/detail?ids=${newSourceInfo.id}'); final resp = await client.get('/song/detail?ids=${newSourceInfo.id}');
final item = resp.body['songs'][0]; final item = (resp.body['songs'] as List<dynamic>).first;
final (:info, :source) = toSiblingType(item); final (:info, :source) = toSiblingType(item);

View File

@ -5,6 +5,7 @@ import 'package:flutter/material.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:google_fonts/google_fonts.dart'; import 'package:google_fonts/google_fonts.dart';
import 'package:rhythm_box/providers/audio_player.dart'; import 'package:rhythm_box/providers/audio_player.dart';
import 'package:rhythm_box/providers/error_notifier.dart';
import 'package:rhythm_box/providers/user_preferences.dart'; import 'package:rhythm_box/providers/user_preferences.dart';
import 'package:rhythm_box/services/database/database.dart'; import 'package:rhythm_box/services/database/database.dart';
import 'package:rhythm_box/services/duration.dart'; import 'package:rhythm_box/services/duration.dart';
@ -89,48 +90,55 @@ class _SiblingTracksState extends State<SiblingTracks> {
final preferences = Get.find<UserPreferencesProvider>().state.value; final preferences = Get.find<UserPreferencesProvider>().state.value;
final searchTerm = _searchTermController.text.trim(); final searchTerm = _searchTermController.text.trim();
if (preferences.audioSource == AudioSource.youtube || try {
preferences.audioSource == AudioSource.piped) { if (preferences.audioSource == AudioSource.youtube ||
final resultsYt = await youtubeClient.search.search(searchTerm.trim()); preferences.audioSource == AudioSource.piped) {
final resultsYt = await youtubeClient.search.search(searchTerm.trim());
final searchResults = await Future.wait( final searchResults = await Future.wait(
resultsYt.map(YoutubeVideoInfo.fromVideo).mapIndexed((i, video) async { resultsYt
final siblingType = await YoutubeSourcedTrack.toSiblingType(i, video); .map(YoutubeVideoInfo.fromVideo)
return siblingType.info; .mapIndexed((i, video) async {
}), final siblingType =
); await YoutubeSourcedTrack.toSiblingType(i, video);
final activeSourceInfo = (_activeTrack! as SourcedTrack).sourceInfo; return siblingType.info;
_siblings = List.from( }),
searchResults );
..removeWhere((element) => element.id == activeSourceInfo.id) final activeSourceInfo = (_activeTrack! as SourcedTrack).sourceInfo;
..insert( _siblings = List.from(
0, searchResults
activeSourceInfo, ..removeWhere((element) => element.id == activeSourceInfo.id)
), ..insert(
growable: true, 0,
); activeSourceInfo,
} else if (preferences.audioSource == AudioSource.netease) { ),
final client = NeteaseSourcedTrack.getClient(); growable: true,
final resp = await client );
.get('/search?keywords=${Uri.encodeComponent(searchTerm)}'); } else if (preferences.audioSource == AudioSource.netease) {
final searchResults = resp.body['result']['songs'] final client = NeteaseSourcedTrack.getClient();
.map(NeteaseSourcedTrack.toSiblingType) final resp = await client
.map((x) => x.info) .get('/search?keywords=${Uri.encodeComponent(searchTerm)}');
.toList(); final searchResults = resp.body['result']['songs']
.map(NeteaseSourcedTrack.toSiblingType)
.map((x) => x.info)
.toList();
final activeSourceInfo = (_activeTrack! as SourcedTrack).sourceInfo; final activeSourceInfo = (_activeTrack! as SourcedTrack).sourceInfo;
_siblings = List.from( _siblings = List.from(
searchResults searchResults
..removeWhere((element) => element.id == activeSourceInfo.id) ..removeWhere((element) => element.id == activeSourceInfo.id)
..insert( ..insert(
0, 0,
activeSourceInfo, activeSourceInfo,
), ),
growable: true, growable: true,
); );
}
} catch (err) {
Get.find<ErrorNotifier>().showError(err.toString());
} finally {
setState(() => _isSearching = false);
} }
setState(() => _isSearching = false);
} }
@override @override

View File

@ -16,7 +16,7 @@ publish_to: "none" # Remove this line if you wish to publish to pub.dev
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts # In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix. # of the product and file versions while build-number is used as the build suffix.
version: 1.0.0+10 version: 1.0.0+11
environment: environment:
sdk: ^3.5.0 sdk: ^3.5.0