From 499bca5b1ca4038adea61d9c54ee9976138d5ea2 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Fri, 6 Sep 2024 16:37:49 +0800 Subject: [PATCH] :wheelchair: Better netease music check --- lib/services/sourced_track/sourced_track.dart | 5 +- .../sourced_track/sources/netease.dart | 17 +++- lib/widgets/player/sibling_tracks.dart | 86 ++++++++++--------- pubspec.yaml | 2 +- 4 files changed, 66 insertions(+), 44 deletions(-) diff --git a/lib/services/sourced_track/sourced_track.dart b/lib/services/sourced_track/sourced_track.dart index 211e785..cc6cec0 100755 --- a/lib/services/sourced_track/sourced_track.dart +++ b/lib/services/sourced_track/sourced_track.dart @@ -109,8 +109,9 @@ abstract class SourcedTrack extends Track { _ => await YoutubeSourcedTrack.fetchFromTrack(track: track), }; } on TrackNotFoundError catch (err) { - Get.find() - .showError('${err.toString()} via ${preferences.audioSource.label}'); + Get.find().showError( + '${err.toString()} via ${preferences.audioSource.label}, querying in fallback sources...', + ); return switch (preferences.audioSource) { AudioSource.piped || AudioSource.youtube => diff --git a/lib/services/sourced_track/sources/netease.dart b/lib/services/sourced_track/sources/netease.dart index fe6b211..5ca71f6 100755 --- a/lib/services/sourced_track/sources/netease.dart +++ b/lib/services/sourced_track/sources/netease.dart @@ -93,6 +93,12 @@ class NeteaseSourcedTrack extends SourcedTrack { 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( SourceMatchTableCompanion.insert( trackId: track.id!, @@ -111,7 +117,14 @@ class NeteaseSourcedTrack extends SourcedTrack { final client = getClient(); final resp = await client.get('/song/detail?ids=${cachedSource.sourceId}'); - final item = resp.body['songs'][0]; + final item = (resp.body['songs'] as List).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( siblings: [], @@ -201,7 +214,7 @@ class NeteaseSourcedTrack extends SourcedTrack { final client = getClient(); final resp = await client.get('/song/detail?ids=${newSourceInfo.id}'); - final item = resp.body['songs'][0]; + final item = (resp.body['songs'] as List).first; final (:info, :source) = toSiblingType(item); diff --git a/lib/widgets/player/sibling_tracks.dart b/lib/widgets/player/sibling_tracks.dart index 2e5c4b9..50c7f16 100644 --- a/lib/widgets/player/sibling_tracks.dart +++ b/lib/widgets/player/sibling_tracks.dart @@ -5,6 +5,7 @@ import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:google_fonts/google_fonts.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/services/database/database.dart'; import 'package:rhythm_box/services/duration.dart'; @@ -89,48 +90,55 @@ class _SiblingTracksState extends State { final preferences = Get.find().state.value; final searchTerm = _searchTermController.text.trim(); - if (preferences.audioSource == AudioSource.youtube || - preferences.audioSource == AudioSource.piped) { - final resultsYt = await youtubeClient.search.search(searchTerm.trim()); + try { + if (preferences.audioSource == AudioSource.youtube || + preferences.audioSource == AudioSource.piped) { + final resultsYt = await youtubeClient.search.search(searchTerm.trim()); - final searchResults = await Future.wait( - resultsYt.map(YoutubeVideoInfo.fromVideo).mapIndexed((i, video) async { - final siblingType = await YoutubeSourcedTrack.toSiblingType(i, video); - return siblingType.info; - }), - ); - final activeSourceInfo = (_activeTrack! as SourcedTrack).sourceInfo; - _siblings = List.from( - searchResults - ..removeWhere((element) => element.id == activeSourceInfo.id) - ..insert( - 0, - activeSourceInfo, - ), - growable: true, - ); - } else if (preferences.audioSource == AudioSource.netease) { - final client = NeteaseSourcedTrack.getClient(); - final resp = await client - .get('/search?keywords=${Uri.encodeComponent(searchTerm)}'); - final searchResults = resp.body['result']['songs'] - .map(NeteaseSourcedTrack.toSiblingType) - .map((x) => x.info) - .toList(); + final searchResults = await Future.wait( + resultsYt + .map(YoutubeVideoInfo.fromVideo) + .mapIndexed((i, video) async { + final siblingType = + await YoutubeSourcedTrack.toSiblingType(i, video); + return siblingType.info; + }), + ); + final activeSourceInfo = (_activeTrack! as SourcedTrack).sourceInfo; + _siblings = List.from( + searchResults + ..removeWhere((element) => element.id == activeSourceInfo.id) + ..insert( + 0, + activeSourceInfo, + ), + growable: true, + ); + } else if (preferences.audioSource == AudioSource.netease) { + final client = NeteaseSourcedTrack.getClient(); + final resp = await client + .get('/search?keywords=${Uri.encodeComponent(searchTerm)}'); + final searchResults = resp.body['result']['songs'] + .map(NeteaseSourcedTrack.toSiblingType) + .map((x) => x.info) + .toList(); - final activeSourceInfo = (_activeTrack! as SourcedTrack).sourceInfo; - _siblings = List.from( - searchResults - ..removeWhere((element) => element.id == activeSourceInfo.id) - ..insert( - 0, - activeSourceInfo, - ), - growable: true, - ); + final activeSourceInfo = (_activeTrack! as SourcedTrack).sourceInfo; + _siblings = List.from( + searchResults + ..removeWhere((element) => element.id == activeSourceInfo.id) + ..insert( + 0, + activeSourceInfo, + ), + growable: true, + ); + } + } catch (err) { + Get.find().showError(err.toString()); + } finally { + setState(() => _isSearching = false); } - - setState(() => _isSearching = false); } @override diff --git a/pubspec.yaml b/pubspec.yaml index 0d72279..2d61fcf 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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 # 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. -version: 1.0.0+10 +version: 1.0.0+11 environment: sdk: ^3.5.0