@@ -121,54 +121,60 @@ class _PkgOfWeekVideoFetcher {
121
121
final youtube = YouTubeApi (apiClient);
122
122
123
123
try {
124
- final videos = < PkgOfWeekVideo > [];
125
124
String ? nextPageToken;
126
- for (var check = true ; check && videos.length < 50 ;) {
127
- final rs = await cache.youtubePlaylistItems ().get (
125
+
126
+ final videos = < PkgOfWeekVideo > [];
127
+ final videoIds = < String > {};
128
+ while (videos.length < 50 ) {
129
+ // get page from cache or from Youtube API
130
+ final rs = await cache.youtubePlaylistItems (nextPageToken ?? '' ).get (
128
131
() async => await youtube.playlistItems.list (
129
132
['snippet' , 'contentDetails' ],
130
133
playlistId: powPlaylistId,
131
134
pageToken: nextPageToken,
132
135
),
133
136
);
134
- videos.addAll (rs! .items! .map (
135
- (i) {
136
- try {
137
- final videoId = i.contentDetails? .videoId;
138
- if (videoId == null ) {
139
- return null ;
140
- }
141
- final thumbnails = i.snippet? .thumbnails;
142
- if (thumbnails == null ) {
143
- return null ;
144
- }
145
- final thumbnail = thumbnails.high ??
146
- thumbnails.default_ ??
147
- thumbnails.maxres ??
148
- thumbnails.standard ??
149
- thumbnails.medium;
150
- final thumbnailUrl = thumbnail? .url;
151
- if (thumbnailUrl == null || thumbnailUrl.isEmpty) {
152
- return null ;
153
- }
154
- return PkgOfWeekVideo (
155
- videoId: videoId,
156
- title: i.snippet? .title ?? '' ,
157
- description:
158
- (i.snippet? .description ?? '' ).trim ().split ('\n ' ).first,
159
- thumbnailUrl: thumbnailUrl,
160
- );
161
- } catch (e, st) {
162
- // this item will be skipped, the rest of the list may be displayed
163
- _logger.pubNoticeShout (
164
- 'youtube' , 'Processing Youtube PlaylistItem failed.' , e, st);
137
+
138
+ // process playlist items
139
+ for (final i in rs! .items! ) {
140
+ try {
141
+ final videoId = i.contentDetails? .videoId;
142
+ if (videoId == null || videoIds.contains (videoId)) {
143
+ continue ;
144
+ }
145
+ final thumbnails = i.snippet? .thumbnails;
146
+ if (thumbnails == null ) {
147
+ continue ;
148
+ }
149
+ final thumbnail = thumbnails.high ??
150
+ thumbnails.default_ ??
151
+ thumbnails.maxres ??
152
+ thumbnails.standard ??
153
+ thumbnails.medium;
154
+ final thumbnailUrl = thumbnail? .url;
155
+ if (thumbnailUrl == null || thumbnailUrl.isEmpty) {
156
+ continue ;
165
157
}
166
- return null ;
167
- },
168
- ).nonNulls);
169
- // next page
158
+ videoIds.add (videoId);
159
+ videos.add (PkgOfWeekVideo (
160
+ videoId: videoId,
161
+ title: i.snippet? .title ?? '' ,
162
+ description:
163
+ (i.snippet? .description ?? '' ).trim ().split ('\n ' ).first,
164
+ thumbnailUrl: thumbnailUrl,
165
+ ));
166
+ } catch (e, st) {
167
+ // this item will be skipped, the rest of the list may be displayed
168
+ _logger.pubNoticeShout (
169
+ 'youtube' , 'Processing Youtube PlaylistItem failed.' , e, st);
170
+ }
171
+ }
172
+
173
+ // advance to next page token
170
174
nextPageToken = rs.nextPageToken;
171
- check = nextPageToken != null && nextPageToken.isNotEmpty;
175
+ if (nextPageToken == null ) {
176
+ break ;
177
+ }
172
178
}
173
179
return videos;
174
180
} finally {
0 commit comments