Use proxy for CORS

This commit is contained in:
poka 2022-04-30 17:49:34 +02:00
parent 1795df619d
commit b7dfcc3fe6
19 changed files with 40 additions and 30 deletions

View File

@ -53,7 +53,7 @@ import 'package:youtube_explode_dart/youtube_explode_dart.dart';
```dart ```dart
// You can provide either a video ID or URL as String or an instance of `VideoId`. // You can provide either a video ID or URL as String or an instance of `VideoId`.
var video = yt.videos.get('https://youtube.com/watch?v=Dpp1sIL1m5Q'); // Returns a Video instance. var video = yt.videos.get('http://127.0.0.1:8080/youtube.com:443/watch?v=Dpp1sIL1m5Q'); // Returns a Video instance.
var title = video.title; // "Scamazon Prime" var title = video.title; // "Scamazon Prime"
var author = video.author; // "Jim Browning" var author = video.author; // "Jim Browning"

View File

@ -26,7 +26,7 @@ class Channel with _$Channel {
) = _Channel; ) = _Channel;
/// Channel URL. /// Channel URL.
String get url => 'https://www.youtube.com/channel/$id'; String get url => 'http://127.0.0.1:8080/www.youtube.com:443/channel/$id';
const Channel._(); const Channel._();
} }

View File

@ -37,5 +37,6 @@ class Playlist with _$Playlist {
const Playlist._(); const Playlist._();
/// Playlist URL. /// Playlist URL.
String get url => 'https://www.youtube.com/playlist?list=$id'; String get url =>
'http://127.0.0.1:8080/www.youtube.com:443/playlist?list=$id';
} }

View File

@ -58,7 +58,8 @@ class EmbeddedPlayerClient {
'videoId': videoId 'videoId': videoId
}; };
final url = Uri.parse('https://www.youtube.com/youtubei/v1/player'); final url = Uri.parse(
'http://127.0.0.1:8080/www.youtube.com:443/youtubei/v1/player');
return retry(httpClient, () async { return retry(httpClient, () async {
final raw = await httpClient.post(url, final raw = await httpClient.post(url,

View File

@ -36,7 +36,8 @@ class ChannelAboutPage extends YoutubePage<_InitialData> {
/// ///
static Future<ChannelAboutPage> get(YoutubeHttpClient httpClient, String id) { static Future<ChannelAboutPage> get(YoutubeHttpClient httpClient, String id) {
var url = 'https://www.youtube.com/channel/$id/about?hl=en'; var url =
'http://127.0.0.1:8080/www.youtube.com:443/channel/$id/about?hl=en';
return retry(httpClient, () async { return retry(httpClient, () async {
var raw = await httpClient.getString(url); var raw = await httpClient.getString(url);
@ -49,7 +50,8 @@ class ChannelAboutPage extends YoutubePage<_InitialData> {
/// ///
static Future<ChannelAboutPage> getByUsername( static Future<ChannelAboutPage> getByUsername(
YoutubeHttpClient httpClient, String username) { YoutubeHttpClient httpClient, String username) {
var url = 'https://www.youtube.com/user/$username/about?hl=en'; var url =
'http://127.0.0.1:8080/www.youtube.com:443/user/$username/about?hl=en';
return retry(httpClient, () async { return retry(httpClient, () async {
var raw = await httpClient.getString(url); var raw = await httpClient.getString(url);

View File

@ -40,7 +40,7 @@ class ChannelPage extends YoutubePage<_InitialData> {
/// ///
static Future<ChannelPage> get(YoutubeHttpClient httpClient, String id) { static Future<ChannelPage> get(YoutubeHttpClient httpClient, String id) {
var url = 'https://www.youtube.com/channel/$id?hl=en'; var url = 'http://127.0.0.1:8080/www.youtube.com:443/channel/$id?hl=en';
return retry(httpClient, () async { return retry(httpClient, () async {
var raw = await httpClient.getString(url); var raw = await httpClient.getString(url);
@ -56,7 +56,7 @@ class ChannelPage extends YoutubePage<_InitialData> {
/// ///
static Future<ChannelPage> getByUsername( static Future<ChannelPage> getByUsername(
YoutubeHttpClient httpClient, String username) { YoutubeHttpClient httpClient, String username) {
var url = 'https://www.youtube.com/user/$username?hl=en'; var url = 'http://127.0.0.1:8080/www.youtube.com:443/user/$username?hl=en';
return retry(httpClient, () async { return retry(httpClient, () async {
try { try {
@ -71,7 +71,7 @@ class ChannelPage extends YoutubePage<_InitialData> {
if (e.statusCode != 404) { if (e.statusCode != 404) {
rethrow; rethrow;
} }
url = 'https://www.youtube.com/c/$username?hl=en'; url = 'http://127.0.0.1:8080/www.youtube.com:443/c/$username?hl=en';
} }
throw FatalFailureException('', 0); throw FatalFailureException('', 0);
}); });

View File

@ -35,7 +35,7 @@ class ChannelUploadPage extends YoutubePage<_InitialData> {
static Future<ChannelUploadPage> get( static Future<ChannelUploadPage> get(
YoutubeHttpClient httpClient, String channelId, String sorting) { YoutubeHttpClient httpClient, String channelId, String sorting) {
var url = var url =
'https://www.youtube.com/channel/$channelId/videos?view=0&sort=$sorting&flow=grid'; 'http://127.0.0.1:8080/www.youtube.com:443/channel/$channelId/videos?view=0&sort=$sorting&flow=grid';
return retry(httpClient, () async { return retry(httpClient, () async {
var raw = await httpClient.getString(url); var raw = await httpClient.getString(url);
return ChannelUploadPage.parse(raw, channelId); return ChannelUploadPage.parse(raw, channelId);

View File

@ -29,7 +29,7 @@ class EmbedPage {
if (url == null) { if (url == null) {
return null; return null;
} }
return 'https://youtube.com$url'; return 'http://127.0.0.1:8080/youtube.com:443$url';
} }
/// ///
@ -69,7 +69,7 @@ class EmbedPage {
/// ///
static Future<EmbedPage> get(YoutubeHttpClient httpClient, String videoId) { static Future<EmbedPage> get(YoutubeHttpClient httpClient, String videoId) {
var url = 'https://youtube.com/embed/$videoId?hl=en'; var url = 'http://127.0.0.1:8080/youtube.com:443/embed/$videoId?hl=en';
// final url = 'http://localhost:8080/embed/$videoId?hl=en'; // final url = 'http://localhost:8080/embed/$videoId?hl=en';
return retry(httpClient, () async { return retry(httpClient, () async {
var raw = await httpClient.getString(url); var raw = await httpClient.getString(url);
@ -87,5 +87,6 @@ class EmbedPlayerConfig implements PlayerConfigBase {
EmbedPlayerConfig(this.root); EmbedPlayerConfig(this.root);
@override @override
String get sourceUrl => 'https://youtube.com${root['assets']['js']}'; String get sourceUrl =>
'http://127.0.0.1:8080/youtube.com:443${root['assets']['js']}';
} }

View File

@ -46,7 +46,8 @@ class PlaylistPage extends YoutubePage<_InitialData> {
YoutubeHttpClient httpClient, YoutubeHttpClient httpClient,
String id, String id,
) async { ) async {
var url = 'https://www.youtube.com/playlist?list=$id&hl=en&persist_hl=1'; var url =
'http://127.0.0.1:8080/www.youtube.com:443/playlist?list=$id&hl=en&persist_hl=1';
return retry(httpClient, () async { return retry(httpClient, () async {
var raw = await httpClient.getString(url); var raw = await httpClient.getString(url);
return PlaylistPage.parse(raw, id); return PlaylistPage.parse(raw, id);

View File

@ -39,7 +39,7 @@ class SearchPage extends YoutubePage<_InitialData> {
YoutubeHttpClient httpClient, String queryString, YoutubeHttpClient httpClient, String queryString,
{SearchFilter filter = const SearchFilter('')}) { {SearchFilter filter = const SearchFilter('')}) {
var url = var url =
'https://www.youtube.com/results?search_query=${Uri.encodeQueryComponent(queryString)}&sp=${filter.value}'; 'http://127.0.0.1:8080/www.youtube.com:443/results?search_query=${Uri.encodeQueryComponent(queryString)}&sp=${filter.value}';
return retry(httpClient, () async { return retry(httpClient, () async {
var raw = await httpClient.getString(url); var raw = await httpClient.getString(url);
return SearchPage.parse(raw, queryString); return SearchPage.parse(raw, queryString);

View File

@ -41,7 +41,7 @@ class WatchPage extends YoutubePage<_InitialData> {
if (url == null) { if (url == null) {
return null; return null;
} }
return 'https://youtube.com$url'; return 'http://127.0.0.1:8080/youtube.com:443$url';
} }
/// ///
@ -120,7 +120,8 @@ class WatchPage extends YoutubePage<_InitialData> {
/// ///
static Future<WatchPage> get(YoutubeHttpClient httpClient, String videoId) { static Future<WatchPage> get(YoutubeHttpClient httpClient, String videoId) {
final url = 'https://youtube.com/watch?v=$videoId&bpctr=9999999999&hl=en'; final url =
'http://127.0.0.1:8080/youtube.com:443/watch?v=$videoId&bpctr=9999999999&hl=en';
return retry(httpClient, () async { return retry(httpClient, () async {
var req = await httpClient.get(url, validate: true); var req = await httpClient.get(url, validate: true);
@ -151,7 +152,7 @@ class WatchPlayerConfig implements PlayerConfigBase {
@override @override
late final String sourceUrl = late final String sourceUrl =
'https://youtube.com${root.get('assets')!.getT<String>('js')}'; 'http://127.0.0.1:8080/youtube.com:443${root.get('assets')!.getT<String>('js')}';
/// ///
late final PlayerResponse playerResponse = late final PlayerResponse playerResponse =

View File

@ -231,7 +231,7 @@ class YoutubeHttpClient extends http.BaseClient {
}; };
final url = Uri.parse( final url = Uri.parse(
'https://www.youtube.com/youtubei/v1/$action?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8'); 'http://127.0.0.1:8080/www.youtube.com:443/youtubei/v1/$action?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8');
return retry<JsonMap>(this, () async { return retry<JsonMap>(this, () async {
final raw = await post(url, body: json.encode(body)); final raw = await post(url, body: json.encode(body));

View File

@ -11,7 +11,7 @@ part 'video.freezed.dart';
@freezed @freezed
class Video with _$Video { class Video with _$Video {
/// Video URL. /// Video URL.
String get url => 'https://www.youtube.com/watch?v=$id'; String get url => 'http://127.0.0.1:8080/www.youtube.com:443/watch?v=$id';
/// Returns true if the watch page is available for this video. /// Returns true if the watch page is available for this video.
bool get hasWatchPage => watchPage != null; bool get hasWatchPage => watchPage != null;

View File

@ -67,7 +67,7 @@ class VideoId with _$VideoId {
return url; return url;
} }
// https://www.youtube.com/watch?v=yIVRs6YSbOM // http://127.0.0.1:8080/www.youtube.com:443/watch?v=yIVRs6YSbOM
var regMatch = _regMatchExp.firstMatch(url)?.group(1); var regMatch = _regMatchExp.firstMatch(url)?.group(1);
if (!regMatch.isNullOrWhiteSpace && validateVideoId(regMatch!)) { if (!regMatch.isNullOrWhiteSpace && validateVideoId(regMatch!)) {
return regMatch; return regMatch;
@ -79,13 +79,13 @@ class VideoId with _$VideoId {
return shortMatch; return shortMatch;
} }
// https://www.youtube.com/embed/yIVRs6YSbOM // http://127.0.0.1:8080/www.youtube.com:443/embed/yIVRs6YSbOM
var embedMatch = _embedMatchExp.firstMatch(url)?.group(1); var embedMatch = _embedMatchExp.firstMatch(url)?.group(1);
if (!embedMatch.isNullOrWhiteSpace && validateVideoId(embedMatch!)) { if (!embedMatch.isNullOrWhiteSpace && validateVideoId(embedMatch!)) {
return embedMatch; return embedMatch;
} }
// https://www.youtube.com/shorts/yIVRs6YSbOM // http://127.0.0.1:8080/www.youtube.com:443/shorts/yIVRs6YSbOM
var shortsMatch = _shortsMatchExp.firstMatch(url)?.group(1); var shortsMatch = _shortsMatchExp.firstMatch(url)?.group(1);
if (!shortsMatch.isNullOrWhiteSpace && validateVideoId(shortsMatch!)) { if (!shortsMatch.isNullOrWhiteSpace && validateVideoId(shortsMatch!)) {
return shortsMatch; return shortsMatch;

View File

@ -12,7 +12,7 @@ void main() {
}); });
test('Get a channel about page', () async { test('Get a channel about page', () async {
var channelUrl = 'https://www.youtube.com/user/FavijTV'; var channelUrl = 'http://127.0.0.1:8080/www.youtube.com:443/user/FavijTV';
var channel = await yt!.channels.getAboutPageByUsername(channelUrl); var channel = await yt!.channels.getAboutPageByUsername(channelUrl);
expect(channel.country, 'Italy'); expect(channel.country, 'Italy');
expect(channel.thumbnails, isNotEmpty); expect(channel.thumbnails, isNotEmpty);

View File

@ -12,7 +12,8 @@ void main() {
}); });
test('Get metadata of a channel', () async { test('Get metadata of a channel', () async {
var channelUrl = 'https://www.youtube.com/channel/UCEnBXANsKmyj2r9xVyKoDiQ'; var channelUrl =
'http://127.0.0.1:8080/www.youtube.com:443/channel/UCEnBXANsKmyj2r9xVyKoDiQ';
var channel = await yt!.channels.get(ChannelId(channelUrl)); var channel = await yt!.channels.get(ChannelId(channelUrl));
expect(channel.url, channelUrl); expect(channel.url, channelUrl);
expect(channel.title, 'Tyrrrz'); expect(channel.title, 'Tyrrrz');
@ -48,7 +49,7 @@ void main() {
test('Get the videos of a youtube channel', () async { test('Get the videos of a youtube channel', () async {
var videos = await yt!.channels var videos = await yt!.channels
.getUploads(ChannelId( .getUploads(ChannelId(
'https://www.youtube.com/channel/UCEnBXANsKmyj2r9xVyKoDiQ')) 'http://127.0.0.1:8080/www.youtube.com:443/channel/UCEnBXANsKmyj2r9xVyKoDiQ'))
.toList(); .toList();
expect(videos.length, greaterThanOrEqualTo(75)); expect(videos.length, greaterThanOrEqualTo(75));
}); });

View File

@ -12,7 +12,8 @@ void main() {
}); });
test('Get comments of a video', () async { test('Get comments of a video', () async {
var videoUrl = 'https://www.youtube.com/watch?v=AI7ULzgf8RU'; var videoUrl =
'http://127.0.0.1:8080/www.youtube.com:443/watch?v=AI7ULzgf8RU';
var video = await yt!.videos.get(VideoId(videoUrl)); var video = await yt!.videos.get(VideoId(videoUrl));
var comments = (await yt!.videos.commentsClient.getComments(video))!; var comments = (await yt!.videos.commentsClient.getComments(video))!;
expect(comments.length, greaterThanOrEqualTo(1)); expect(comments.length, greaterThanOrEqualTo(1));

View File

@ -13,7 +13,7 @@ void main() {
test('Get metadata of a playlist', () async { test('Get metadata of a playlist', () async {
var playlistUrl = var playlistUrl =
'https://www.youtube.com/playlist?list=PLr-IftNTIujSF-8tlGbZBQyGIT6TCF6Yd'; 'http://127.0.0.1:8080/www.youtube.com:443/playlist?list=PLr-IftNTIujSF-8tlGbZBQyGIT6TCF6Yd';
var playlist = await yt!.playlists.get(PlaylistId(playlistUrl)); var playlist = await yt!.playlists.get(PlaylistId(playlistUrl));
expect(playlist.id.value, 'PLr-IftNTIujSF-8tlGbZBQyGIT6TCF6Yd'); expect(playlist.id.value, 'PLr-IftNTIujSF-8tlGbZBQyGIT6TCF6Yd');
expect(playlist.url, playlistUrl); expect(playlist.url, playlistUrl);
@ -48,7 +48,7 @@ void main() {
test('Get more than 100 videos in a playlist', () async { test('Get more than 100 videos in a playlist', () async {
var videos = await yt!.playlists var videos = await yt!.playlists
.getVideos(PlaylistId( .getVideos(PlaylistId(
'https://www.youtube.com/playlist?list=PLCSusC_jlo14J0uBgFqfHsKu7gc5W2HyM')) 'http://127.0.0.1:8080/www.youtube.com:443/playlist?list=PLCSusC_jlo14J0uBgFqfHsKu7gc5W2HyM'))
.toList(); .toList();
expect(videos.length, greaterThan(100)); expect(videos.length, greaterThan(100));
}); });

View File

@ -12,7 +12,8 @@ void main() {
}); });
test('Get metadata of a video', () async { test('Get metadata of a video', () async {
var videoUrl = 'https://www.youtube.com/watch?v=AI7ULzgf8RU'; var videoUrl =
'http://127.0.0.1:8080/www.youtube.com:443/watch?v=AI7ULzgf8RU';
var video = await yt!.videos.get(VideoId(videoUrl)); var video = await yt!.videos.get(VideoId(videoUrl));
expect(video.id.value, 'AI7ULzgf8RU'); expect(video.id.value, 'AI7ULzgf8RU');
expect(video.url, videoUrl); expect(video.url, videoUrl);