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

58 lines
1.6 KiB
Dart
Raw Normal View History

2020-04-18 23:22:13 +02:00
import 'package:html/dom.dart';
import 'package:html/parser.dart' as parser;
2020-06-03 23:02:21 +02:00
import '../../exceptions/exceptions.dart';
2020-06-05 16:17:08 +02:00
import '../../extensions/helpers_extension.dart';
2020-06-03 23:02:21 +02:00
import '../../retry.dart';
import '../youtube_http_client.dart';
2020-04-18 23:22:13 +02:00
class ChannelPage {
final Document _root;
bool get isOk => _root.querySelector('meta[property="og:url"]') != null;
2020-05-31 23:36:23 +02:00
String get channelUrl =>
_root.querySelector('meta[property="og:url"]')?.attributes['content'];
2020-04-18 23:22:13 +02:00
2020-06-05 16:17:08 +02:00
String get channelId => channelUrl.substringAfter('channel/');
2020-04-18 23:22:13 +02:00
2020-05-31 23:36:23 +02:00
String get channelTitle =>
_root.querySelector('meta[property="og:title"]')?.attributes['content'];
2020-04-18 23:22:13 +02:00
2020-05-31 23:36:23 +02:00
String get channelLogoUrl =>
_root.querySelector('meta[property="og:image"]')?.attributes['content'];
2020-04-18 23:22:13 +02:00
ChannelPage(this._root);
ChannelPage.parse(String raw) : _root = parser.parse(raw);
2020-05-31 23:36:23 +02:00
static Future<ChannelPage> get(YoutubeHttpClient httpClient, String id) {
var url = 'https://www.youtube.com/channel/$id?hl=en';
return retry(() async {
var raw = await httpClient.getString(url);
var result = ChannelPage.parse(raw);
2020-04-18 23:22:13 +02:00
2020-05-31 23:36:23 +02:00
if (!result.isOk) {
throw TransientFailureException('Channel page is broken');
}
return result;
});
2020-04-18 23:22:13 +02:00
}
2020-06-03 23:02:21 +02:00
static Future<ChannelPage> getByUsername(
YoutubeHttpClient httpClient, String username) {
2020-05-31 23:36:23 +02:00
var url = 'https://www.youtube.com/user/$username?hl=en';
return retry(() async {
var raw = await httpClient.getString(url);
var result = ChannelPage.parse(raw);
if (!result.isOk) {
throw TransientFailureException('Channel page is broken');
}
return result;
});
2020-04-18 23:22:13 +02:00
}
}