youtube_explode/lib/src/reverse_engineering/responses/player_response.dart

187 lines
5.0 KiB
Dart
Raw Normal View History

2020-05-31 23:36:23 +02:00
import 'dart:convert';
import 'package:http_parser/http_parser.dart';
2020-06-05 16:17:08 +02:00
import '../../extensions/helpers_extension.dart';
import 'stream_info_provider.dart';
2020-05-31 23:36:23 +02:00
class PlayerResponse {
// Json parsed map
final Map<String, dynamic> _root;
PlayerResponse(this._root);
String get playabilityStatus => _root['playabilityStatus']['status'];
2020-06-05 16:17:08 +02:00
bool get isVideoAvailable => playabilityStatus.toLowerCase() != 'error';
2020-05-31 23:36:23 +02:00
2020-06-05 16:17:08 +02:00
bool get isVideoPlayable => playabilityStatus.toLowerCase() == 'ok';
2020-05-31 23:36:23 +02:00
String get videoTitle => _root['videoDetails']['title'];
String get videoAuthor => _root['videoDetails']['author'];
2020-06-03 23:02:21 +02:00
DateTime get videoUploadDate => DateTime.parse(
_root['microformat']['playerMicroformatRenderer']['uploadDate']);
2020-05-31 23:36:23 +02:00
String get videoChannelId => _root['videoDetails']['channelId'];
Duration get videoDuration =>
2020-06-03 23:02:21 +02:00
Duration(seconds: int.parse(_root['videoDetails']['lengthSeconds']));
2020-05-31 23:36:23 +02:00
Iterable<String> get videoKeywords =>
2020-06-05 16:17:08 +02:00
_root['videoDetails']['keywords']?.cast<String>() ?? const [];
2020-05-31 23:36:23 +02:00
String get videoDescription => _root['videoDetails']['shortDescription'];
int get videoViewCount => int.parse(_root['videoDetails']['viewCount']);
// Can be null
String get previewVideoId =>
_root
.get('playabilityStatus')
?.get('errorScreen')
?.get('playerLegacyDesktopYpcTrailerRenderer')
2020-06-05 16:17:08 +02:00
?.getValue('trailerVideoId') ??
2020-05-31 23:36:23 +02:00
Uri.splitQueryString(_root
.get('playabilityStatus')
?.get('errorScreen')
?.get('')
?.get('ypcTrailerRenderer')
2020-06-05 16:17:08 +02:00
?.getValue('playerVars') ??
2020-05-31 23:36:23 +02:00
'')['video_id'];
2020-06-05 16:17:08 +02:00
bool get isLive => _root.get('videoDetails')?.getValue('isLive') ?? false;
2020-05-31 23:36:23 +02:00
2020-06-03 13:18:37 +02:00
// Can be null
String get hlsManifestUrl =>
2020-06-05 16:17:08 +02:00
_root.get('streamingData')?.getValue('hlsManifestUrl');
2020-06-03 13:18:37 +02:00
2020-05-31 23:36:23 +02:00
// Can be null
String get dashManifestUrl =>
2020-06-05 16:17:08 +02:00
_root.get('streamingData')?.getValue('dashManifestUrl');
2020-05-31 23:36:23 +02:00
Iterable<StreamInfoProvider> get muxedStreams =>
2020-06-05 16:17:08 +02:00
_root
?.get('streamingData')
?.getValue('formats')
?.map((e) => _StreamInfo(e))
?.cast<StreamInfoProvider>() ??
const <StreamInfoProvider>[];
2020-05-31 23:36:23 +02:00
Iterable<StreamInfoProvider> get adaptiveStreams =>
_root
?.get('streamingData')
2020-06-05 16:17:08 +02:00
?.getValue('adaptiveFormats')
?.map((e) => _StreamInfo(e))
?.cast<StreamInfoProvider>() ??
const <StreamInfoProvider>[];
2020-05-31 23:36:23 +02:00
Iterable<StreamInfoProvider> get streams =>
[...muxedStreams, ...adaptiveStreams];
Iterable<ClosedCaptionTrack> get closedCaptionTrack =>
_root
.get('captions')
?.get('playerCaptionsTracklistRenderer')
2020-06-05 16:17:08 +02:00
?.getValue('captionTracks')
?.map((e) => ClosedCaptionTrack(e))
?.cast<ClosedCaptionTrack>() ??
2020-05-31 23:36:23 +02:00
const [];
2020-06-03 13:18:37 +02:00
String getVideoPlayabilityError() =>
2020-06-05 16:17:08 +02:00
_root.get('playabilityStatus')?.getValue('reason');
2020-06-03 13:18:37 +02:00
2020-05-31 23:36:23 +02:00
PlayerResponse.parse(String raw) : _root = json.decode(raw);
}
class ClosedCaptionTrack {
// Json parsed map
final Map<String, dynamic> _root;
ClosedCaptionTrack(this._root);
String get url => _root['baseUrl'];
2020-06-03 23:02:21 +02:00
String get languageCode => _root['languageCode'];
String get languageName => _root['name']['simpleText'];
2020-05-31 23:36:23 +02:00
2020-06-03 23:02:21 +02:00
bool get autoGenerated => _root['vssId'].toLowerCase().startsWith("a.");
2020-05-31 23:36:23 +02:00
}
class _StreamInfo extends StreamInfoProvider {
// Json parsed map
final Map<String, dynamic> _root;
_StreamInfo(this._root);
@override
int get bitrate => _root['bitrate'];
@override
String get container => mimeType.subtype;
2020-06-05 16:17:08 +02:00
static final _contentLenExp = RegExp(r'[\?&]clen=(\d+)');
2020-05-31 23:36:23 +02:00
@override
int get contentLength =>
2020-06-05 16:17:08 +02:00
int.tryParse(_root['contentLength'] ?? '') ??
_contentLenExp.firstMatch(url)?.group(1);
2020-05-31 23:36:23 +02:00
@override
2020-06-05 16:17:08 +02:00
int get framerate => _root['fps'];
2020-05-31 23:36:23 +02:00
@override
2020-06-05 16:17:08 +02:00
String get signature =>
Uri.splitQueryString(_root['signatureCipher'] ?? '')['s'];
2020-05-31 23:36:23 +02:00
@override
String get signatureParameter =>
2020-06-05 16:17:08 +02:00
Uri.splitQueryString(_root['cipher'] ?? '')['sp'] ??
Uri.splitQueryString(_root['signatureCipher'] ?? '')['sp'];
2020-05-31 23:36:23 +02:00
@override
2020-06-05 16:17:08 +02:00
int get tag => _root['itag'];
2020-05-31 23:36:23 +02:00
@override
2020-06-05 16:17:08 +02:00
String get url => _getUrl();
String _getUrl() {
var url = _root['url'];
url ??= Uri.splitQueryString(_root['cipher'] ?? '')['url'];
url ??= Uri.splitQueryString(_root['signatureCipher'] ?? '')['url'];
return url;
}
2020-05-31 23:36:23 +02:00
@override
2020-06-05 16:17:08 +02:00
String get videoCodec =>
isAudioOnly ? null : codecs.split(',').first.trim().nullIfWhitespace;
2020-05-31 23:36:23 +02:00
@override
int get videoHeight => _root['height'];
@override
String get videoQualityLabel => _root['qualityLabel'];
@override
int get videoWidth => _root['width'];
2020-06-05 16:17:08 +02:00
bool get isAudioOnly => mimeType.type == 'audio';
2020-05-31 23:36:23 +02:00
MediaType get mimeType => MediaType.parse(_root['mimeType']);
String get codecs => mimeType?.parameters['codecs']?.toLowerCase();
@override
2020-06-05 16:17:08 +02:00
String get audioCodec =>
2020-06-05 20:08:04 +02:00
isAudioOnly ? codecs : _getAudioCodec(codecs.split(','))?.trim();
String _getAudioCodec(List<String> codecs) {
if (codecs.length == 1) {
return null;
}
return codecs.last;
}
2020-05-31 23:36:23 +02:00
}