mirror of
https://github.com/KRTirtho/spotube.git
synced 2025-09-13 16:05:18 +00:00
Custom format for search term in toYoutubeTrack method impletemented
This commit is contained in:
parent
94bfe5ccc6
commit
a7b3073539
@ -20,11 +20,17 @@ class Settings extends HookConsumerWidget {
|
|||||||
Widget build(BuildContext context, ref) {
|
Widget build(BuildContext context, ref) {
|
||||||
final UserPreferences preferences = ref.watch(userPreferencesProvider);
|
final UserPreferences preferences = ref.watch(userPreferencesProvider);
|
||||||
final Auth auth = ref.watch(authProvider);
|
final Auth auth = ref.watch(authProvider);
|
||||||
var geniusAccessToken = useState<String?>(null);
|
final geniusAccessToken = useState<String?>(null);
|
||||||
TextEditingController textEditingController = useTextEditingController();
|
TextEditingController geniusTokenController = useTextEditingController();
|
||||||
|
final ytSearchFormatController =
|
||||||
|
useTextEditingController(text: preferences.ytSearchFormat);
|
||||||
|
|
||||||
textEditingController.addListener(() {
|
geniusTokenController.addListener(() {
|
||||||
geniusAccessToken.value = textEditingController.value.text;
|
geniusAccessToken.value = geniusTokenController.value.text;
|
||||||
|
});
|
||||||
|
|
||||||
|
ytSearchFormatController.addListener(() {
|
||||||
|
preferences.setYtSearchFormat(ytSearchFormatController.value.text);
|
||||||
});
|
});
|
||||||
|
|
||||||
return SafeArea(
|
return SafeArea(
|
||||||
@ -52,7 +58,7 @@ class Settings extends HookConsumerWidget {
|
|||||||
Expanded(
|
Expanded(
|
||||||
flex: 1,
|
flex: 1,
|
||||||
child: TextField(
|
child: TextField(
|
||||||
controller: textEditingController,
|
controller: geniusTokenController,
|
||||||
decoration: InputDecoration(
|
decoration: InputDecoration(
|
||||||
hintText: preferences.geniusAccessToken,
|
hintText: preferences.geniusAccessToken,
|
||||||
),
|
),
|
||||||
@ -76,7 +82,7 @@ class Settings extends HookConsumerWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
geniusAccessToken.value = null;
|
geniusAccessToken.value = null;
|
||||||
textEditingController.text = "";
|
geniusTokenController.text = "";
|
||||||
}
|
}
|
||||||
: null,
|
: null,
|
||||||
child: const Text("Save"),
|
child: const Text("Save"),
|
||||||
@ -174,6 +180,23 @@ class Settings extends HookConsumerWidget {
|
|||||||
],
|
],
|
||||||
),
|
),
|
||||||
const SizedBox(height: 10),
|
const SizedBox(height: 10),
|
||||||
|
Row(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||||
|
children: [
|
||||||
|
const Expanded(
|
||||||
|
flex: 2,
|
||||||
|
child: Text(
|
||||||
|
"Format of the YouTube Search term (Case sensitive)"),
|
||||||
|
),
|
||||||
|
Expanded(
|
||||||
|
flex: 1,
|
||||||
|
child: TextField(
|
||||||
|
controller: ytSearchFormatController,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
const SizedBox(height: 10),
|
||||||
if (auth.isAnonymous)
|
if (auth.isAnonymous)
|
||||||
Wrap(
|
Wrap(
|
||||||
spacing: 20,
|
spacing: 20,
|
||||||
|
@ -1,11 +1,24 @@
|
|||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
import 'package:spotify/spotify.dart';
|
import 'package:spotify/spotify.dart';
|
||||||
|
import 'package:spotube/helpers/getLyrics.dart';
|
||||||
|
import 'package:spotube/models/Logger.dart';
|
||||||
import 'package:youtube_explode_dart/youtube_explode_dart.dart';
|
import 'package:youtube_explode_dart/youtube_explode_dart.dart';
|
||||||
|
|
||||||
Future<Track> toYoutubeTrack(YoutubeExplode youtube, Track track) async {
|
final logger = getLogger("toYoutubeTrack");
|
||||||
var artistsName = track.artists?.map((ar) => ar.name).toList() ?? [];
|
Future<Track> toYoutubeTrack(
|
||||||
String queryString =
|
YoutubeExplode youtube, Track track, String format) async {
|
||||||
"${artistsName.first} - ${track.name}${artistsName.length > 1 ? " feat. ${artistsName.sublist(1).join(" ")}" : ""}";
|
final artistsName = track.artists?.map((ar) => ar.name).toList() ?? [];
|
||||||
|
logger.v("[Track Search Artists] $artistsName");
|
||||||
|
final mainArtist = artistsName.first ?? "";
|
||||||
|
final featuredArtists =
|
||||||
|
artistsName.length > 1 ? "feat. " + artistsName.sublist(1).join(" ") : "";
|
||||||
|
final title = getTitle(track.name!, "").trim();
|
||||||
|
logger.v("[Track Search Title] $title");
|
||||||
|
final queryString = format
|
||||||
|
.replaceAll("\$MAIN_ARTIST", mainArtist)
|
||||||
|
.replaceAll("\$TITLE", title)
|
||||||
|
.replaceAll("\$FEATURED_ARTISTS", featuredArtists);
|
||||||
|
logger.v("[Youtube Search Term] $queryString");
|
||||||
|
|
||||||
SearchList videos = await youtube.search.getVideos(queryString);
|
SearchList videos = await youtube.search.getVideos(queryString);
|
||||||
|
|
||||||
@ -33,5 +46,6 @@ Future<Track> toYoutubeTrack(YoutubeExplode youtube, Track track) async {
|
|||||||
.url
|
.url
|
||||||
.toString();
|
.toString();
|
||||||
track.href = ytVideo.url;
|
track.href = ytVideo.url;
|
||||||
|
logger.v("[YouTube Matched Track] ${ytVideo.title} - ${track.href}");
|
||||||
return track;
|
return track;
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
abstract class LocalStorageKeys {
|
abstract class LocalStorageKeys {
|
||||||
static String saveTrackLyrics = 'save_track_lyrics';
|
static String saveTrackLyrics = 'save_track_lyrics';
|
||||||
static String recommendationMarket = 'recommendation_market';
|
static String recommendationMarket = 'recommendation_market';
|
||||||
|
static String ytSearchFormate = 'youtube_search_format';
|
||||||
|
|
||||||
static String clientId = 'client_id';
|
static String clientId = 'client_id';
|
||||||
static String clientSecret = 'client_secret';
|
static String clientSecret = 'client_secret';
|
||||||
static String accessToken = 'access_token';
|
static String accessToken = 'access_token';
|
||||||
|
@ -11,6 +11,7 @@ import 'package:spotube/helpers/image-to-url-string.dart';
|
|||||||
import 'package:spotube/helpers/search-youtube.dart';
|
import 'package:spotube/helpers/search-youtube.dart';
|
||||||
import 'package:spotube/models/Logger.dart';
|
import 'package:spotube/models/Logger.dart';
|
||||||
import 'package:spotube/provider/AudioPlayer.dart';
|
import 'package:spotube/provider/AudioPlayer.dart';
|
||||||
|
import 'package:spotube/provider/UserPreferences.dart';
|
||||||
import 'package:spotube/provider/YouTube.dart';
|
import 'package:spotube/provider/YouTube.dart';
|
||||||
import 'package:youtube_explode_dart/youtube_explode_dart.dart';
|
import 'package:youtube_explode_dart/youtube_explode_dart.dart';
|
||||||
|
|
||||||
@ -48,6 +49,7 @@ class CurrentPlaylist {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class Playback extends ChangeNotifier {
|
class Playback extends ChangeNotifier {
|
||||||
|
ChangeNotifierProviderRef<Playback> ref;
|
||||||
AudioSource? _currentAudioSource;
|
AudioSource? _currentAudioSource;
|
||||||
final _logger = getLogger(Playback);
|
final _logger = getLogger(Playback);
|
||||||
CurrentPlaylist? _currentPlaylist;
|
CurrentPlaylist? _currentPlaylist;
|
||||||
@ -76,6 +78,7 @@ class Playback extends ChangeNotifier {
|
|||||||
Playback({
|
Playback({
|
||||||
required this.player,
|
required this.player,
|
||||||
required this.youtube,
|
required this.youtube,
|
||||||
|
required this.ref,
|
||||||
CurrentPlaylist? currentPlaylist,
|
CurrentPlaylist? currentPlaylist,
|
||||||
Track? currentTrack,
|
Track? currentTrack,
|
||||||
}) : _currentPlaylist = currentPlaylist,
|
}) : _currentPlaylist = currentPlaylist,
|
||||||
@ -270,7 +273,12 @@ class Playback extends ChangeNotifier {
|
|||||||
notifyListeners();
|
notifyListeners();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
final ytTrack = await toYoutubeTrack(youtube, track);
|
final preferences = ref.read(userPreferencesProvider);
|
||||||
|
final ytTrack = await toYoutubeTrack(
|
||||||
|
youtube,
|
||||||
|
track,
|
||||||
|
preferences.ytSearchFormat,
|
||||||
|
);
|
||||||
if (setTrackUriById(track.id!, ytTrack.uri!)) {
|
if (setTrackUriById(track.id!, ytTrack.uri!)) {
|
||||||
_currentAudioSource =
|
_currentAudioSource =
|
||||||
AudioSource.uri(Uri.parse(ytTrack.uri!), tag: tag);
|
AudioSource.uri(Uri.parse(ytTrack.uri!), tag: tag);
|
||||||
@ -294,5 +302,9 @@ class Playback extends ChangeNotifier {
|
|||||||
final playbackProvider = ChangeNotifierProvider<Playback>((ref) {
|
final playbackProvider = ChangeNotifierProvider<Playback>((ref) {
|
||||||
final player = ref.watch(audioPlayerProvider);
|
final player = ref.watch(audioPlayerProvider);
|
||||||
final youtube = ref.watch(youtubeProvider);
|
final youtube = ref.watch(youtubeProvider);
|
||||||
return Playback(player: player, youtube: youtube);
|
return Playback(
|
||||||
|
player: player,
|
||||||
|
youtube: youtube,
|
||||||
|
ref: ref,
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
@ -11,6 +11,7 @@ import 'package:spotube/models/generated_secrets.dart';
|
|||||||
|
|
||||||
class UserPreferences extends ChangeNotifier {
|
class UserPreferences extends ChangeNotifier {
|
||||||
ThemeMode themeMode;
|
ThemeMode themeMode;
|
||||||
|
String ytSearchFormat;
|
||||||
String recommendationMarket;
|
String recommendationMarket;
|
||||||
bool saveTrackLyrics;
|
bool saveTrackLyrics;
|
||||||
String geniusAccessToken;
|
String geniusAccessToken;
|
||||||
@ -22,6 +23,7 @@ class UserPreferences extends ChangeNotifier {
|
|||||||
required this.geniusAccessToken,
|
required this.geniusAccessToken,
|
||||||
required this.recommendationMarket,
|
required this.recommendationMarket,
|
||||||
required this.themeMode,
|
required this.themeMode,
|
||||||
|
required this.ytSearchFormat,
|
||||||
this.saveTrackLyrics = false,
|
this.saveTrackLyrics = false,
|
||||||
this.nextTrackHotKey,
|
this.nextTrackHotKey,
|
||||||
this.prevTrackHotKey,
|
this.prevTrackHotKey,
|
||||||
@ -149,6 +151,12 @@ class UserPreferences extends ChangeNotifier {
|
|||||||
);
|
);
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setYtSearchFormat(String format) {
|
||||||
|
ytSearchFormat = format;
|
||||||
|
localStorage?.setString(LocalStorageKeys.ytSearchFormate, format);
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
final userPreferencesProvider = ChangeNotifierProvider(
|
final userPreferencesProvider = ChangeNotifierProvider(
|
||||||
@ -156,5 +164,6 @@ final userPreferencesProvider = ChangeNotifierProvider(
|
|||||||
geniusAccessToken: "",
|
geniusAccessToken: "",
|
||||||
recommendationMarket: 'US',
|
recommendationMarket: 'US',
|
||||||
themeMode: ThemeMode.system,
|
themeMode: ThemeMode.system,
|
||||||
|
ytSearchFormat: "\$MAIN_ARTIST - \$TITLE \$FEATURED_ARTISTS",
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user