mirror of
https://github.com/gokadzev/Musify.git
synced 2026-03-13 15:20:46 +08:00
refactor: move playlist validators into utils
This commit is contained in:
@@ -36,6 +36,7 @@ import 'package:musify/services/lyrics_manager.dart';
|
||||
import 'package:musify/services/settings_manager.dart';
|
||||
import 'package:musify/utilities/flutter_toast.dart';
|
||||
import 'package:musify/utilities/formatter.dart';
|
||||
import 'package:musify/utilities/utils.dart';
|
||||
import 'package:youtube_explode_dart/youtube_explode_dart.dart';
|
||||
|
||||
final _yt = YoutubeExplode();
|
||||
@@ -200,29 +201,11 @@ Future<List<dynamic>> getUserPlaylists() async {
|
||||
return playlistsByUser;
|
||||
}
|
||||
|
||||
bool youtubeValidate(String url) {
|
||||
final regExp = RegExp(
|
||||
r'^(https?:\/\/)?(www\.)?(youtube\.com|youtu\.be)\/.*(list=([a-zA-Z0-9_-]+)).*$',
|
||||
);
|
||||
return regExp.hasMatch(url);
|
||||
}
|
||||
|
||||
String? youtubePlaylistParser(String url) {
|
||||
if (!youtubeValidate(url)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final regExp = RegExp('[&?]list=([a-zA-Z0-9_-]+)');
|
||||
final match = regExp.firstMatch(url);
|
||||
|
||||
return match?.group(1);
|
||||
}
|
||||
|
||||
Future<String> addUserPlaylist(String input, BuildContext context) async {
|
||||
String? playlistId = input;
|
||||
|
||||
if (input.startsWith('http://') || input.startsWith('https://')) {
|
||||
playlistId = youtubePlaylistParser(input);
|
||||
playlistId = extractYoutubePlaylistId(input);
|
||||
|
||||
if (playlistId == null) {
|
||||
return '${context.l10n!.notYTlist}!';
|
||||
|
||||
@@ -47,3 +47,23 @@ Locale getLocaleFromLanguageCode(String? languageCode) {
|
||||
// Default fallback
|
||||
return const Locale('en');
|
||||
}
|
||||
|
||||
/// Validates if a URL is a YouTube playlist URL
|
||||
bool isYoutubePlaylistUrl(String url) {
|
||||
final playlistRegExp = RegExp(
|
||||
r'^(https?:\/\/)?(www\.)?(youtube\.com|youtu\.be)\/.*(list=([a-zA-Z0-9_-]+)).*$',
|
||||
);
|
||||
return playlistRegExp.hasMatch(url);
|
||||
}
|
||||
|
||||
/// Extracts the playlist ID from a YouTube playlist URL
|
||||
String? extractYoutubePlaylistId(String url) {
|
||||
if (!isYoutubePlaylistUrl(url)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final playlistIdRegExp = RegExp('[&?]list=([a-zA-Z0-9_-]+)');
|
||||
final match = playlistIdRegExp.firstMatch(url);
|
||||
|
||||
return match?.group(1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user