commit
4646f484b8
|
@ -13,11 +13,14 @@ class Channel with EquatableMixin {
|
|||
/// Channel title.
|
||||
final String title;
|
||||
|
||||
/// Channel description
|
||||
final String description;
|
||||
|
||||
/// URL of the channel's logo image.
|
||||
final String logoUrl;
|
||||
|
||||
/// Initializes an instance of [Channel]
|
||||
Channel(this.id, this.title, this.logoUrl);
|
||||
Channel(this.id, this.title, this.description, this.logoUrl);
|
||||
|
||||
@override
|
||||
String toString() => 'Channel ($title)';
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import '../extensions/helpers_extension.dart';
|
||||
import '../playlists/playlists.dart';
|
||||
import '../reverse_engineering/responses/channel_about_page.dart';
|
||||
import '../reverse_engineering/responses/channel_upload_page.dart';
|
||||
import '../reverse_engineering/responses/responses.dart';
|
||||
import '../reverse_engineering/youtube_http_client.dart';
|
||||
|
@ -24,8 +25,10 @@ class ChannelClient {
|
|||
Future<Channel> get(dynamic id) async {
|
||||
id = ChannelId.fromString(id);
|
||||
var channelPage = await ChannelPage.get(_httpClient, id.value);
|
||||
var channelAboutPage = await ChannelAboutPage.get(_httpClient, id.value);
|
||||
|
||||
return Channel(id, channelPage.channelTitle, channelPage.channelLogoUrl);
|
||||
return Channel(id, channelPage.channelTitle, channelAboutPage.description,
|
||||
channelPage.channelLogoUrl);
|
||||
}
|
||||
|
||||
/// Gets the metadata associated with the channel of the specified user.
|
||||
|
@ -36,8 +39,11 @@ class ChannelClient {
|
|||
|
||||
var channelPage =
|
||||
await ChannelPage.getByUsername(_httpClient, username.value);
|
||||
var channelAboutPage =
|
||||
await ChannelAboutPage.getByUsername(_httpClient, username.value);
|
||||
|
||||
return Channel(ChannelId(channelPage.channelId), channelPage.channelTitle,
|
||||
channelPage.channelLogoUrl);
|
||||
channelAboutPage.description, channelPage.channelLogoUrl);
|
||||
}
|
||||
|
||||
/// Gets the metadata associated with the channel
|
||||
|
|
|
@ -0,0 +1,113 @@
|
|||
import 'dart:convert';
|
||||
|
||||
import 'package:html/dom.dart';
|
||||
import 'package:html/parser.dart' as parser;
|
||||
|
||||
import '../../exceptions/exceptions.dart';
|
||||
import '../../extensions/helpers_extension.dart';
|
||||
import '../../retry.dart';
|
||||
import '../youtube_http_client.dart';
|
||||
|
||||
///
|
||||
class ChannelAboutPage {
|
||||
final Document _root;
|
||||
|
||||
_InitialData _initialData;
|
||||
|
||||
///
|
||||
_InitialData get initialData =>
|
||||
_initialData ??= _InitialData(json.decode(_matchJson(_extractJson(
|
||||
_root
|
||||
.querySelectorAll('script')
|
||||
.map((e) => e.text)
|
||||
.toList()
|
||||
.firstWhere((e) => e.contains('window["ytInitialData"] =')),
|
||||
'window["ytInitialData"] ='))));
|
||||
|
||||
///
|
||||
bool get isOk => initialData != null;
|
||||
|
||||
///
|
||||
String get description => initialData.description;
|
||||
|
||||
String _extractJson(String html, String separator) {
|
||||
return _matchJson(
|
||||
html.substring(html.indexOf(separator) + separator.length));
|
||||
}
|
||||
|
||||
String _matchJson(String str) {
|
||||
var bracketCount = 0;
|
||||
int lastI;
|
||||
for (var i = 0; i < str.length; i++) {
|
||||
lastI = i;
|
||||
if (str[i] == '{') {
|
||||
bracketCount++;
|
||||
} else if (str[i] == '}') {
|
||||
bracketCount--;
|
||||
} else if (str[i] == ';') {
|
||||
if (bracketCount == 0) {
|
||||
return str.substring(0, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
return str.substring(0, lastI + 1);
|
||||
}
|
||||
|
||||
///
|
||||
ChannelAboutPage(this._root);
|
||||
|
||||
///
|
||||
ChannelAboutPage.parse(String raw) : _root = parser.parse(raw);
|
||||
|
||||
///
|
||||
static Future<ChannelAboutPage> get(YoutubeHttpClient httpClient, String id) {
|
||||
var url = 'https://www.youtube.com/channel/$id/about?hl=en';
|
||||
|
||||
return retry(() async {
|
||||
var raw = await httpClient.getString(url);
|
||||
var result = ChannelAboutPage.parse(raw);
|
||||
|
||||
if (!result.isOk) {
|
||||
throw TransientFailureException('Channel about page is broken');
|
||||
}
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
///
|
||||
static Future<ChannelAboutPage> getByUsername(
|
||||
YoutubeHttpClient httpClient, String username) {
|
||||
var url = 'https://www.youtube.com/user/$username/about?hl=en';
|
||||
|
||||
return retry(() async {
|
||||
var raw = await httpClient.getString(url);
|
||||
var result = ChannelAboutPage.parse(raw);
|
||||
|
||||
if (!result.isOk) {
|
||||
throw TransientFailureException('Channel about page is broken');
|
||||
}
|
||||
return result;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
class _InitialData {
|
||||
// Json parsed map
|
||||
final Map<String, dynamic> root;
|
||||
|
||||
_InitialData(this.root);
|
||||
|
||||
/* Cache results */
|
||||
|
||||
String _description;
|
||||
|
||||
Map<String, dynamic> getDescriptionContext(Map<String, dynamic> root) {
|
||||
if (root['metadata'] != null) {
|
||||
return root['metadata']['channelMetadataRenderer'];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
String get description => _description ??=
|
||||
getDescriptionContext(root)?.getValue('description') ?? '';
|
||||
}
|
Loading…
Reference in New Issue