youtube_explode/lib/src/reverse_engineering/responses/closed_caption_track_respon...

88 lines
2.0 KiB
Dart
Raw Normal View History

2020-05-31 23:36:23 +02:00
import 'package:xml/xml.dart' as xml;
2020-06-03 23:02:21 +02:00
import '../../retry.dart';
import '../youtube_http_client.dart';
2020-05-31 23:36:23 +02:00
2020-07-16 20:02:54 +02:00
///
2020-05-31 23:36:23 +02:00
class ClosedCaptionTrackResponse {
final xml.XmlDocument _root;
2020-06-22 17:40:57 +02:00
Iterable<ClosedCaption> _closedCaptions;
2020-05-31 23:36:23 +02:00
2020-07-16 20:02:54 +02:00
///
2020-06-22 17:40:57 +02:00
Iterable<ClosedCaption> get closedCaptions => _closedCaptions ??=
_root.findAllElements('p').map((e) => ClosedCaption._(e));
2020-05-31 23:36:23 +02:00
2020-07-16 20:02:54 +02:00
///
2020-06-22 17:40:57 +02:00
ClosedCaptionTrackResponse(this._root);
2020-07-16 20:02:54 +02:00
///
// ignore: deprecated_member_use
2020-05-31 23:36:23 +02:00
ClosedCaptionTrackResponse.parse(String raw) : _root = xml.parse(raw);
2020-07-16 20:02:54 +02:00
///
2020-05-31 23:36:23 +02:00
static Future<ClosedCaptionTrackResponse> get(
YoutubeHttpClient httpClient, String url) {
var formatUrl = _setQueryParameters(url, {'format': '3'});
return retry(() async {
var raw = await httpClient.getString(formatUrl);
return ClosedCaptionTrackResponse.parse(raw);
});
}
static Uri _setQueryParameters(String url, Map<String, String> parameters) {
var uri = Uri.parse(url);
var query = Map<String, String>.from(uri.queryParameters);
query.addAll(parameters);
return uri.replace(queryParameters: query);
}
}
2020-07-16 20:02:54 +02:00
///
2020-05-31 23:36:23 +02:00
class ClosedCaption {
final xml.XmlElement _root;
2020-06-22 17:40:57 +02:00
Duration _offset;
Duration _duration;
Duration _end;
Iterable<ClosedCaptionPart> _parts;
2020-05-31 23:36:23 +02:00
2020-07-16 20:02:54 +02:00
///
String get text => _root.text;
2020-05-31 23:36:23 +02:00
2020-07-16 20:02:54 +02:00
///
2020-06-22 17:40:57 +02:00
Duration get offset => _offset ??=
2020-05-31 23:36:23 +02:00
Duration(milliseconds: int.parse(_root.getAttribute('t') ?? 0));
2020-07-16 20:02:54 +02:00
///
2020-06-22 17:40:57 +02:00
Duration get duration => _duration ??=
2020-05-31 23:36:23 +02:00
Duration(milliseconds: int.parse(_root.getAttribute('d') ?? 0));
2020-07-16 20:02:54 +02:00
///
2020-06-22 17:40:57 +02:00
Duration get end => _end ??= offset + duration;
2020-05-31 23:36:23 +02:00
2020-07-16 20:02:54 +02:00
///
2020-06-03 23:02:21 +02:00
Iterable<ClosedCaptionPart> getParts() =>
2020-06-22 17:40:57 +02:00
_parts ??= _root.findAllElements('s').map((e) => ClosedCaptionPart._(e));
ClosedCaption._(this._root);
2020-05-31 23:36:23 +02:00
}
2020-07-16 20:02:54 +02:00
///
2020-05-31 23:36:23 +02:00
class ClosedCaptionPart {
final xml.XmlElement _root;
2020-06-22 17:40:57 +02:00
Duration _offset;
2020-05-31 23:36:23 +02:00
2020-07-16 20:02:54 +02:00
///
String get text => _root.text;
2020-05-31 23:36:23 +02:00
2020-07-16 20:02:54 +02:00
///
2020-06-22 17:40:57 +02:00
Duration get offset => _offset ??=
Duration(milliseconds: int.parse(_root.getAttribute('t') ?? '0'));
2020-06-22 17:40:57 +02:00
ClosedCaptionPart._(this._root);
2020-05-31 23:36:23 +02:00
}