Skip to content

Commit 5e6dd68

Browse files
committed
wip
1 parent b4fb1d8 commit 5e6dd68

File tree

24 files changed

+137
-192
lines changed

24 files changed

+137
-192
lines changed

components/youtube_data_api/actions/add-playlist-items/add-playlist-items.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default {
1818
},
1919
videoIds: {
2020
type: "string[]",
21-
label: "Video Ids",
21+
label: "Video IDs",
2222
description: "Array of identifiers of the videos to be added to the playlist",
2323
},
2424
onBehalfOfContentOwner: {

components/youtube_data_api/actions/delete-playlist-items/delete-playlist-items.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default {
1818
},
1919
videoIds: {
2020
type: "string[]",
21-
label: "Video Ids",
21+
label: "Video IDs",
2222
description: "Array of identifiers of the videos to be removed from the playlist",
2323
async options() {
2424
const { data } = await this.youtubeDataApi.getPlaylistItems({

components/youtube_data_api/actions/list-videos/list-videos.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export default {
4141
};
4242
if (this.regionCode && this.regionCode.length === 2) {
4343
dynamicProps.videoCategoryId = {
44-
label: "Video Category Id",
44+
label: "Video Category ID",
4545
description: "The videoCategoryId parameter identifies the video category for which the chart should be retrieved. By default, charts are not restricted to a particular category.",
4646
type: "string",
4747
optional: true,

components/youtube_data_api/common-app.mjs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import youtube from "@googleapis/youtube";
2-
import { toArray } from "./utils.mjs";
2+
import { toArray } from "./common/utils.mjs";
33
import { promisify } from "util";
44
const pause = promisify((delay, fn) => setTimeout(fn, delay));
55

@@ -12,12 +12,12 @@ export default {
1212
reloadProps: true,
1313
},
1414
playlistId: {
15-
label: "Playlist Id",
15+
label: "Playlist ID",
1616
description: "The playlistId parameter specifies a unique YouTube playlist ID.",
1717
type: "string",
1818
},
1919
channelId: {
20-
label: "Channel Id",
20+
label: "Channel ID",
2121
description: "The channelId parameter specifies a unique YouTube channel ID.",
2222
type: "string",
2323
},
@@ -56,7 +56,7 @@ export default {
5656
maxResults: {
5757
type: "integer",
5858
label: "Maximum Results",
59-
description: "The maximum number of results in a channel to return. Should be divisible by 5 (ex. 5, 10, 15).",
59+
description: "The maximum number of items that should be returned in the result set. Acceptable values are 0 to 50, inclusive.",
6060
default: 20,
6161
},
6262
title: {
@@ -113,7 +113,7 @@ export default {
113113
},
114114
userOwnedPlaylist: {
115115
type: "string",
116-
label: "Playlist Id",
116+
label: "Playlist ID",
117117
description: "Add items to the selected playlist",
118118
async options({ prevContext }) {
119119
const { pageToken } = prevContext;
@@ -140,19 +140,25 @@ export default {
140140
},
141141
userOwnedVideo: {
142142
type: "string",
143-
label: "Video Id",
143+
label: "Video ID",
144144
description: "Select the video to update",
145-
async options({ prevContext }) {
145+
async options({
146+
prevContext, channelId,
147+
}) {
146148
const { pageToken } = prevContext;
147149
const params = {
148150
part: [
149151
"id",
150152
"snippet",
151153
],
152154
type: "video",
153-
forMine: true,
154155
pageToken,
155156
};
157+
if (channelId) {
158+
params.channelId = channelId;
159+
} else {
160+
params.forMine = true;
161+
}
156162
const { data } = await this.getVideos(params);
157163
const options = data.items?.map((item) => ({
158164
label: item.snippet.title,
@@ -168,7 +174,7 @@ export default {
168174
},
169175
userOwnedChannel: {
170176
type: "string",
171-
label: "Channel Id",
177+
label: "Channel ID",
172178
description: "Select the channel to update",
173179
async options({ prevContext }) {
174180
const { pageToken } = prevContext;

components/youtube_data_api/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/youtube_data_api",
3-
"version": "0.6.1",
3+
"version": "0.6.2",
44
"description": "Pipedream Youtube Components",
55
"main": "youtube_data_api.app.mjs",
66
"keywords": [
@@ -11,6 +11,7 @@
1111
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1212
"dependencies": {
1313
"@googleapis/youtube": "^6.0.0",
14+
"@pipedream/platform": "^3.0.3",
1415
"got": "^12.5.1",
1516
"util": "^0.12.4"
1617
},

components/youtube_data_api/sources/common.mjs renamed to components/youtube_data_api/sources/common/common.mjs

Lines changed: 43 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ export default {
2020
...this.getParams(),
2121
};
2222
const publishedAfter = await this.paginateVideos(params);
23-
if (publishedAfter) this._setPublishedAfter(publishedAfter);
24-
else this._setPublishedAfter(new Date());
23+
this._setPublishedAfter(publishedAfter || new Date());
2524
},
2625
},
2726
methods: {
@@ -49,6 +48,9 @@ export default {
4948
getParams() {
5049
return {};
5150
},
51+
isRelevant() {
52+
return true;
53+
},
5254
generateMeta(video) {
5355
const {
5456
id,
@@ -64,58 +66,54 @@ export default {
6466
const meta = this.generateMeta(result);
6567
this.$emit(result, meta);
6668
},
67-
/**
68-
* Paginate through results from getVideos(), emit each video, and return the
69-
* most recent date that a video was published.
70-
*
71-
* @param {object} params - The parameters to pass into getVideos().
72-
* @returns a string of the most recent date that a video was published
73-
*/
7469
async paginateVideos(params) {
75-
let count = 0;
76-
let totalResults = 1;
70+
const videos = await this.paginate({
71+
fn: this.youtubeDataApi.getVideos,
72+
params,
73+
});
74+
videos.forEach((video) => this.emitEvent(video));
75+
return videos[0].snippet.publishedAt;
76+
},
77+
async paginatePlaylistItems(params, publishedAfter = null) {
78+
const videos = await this.paginate({
79+
fn: this.youtubeDataApi.getPlaylistItems,
80+
params,
81+
publishedAfter,
82+
});
7783
let lastPublished;
78-
while (
79-
count < totalResults &&
80-
(!params.maxResults || count < params.maxResults)
81-
) {
82-
const results = (await this.youtubeDataApi.getVideos(params)).data;
83-
totalResults = results.pageInfo.totalResults;
84-
for (const video of results.items) {
85-
if (!lastPublished) lastPublished = video.snippet.publishedAt;
84+
for (const video of videos) {
85+
if (!lastPublished || Date.parse(video.snippet.publishedAt) > Date.parse(lastPublished)) {
86+
lastPublished = video.snippet.publishedAt;
8687
this.emitEvent(video);
87-
count++;
8888
}
89-
if (!results.items || results.items.length < 1) break;
90-
if (!results.nextPageToken) break;
91-
else params.pageToken = results.nextPageToken;
9289
}
9390
return lastPublished;
9491
},
95-
async paginatePlaylistItems(params, publishedAfter = null) {
96-
let totalResults = 1;
97-
let count = 0;
98-
let countEmitted = 0;
99-
let lastPublished;
100-
101-
while (count < totalResults && countEmitted < params.maxResults) {
102-
const results = (await this.youtubeDataApi.getPlaylistItems(params)).data;
103-
totalResults = results.pageInfo.totalResults;
104-
for (const video of results.items) {
105-
if (this.isRelevant(video, publishedAfter)) {
106-
if (
107-
!lastPublished ||
108-
Date.parse(video.snippet.publishedAt) > Date.parse(lastPublished)
109-
)
110-
lastPublished = video.snippet.publishedAt;
111-
this.emitEvent(video);
112-
countEmitted++;
113-
}
92+
async paginate({
93+
fn, params, publishedAfter,
94+
}) {
95+
let totalResults, done, count = 0;;
96+
const results = [];
97+
do {
98+
const {
99+
data: {
100+
pageInfo, items, nextPageToken,
101+
},
102+
} = await fn(params);
103+
for (const item of items) {
114104
count++;
105+
if (this.isRelevant(item, publishedAfter)) {
106+
results.push(item);
107+
if ((params.maxResults && results.length >= params.maxResults) || !nextPageToken) {
108+
done = true;
109+
break;
110+
}
111+
}
115112
}
116-
params.pageToken = results.nextPageToken;
117-
}
118-
return lastPublished;
113+
params.pageToken = nextPageToken;
114+
totalResults = pageInfo.totalResults;
115+
} while ((count < totalResults) && !done);
116+
return results;
119117
},
120118
},
121119
async run() {

components/youtube_data_api/sources/new-comment-in-channel/common.mjs

Lines changed: 0 additions & 71 deletions
This file was deleted.

components/youtube_data_api/sources/new-comment-in-channel/new-comment-in-channel.mjs

Lines changed: 0 additions & 23 deletions
This file was deleted.

components/youtube_data_api/sources/new-comment-posted/common.mjs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
import common from "../common.mjs";
1+
import common from "../common/common.mjs";
2+
import { ConfigurationError } from "@pipedream/platform";
23

34
export default {
45
...common,
56
hooks: {
67
...common.hooks,
7-
deploy() {},
8+
deploy() {
9+
if (!this.channelId && !this.videoId) {
10+
throw new ConfigurationError("Must enter one of `channelId` or `videoId`");
11+
}
12+
},
813
},
914
methods: {
1015
...common.methods,
@@ -22,8 +27,12 @@ export default {
2227

2328
const params = {
2429
part: "id,replies,snippet",
25-
videoId: this.videoId,
2630
};
31+
if (this.videoId) {
32+
params.videoId = this.videoId;
33+
} else {
34+
params.allThreadsRelatedToChannelId = this.channelId;
35+
}
2736

2837
const items = [];
2938
while (true) {
@@ -47,12 +56,9 @@ export default {
4756
for (const comment of comments) {
4857
const publishedAt = Date.parse(comment.snippet.publishedAt);
4958
if (!publishedAfter || publishedAt > publishedAfter) {
50-
const meta = this.generateMeta(comment);
51-
this.$emit(comment, meta);
52-
}
53-
if (publishedAt > maxPublishedAfter) {
54-
maxPublishedAfter = publishedAt;
59+
this.emitEvent(comment);
5560
}
61+
maxPublishedAfter = Math.max(maxPublishedAfter, publishedAt);
5662
}
5763

5864
this._setPublishedAfter(maxPublishedAfter);

0 commit comments

Comments
 (0)