youtube_explode/lib/src/channels/channel_client.dart

146 lines
5.0 KiB
Dart
Raw Normal View History

import '../common/common.dart';
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';
2021-07-21 02:06:02 +02:00
import '../reverse_engineering/pages/channel_about_page.dart';
2021-09-28 16:49:38 +02:00
import '../reverse_engineering/pages/channel_page.dart';
2021-07-21 02:06:02 +02:00
import '../reverse_engineering/pages/channel_upload_page.dart';
2021-09-28 16:49:38 +02:00
import '../reverse_engineering/pages/watch_page.dart';
2020-06-03 23:02:21 +02:00
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 'channels.dart';
2020-06-03 13:18:37 +02:00
/// 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]
const ChannelClient(this._httpClient);
2020-06-03 13:18:37 +02:00
/// 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);
2021-07-14 22:36:25 +02:00
return Channel(id, channelPage.channelTitle, channelPage.channelLogoUrl,
channelPage.channelBannerUrl, channelPage.subscribersCount);
2020-06-03 13:18:37 +02:00
}
/// 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 =
2022-02-03 12:06:09 +01:00
await ChannelPage.getByUsername(_httpClient, (username as Username).value);
return Channel(
ChannelId(channelPage.channelId),
channelPage.channelTitle,
channelPage.channelLogoUrl,
channelPage.channelBannerUrl,
channelPage.subscribersCount);
2020-06-03 13:18:37 +02:00
}
2020-10-17 14:45:42 +02:00
/// Gets the info found on a YouTube Channel About page.
/// [id] must be either a [ChannelId] or a string
/// which is parsed to a [ChannelId]
Future<ChannelAbout> getAboutPage(dynamic channelId) async {
channelId = ChannelId.fromString(channelId);
2020-10-17 14:45:42 +02:00
final aboutPage = await ChannelAboutPage.get(_httpClient, channelId.value);
2020-10-17 14:45:42 +02:00
return ChannelAbout(
2021-07-21 02:06:02 +02:00
aboutPage.description,
aboutPage.viewCount,
aboutPage.joinDate,
aboutPage.title,
2020-10-17 14:50:58 +02:00
[
2021-07-21 02:06:02 +02:00
for (var e in aboutPage.avatar)
Thumbnail(Uri.parse(e['url']), e['height'], e['width'])
2020-10-17 14:50:58 +02:00
],
2021-07-21 02:06:02 +02:00
aboutPage.country,
aboutPage.channelLinks);
2020-10-17 14:45:42 +02:00
}
/// Gets the info found on a YouTube Channel About page.
/// [username] must be either a [Username] or a string
/// which is parsed to a [Username]
Future<ChannelAbout> getAboutPageByUsername(dynamic username) async {
username = Username.fromString(username);
2021-08-31 18:06:34 +02:00
var page =
2020-10-17 14:45:42 +02:00
await ChannelAboutPage.getByUsername(_httpClient, username.value);
2020-10-17 14:45:42 +02:00
return ChannelAbout(
2021-08-31 18:06:34 +02:00
page.description,
page.viewCount,
page.joinDate,
page.title,
2020-10-17 14:50:58 +02:00
[
2021-08-31 18:06:34 +02:00
for (var e in page.avatar)
2021-03-11 14:20:10 +01:00
Thumbnail(Uri.parse(e['url']), e['height'], e['width'])
2020-10-17 14:50:58 +02:00
],
2021-08-31 18:06:34 +02:00
page.country,
page.channelLinks);
2020-10-17 14:45:42 +02:00
}
2020-06-03 13:18:37 +02:00
/// 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);
var videoInfoResponse = await WatchPage.get(_httpClient, videoId.value);
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-07-12 18:24:22 +02:00
/// If you want a full list of uploads see [getUploadsFromPage]
Stream<Video> getUploads(dynamic channelId) {
channelId = ChannelId.fromString(channelId);
2020-07-12 18:31:16 +02:00
var playlistId = 'UU${(channelId.value as String).substringAfter('UC')}';
2020-06-05 16:17:08 +02:00
return PlaylistClient(_httpClient).getVideos(PlaylistId(playlistId));
2020-06-03 13:18:37 +02:00
}
2020-07-12 18:24:22 +02:00
/// Enumerates videos uploaded by the specified channel.
/// This fetches thru all the uploads pages of the channel so it is
/// recommended to use _.take_ (or any other method) to limit the
/// search result. Every page has 30 results.
///
/// Note that this endpoint provides less info about each video
/// (only the Title and VideoId).
2021-04-30 23:49:49 +02:00
Future<ChannelUploadsList> getUploadsFromPage(dynamic channelId,
[VideoSorting videoSorting = VideoSorting.newest]) async {
2020-07-12 18:24:22 +02:00
channelId = ChannelId.fromString(channelId);
2021-04-30 23:49:49 +02:00
final page = await ChannelUploadPage.get(
2021-03-11 14:20:10 +01:00
_httpClient, (channelId as ChannelId).value, videoSorting.code);
2021-04-30 23:49:49 +02:00
final channel = await get(channelId);
return ChannelUploadsList(
2021-07-21 02:06:02 +02:00
page.uploads
2021-04-30 23:49:49 +02:00
.map((e) => Video(
e.videoId,
e.videoTitle,
channel.title,
channelId,
e.videoUploadDate.toDateTime(),
null,
'',
e.videoDuration,
ThumbnailSet(e.videoId.value),
null,
Engagement(e.videoViews, null, null),
false))
.toList(),
channel.title,
channelId,
page,
_httpClient);
2020-07-12 18:24:22 +02:00
}
2020-06-03 13:18:37 +02:00
}