youtube_explode/lib/src/videos/video_id.dart

96 lines
2.7 KiB
Dart
Raw Normal View History

import 'package:freezed_annotation/freezed_annotation.dart';
2020-06-03 13:18:37 +02:00
2020-06-03 23:02:21 +02:00
import '../extensions/helpers_extension.dart';
2020-06-03 13:18:37 +02:00
part 'video_id.freezed.dart';
2020-06-03 13:18:37 +02:00
/// Encapsulates a valid YouTube video ID.
@freezed
class VideoId with _$VideoId {
2020-06-03 13:18:37 +02:00
static final _regMatchExp = RegExp(r'youtube\..+?/watch.*?v=(.*?)(?:&|/|$)');
static final _shortMatchExp = RegExp(r'youtu\.be/(.*?)(?:\?|&|/|$)');
static final _embedMatchExp = RegExp(r'youtube\..+?/embed/(.*?)(?:\?|&|/|$)');
2021-10-17 08:42:14 +02:00
static final _shortsMatchExp =
RegExp(r'youtube\..+/shorts/([A-Za-z0-9-_]+$)');
2020-06-03 13:18:37 +02:00
/// Initializes an instance of [VideoId] with a url or video id.
factory VideoId(String idOrUrl) {
final id = parseVideoId(idOrUrl);
if (id == null) {
2020-06-05 16:17:08 +02:00
throw ArgumentError.value(
idOrUrl, 'urlOrUrl', 'Invalid YouTube video ID or URL');
}
return VideoId._internal(id);
2020-06-05 16:17:08 +02:00
}
2020-06-03 13:18:37 +02:00
2021-07-23 12:54:29 +02:00
const VideoId._();
const factory VideoId._internal(
/// ID as string.
String value) = _VideoId;
/// Converts [obj] to a [VideoId] by calling .toString on that object.
/// If it is already a [VideoId], [obj] is returned
factory VideoId.fromString(dynamic obj) {
if (obj is VideoId) {
return obj;
}
return VideoId(obj.toString());
}
2020-06-03 13:18:37 +02:00
@override
String toString() => value;
2020-06-03 13:18:37 +02:00
/// Returns true if the given [videoId] is valid.
static bool validateVideoId(String videoId) {
if (videoId.isNullOrWhiteSpace) {
return false;
}
if (videoId.length != 11) {
return false;
}
return !RegExp(r'[^0-9a-zA-Z_\-]').hasMatch(videoId);
}
/// Parses a video id from url or if given a valid id as url returns itself.
/// Returns null if the id couldn't be extracted.
2021-03-04 12:20:00 +01:00
static String? parseVideoId(String url) {
2020-06-03 13:18:37 +02:00
if (url.isNullOrWhiteSpace) {
return null;
}
if (validateVideoId(url)) {
return url;
}
2022-04-30 17:49:34 +02:00
// http://127.0.0.1:8080/www.youtube.com:443/watch?v=yIVRs6YSbOM
2020-06-03 13:18:37 +02:00
var regMatch = _regMatchExp.firstMatch(url)?.group(1);
2021-03-04 12:20:00 +01:00
if (!regMatch.isNullOrWhiteSpace && validateVideoId(regMatch!)) {
2020-06-03 13:18:37 +02:00
return regMatch;
}
// https://youtu.be/yIVRs6YSbOM
var shortMatch = _shortMatchExp.firstMatch(url)?.group(1);
2021-03-04 12:20:00 +01:00
if (!shortMatch.isNullOrWhiteSpace && validateVideoId(shortMatch!)) {
2020-06-03 13:18:37 +02:00
return shortMatch;
}
2022-04-30 17:49:34 +02:00
// http://127.0.0.1:8080/www.youtube.com:443/embed/yIVRs6YSbOM
2020-06-03 13:18:37 +02:00
var embedMatch = _embedMatchExp.firstMatch(url)?.group(1);
2021-03-04 12:20:00 +01:00
if (!embedMatch.isNullOrWhiteSpace && validateVideoId(embedMatch!)) {
2020-06-03 13:18:37 +02:00
return embedMatch;
}
2021-10-17 08:42:14 +02:00
2022-04-30 17:49:34 +02:00
// http://127.0.0.1:8080/www.youtube.com:443/shorts/yIVRs6YSbOM
2021-10-17 08:42:14 +02:00
var shortsMatch = _shortsMatchExp.firstMatch(url)?.group(1);
if (!shortsMatch.isNullOrWhiteSpace && validateVideoId(shortsMatch!)) {
return shortsMatch;
}
2020-06-03 13:18:37 +02:00
return null;
}
}