Skip to content

Commit

Permalink
Merge pull request #269 from igormidev/master
Browse files Browse the repository at this point in the history
Shorts filter added. Closes #268
  • Loading branch information
Hexer10 authored Jan 12, 2024
2 parents 80523b9 + 08bcff4 commit 6e9daf8
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 13 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 2.1.0
- BREAKING CHANGE:
- In `getUploadsFromPage`: the `videoSorting` parameter is now a named parameter
- Shorts filter possibility added in `getUploadsFromPage`.

## 2.0.4
- Fix issue when parsing dates formatted as "Streamed <q> <unit> ago" due to a leading whitespace. #265

Expand Down
8 changes: 6 additions & 2 deletions lib/src/channels/channel_client.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'video_type.dart';

import '../common/common.dart';
import '../extensions/helpers_extension.dart';
import '../playlists/playlists.dart';
Expand Down Expand Up @@ -137,14 +139,16 @@ class ChannelClient {
///
/// Use .nextPage() to fetch the next batch of videos.
Future<ChannelUploadsList> getUploadsFromPage(
dynamic channelId, [
dynamic channelId, {
VideoSorting videoSorting = VideoSorting.newest,
]) async {
VideoType videoType = VideoType.normal,
}) async {
channelId = ChannelId.fromString(channelId);
final page = await ChannelUploadPage.get(
_httpClient,
(channelId as ChannelId).value,
videoSorting.code,
videoType,
);

final channel = await get(channelId);
Expand Down
1 change: 1 addition & 0 deletions lib/src/channels/channels.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export 'channel_uploads_list.dart';
export 'channel_video.dart';
export 'username.dart';
export 'video_sorting.dart';
export 'video_type.dart';
14 changes: 14 additions & 0 deletions lib/src/channels/video_type.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// The type of the video you want to get.
///
/// Will filter only by the type you want.
enum VideoType {
/// Default horizontal video
normal('videos', 'videoRenderer'),

/// Youtube shorts video
shorts('shorts', 'reelItemRenderer');

final String name;
final String youtubeRenderText;
const VideoType(this.name, this.youtubeRenderText);
}
5 changes: 4 additions & 1 deletion lib/src/extensions/helpers_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,10 @@ extension StringUtility2 on String? {
// Streamed x y ago
parts = parts.skip(1).toList();
}
assert(parts.length == 3);

if (parts.length != 3) {
return null;
}

final qty = int.parse(parts.first);

Expand Down
26 changes: 17 additions & 9 deletions lib/src/reverse_engineering/pages/channel_upload_page.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:collection/collection.dart';
import 'package:html/parser.dart' as parser;
import '../../channels/video_type.dart';

import '../../channels/channel_video.dart';
import '../../exceptions/exceptions.dart';
Expand All @@ -15,10 +16,12 @@ class ChannelUploadPage extends YoutubePage<_InitialData> {
///
final String channelId;

final VideoType type;

late final List<ChannelVideo> uploads = initialData.uploads;

/// InitialData
ChannelUploadPage.id(this.channelId, super.initialData)
ChannelUploadPage.id(this.channelId, this.type, super.initialData)
: super.fromInitialData();

///
Expand All @@ -28,30 +31,33 @@ class ChannelUploadPage extends YoutubePage<_InitialData> {
}

final data = await httpClient.sendContinuation('browse', initialData.token);
return ChannelUploadPage.id(channelId, _InitialData(data));
return ChannelUploadPage.id(channelId, type, _InitialData(data, type));
}

///
static Future<ChannelUploadPage> get(
YoutubeHttpClient httpClient,
String channelId,
String sorting,
VideoType type,
) {
final url =
'https://www.youtube.com/channel/$channelId/videos?view=0&sort=$sorting&flow=grid';
'https://www.youtube.com/channel/$channelId/${type.name}?view=0&sort=$sorting&flow=grid';
return retry(httpClient, () async {
final raw = await httpClient.getString(url);
return ChannelUploadPage.parse(raw, channelId);
return ChannelUploadPage.parse(raw, channelId, type);
});
}

///
ChannelUploadPage.parse(String raw, this.channelId)
: super(parser.parse(raw), (root) => _InitialData(root));
ChannelUploadPage.parse(String raw, this.channelId, this.type)
: super(parser.parse(raw), (root) => _InitialData(root, type));
}

class _InitialData extends InitialData {
_InitialData(super.root);
_InitialData(super.root, this.type);

final VideoType type;

late final JsonMap? continuationContext = getContinuationContext();

Expand Down Expand Up @@ -175,8 +181,10 @@ class _InitialData extends InitialData {
if (content.containsKey('gridVideoRenderer')) {
video = content.get('gridVideoRenderer');
} else if (content.containsKey('richItemRenderer')) {
video =
content.get('richItemRenderer')?.get('content')?.get('videoRenderer');
video = content
.get('richItemRenderer')
?.get('content')
?.get(type.youtubeRenderText);
}

if (video == null) {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: youtube_explode_dart
description: A port in dart of the youtube explode library. Supports several API functions without the need of Youtube API Key.
version: 2.0.4
version: 2.1.0
homepage: https://github.com/Hexer10/youtube_explode_dart

topics:
Expand Down

0 comments on commit 6e9daf8

Please sign in to comment.