YouTube API:检索用户在给定日期 before/after 订阅的订阅列表

YouTube API: Retrieve a list of subscriptions the user subscribed to before/after a given date

我正在创建一个 RESTful API,其中一些端点必须使用 YouTube API 来获取所需用户的数据,而我正在努力解决其中一个问题现在几天的功能。

其中一项功能涉及 YouTube 订阅,并允许用户将自己的数据分配给他们。然后订阅数据将存储在 API 数据库中。我面临的问题是,我想检索尚未分配给用户的列表(意思是:尚未 return 由 API 编辑)订阅。必须对未分配订阅的端点进行分页,因为我无法用可能很大的对象进行响应。所以我想让用户指定参数,例如 page, per_page.

YouTube API 也以分页的订阅列表作为响应。

请求 GET https://www.googleapis.com/youtube/v3/subscriptions?part=snippet&mine=true&key=[YOUR_API_KEY] 给我一个响应,包括 nextPageTokenprevPageToken 等字段,订阅对象列表,其中每个对象都包含 publishedAt 字段。

我想到的解决方案如下:

为了跟踪 returned 订阅,我会在 API 数据库中创建以下字段:

lastSubscriptionDate: datetime - 我的 API

缓存的最早订阅日期

firstSubscriptionDate: datetime - 我的 API

缓存的最新订阅日期

lastPageToken: string - 我从中请求数据并保存的最后一页 lastSubscriptionDate

流量:

  1. 用户请求GET /subscriptions?filter=unassigned&page=[PAGE]&per_page=[PER_PAGE]
  2. 由于我会按时间顺序(最近)从 YouTube API 请求这些订阅,因此我有以下情况:

我检查 lastSubscriptionDate 是否等于 lastPageToken,

页面上最后一个结果的 publishedAt

if true => return nextPageToken 的页面(如果存在)并更新我的 'tracking' 数据。

其他=>

a) 页面上最后一个结果的 publishedAt 早于缓存 lastSubscriptionDate <=> 用户删除了他的一些订阅。

b) 页面上最后一个结果的 publishedAt 比缓存 lastSubscriptionDate 更新 <=> 用户订阅了一些新的 YouTube 频道

在 a) 和 b) 选项中,最好的方法是在 lastSubscriptionDate 或之后检索 N(其中 N 等于 per_page 参数)订阅在 firstSubscriptionDate 之前确保我不会在分页过程中跳过任何订阅,但我觉得用当前版本的 YouTube API.

是不可能的

我错过了什么吗?有没有其他方法可以从 YouTube API 检索那些未分配的订阅,同时确保我没有跳过任何订阅?

你的话:

Since I would request those subscriptions from the YouTube API in the chronological order (by the most recent) [...].

Subscriptions.list API endpoint queried with mine=true has no parameter that would enable you to obtain Subscription resources ordered by publishedAt.

我看不到其他解决方案,但您的应用程序具有 刷新我的订阅列表 功能,用户在知道 he/she 已更改时调用 his/her YouTube 网络 UI 中的订阅列表。当然,事情可以更进一步:例如,还可以在没有任何用户干预的情况下定期调用 刷新我的订阅列表(使用您设计的逻辑,使其适合您应用程序的一般工作方式).

刷新我的订阅列表然后将从 YouTube 数据 API 中获取所有 Subscription resources 并匹配此列表 -- L1 -- 用一个 -- L2 -- 记录在你的应用程序的数据库中以获得差异:对于 L1L2 的并集中的每个条目附加 (1) = 到两个列表中的那些,(2) - 到仅存在于 L2 中的那些和 (3) + 到仅存在于 L1.[=42= 中的那些]

然后,计算出的差异将允许更新您的应用程序的数据库(从而使其与 YouTube 的远程同步)。


附录

(我不确定使用 Activities.list API 端点是否值得麻烦:我只是指出来让您了解它。)

您可以考虑使用 Activities.list API 端点查询优化您的 刷新我的订阅列表 功能的逻辑:

  • mine=true,
  • part=snippet,contentDetails,
  • fields=items(snippet(type),contentDetails(subscription)),以及
  • publishedAfter=....

但是,不幸的是,API 的实现方式只解决了我上面描述的 差异计算 的部分问题:

contentDetails.subscription (object)
The subscription object contains information about a channel that a user subscribed to. This property is only present if the snippet.type is subscription.

contentDetails.subscription.resourceId (object)
The resourceId object contains information that identifies the resource that the user subscribed to.

contentDetails.subscription.resourceId.kind (string)
The type of the API resource.

contentDetails.subscription.resourceId.channelId (string)
The ID that YouTube uses to uniquely identify the channel that the user subscribed to. This property is only present if the resourceId.kind is youtube#channel.

这些引号表明 API 只会让您知道自从 datetime 值传递给 [=35] 后您进行了哪些 新订阅 =].从 API 中获得的数据只是上述 diff 的 + 部分。