youtube_explode/lib/src/channels/channel_client.dart

58 lines
2.0 KiB
Dart
Raw Normal View History

2020-06-03 23:02:21 +02:00
import '../extensions/helpers_extension.dart';
2020-06-05 16:17:08 +02:00
import '../playlists/playlists.dart';
2020-06-03 23:02:21 +02:00
import '../reverse_engineering/responses/responses.dart';
import '../reverse_engineering/youtube_http_client.dart';
2020-06-05 16:17:08 +02:00
import '../videos/video.dart';
2020-06-03 13:18:37 +02:00
import '../videos/video_id.dart';
import 'channel.dart';
import 'channel_id.dart';
import 'username.dart';
/// Queries related to YouTube channels.
2020-04-18 23:22:13 +02:00
class ChannelClient {
2020-06-03 13:18:37 +02:00
final YoutubeHttpClient _httpClient;
/// Initializes an instance of [ChannelClient]
ChannelClient(this._httpClient);
/// Gets the metadata associated with the specified channel.
2020-06-05 20:08:04 +02:00
/// [id] must be either a [ChannelId] or a string
/// which is parsed to a [ChannelId]
Future<Channel> get(dynamic id) async {
id = ChannelId.fromString(id);
2020-06-03 13:18:37 +02:00
var channelPage = await ChannelPage.get(_httpClient, id.value);
return Channel(id, channelPage.channelTitle, channelPage.channelLogoUrl);
}
/// Gets the metadata associated with the channel of the specified user.
2020-06-05 20:08:04 +02:00
/// [username] must be either a [Username] or a string
/// which is parsed to a [Username]
Future<Channel> getByUsername(dynamic username) async {
username = Username.fromString(username);
2020-06-03 13:18:37 +02:00
var channelPage =
await ChannelPage.getByUsername(_httpClient, username.value);
return Channel(ChannelId(channelPage.channelId), channelPage.channelTitle,
channelPage.channelLogoUrl);
}
/// Gets the metadata associated with the channel
/// that uploaded the specified video.
2020-06-05 20:08:04 +02:00
Future<Channel> getByVideo(dynamic videoId) async {
videoId = VideoId.fromString(videoId);
2020-06-03 13:18:37 +02:00
var videoInfoResponse =
await VideoInfoResponse.get(_httpClient, videoId.value);
2020-06-06 11:28:36 +02:00
var playerResponse = videoInfoResponse.playerResponse;
2020-06-03 13:18:37 +02:00
2020-06-06 11:28:36 +02:00
var channelId = playerResponse.videoChannelId;
2020-06-10 00:08:16 +02:00
return get(ChannelId(channelId));
2020-06-03 13:18:37 +02:00
}
/// Enumerates videos uploaded by the specified channel.
2020-06-05 16:17:08 +02:00
Stream<Video> getUploads(ChannelId id) {
var playlistId = 'UU${id.value.substringAfter('UC')}';
return PlaylistClient(_httpClient).getVideos(PlaylistId(playlistId));
2020-06-03 13:18:37 +02:00
}
}