沃梦达 / 编程问答 / php问题 / 正文

用于任意页面的 Youtube Data API v3 pageToken

Youtube Data API v3 pageToken for arbitrary page(用于任意页面的 Youtube Data API v3 pageToken)

本文介绍了用于任意页面的 Youtube Data API v3 pageToken的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于 SO 的另一个问题显示,对于不同的搜索,pageTokens 是相同的,前提是页码和 maxResults 设置相同.

Another question on SO revealed that pageTokens are identical for different searches, provided that the page number and maxResults settings are the same.

API 的第 2 版允许您通过设置起始位置转到任意页面,但第 3 版仅提供下一个和上一个标记.即使您知道有 5 页结果,也不会从第 1 页跳转到第 5 页.

Version 2 of the API let you go to any arbitrary page by setting a start position, but v3 only provides next and previous tokens. There's no jumping from page 1 to page 5, even if you know there are 5 pages of results.

那么我们如何解决这个问题呢?

So how do we work around this?

推荐答案

YouTube pageToken 有六个字符长.以下是我能够确定的格式:

A YouTube pageToken is six characters long. Here's what I've been able to determine about the format:

char 1:我见过的总是'C'.char 2-3:编码的起始位置char 4-5:我见过的总是'QA'.char 6:'A' 表示位置大于或等于起始位置的列表项.'Q' 表示开始位置之前的列表项.

char 1: Always 'C' that I've seen. char 2-3: Encoded start position char 4-5: Always 'QA' that I've seen. char 6: 'A' means list items in a position greater than or equal to the start position. 'Q' means list items before the start position.

由于字符 6 的性质,有两种不同的方式来表示同一页面.给定 maxResults=1,可以通过将页面令牌设置为CAEQAA"或CAEQAA"来访问第 2 页.或CAIQAQ".第一个表示从结果编号 2(由字符 2-3AE"表示)开始并列出 1 个项目.第二种表示返回结果编号 3 之前的一项(由字符 2-3 AI"表示.

Due to the nature of character 6, there are two different ways to represent the same page. Given maxResults=1, page 2 can be reached by setting the page token to either "CAEQAA" or "CAIQAQ". The first one means to start at result number 2 (represented by characters 2-3 "AE") and list 1 item. The second means to return one item before result number 3 (represented by characters 2-3 "AI".

字符 2-3 是一种奇怪的 base 16 编码.

Characters 2-3 are a strange base 16 encoding.

字符 3 使用从 AZ 开始的列表,然后是 az,然后是 0-9,列表中每增加 1 就增加 4.系列是 A,E,I,M,Q,U,Y,c,g,k,o,s,w,0,4,8.字符 2 从 A 到 B 到 C 到 D 等等.出于我的目的,我没有使用大型结果集,所以我没有费心去看看第二个字符会发生什么超过几百个结果.也许有人在处理更大的集合时会提供关于角色 2 之后的行为方式的更新.

Character 3 uses a list from A-Z, then a-z, then 0-9 and increments by 4 in the list for each increase of 1. The series is A,E,I,M,Q,U,Y,c,g,k,o,s,w,0,4,8. Character 2 goes from A to B to C to D and so on. For my purposes, I'm not working with large result sets, so I haven't bothered to see what happens to the second character beyond a couple hundred results. Perhaps someone working with larger sets will provide an update as to how character 2 behaves after that.

因为字符串只包含一个开始位置和一个>="选项.或<",在多种情况下使用相同的字符串.例如,对于每页2个结果,第二页的开始位置是结果3.为此的pageToken是CAIQAA".这与第三页的令牌相同,每页一个结果.

Since the string only contains a start position and an option for ">=" or "<", the same string is used in multiple cases. For instance, with 2 results per page, the start position of the second page is result 3. The pageToken for this is "CAIQAA". This is identical to the token for the third page with one result per page.

由于我主要是 php 人,因此我使用以下函数来获取给定页面的 pageToken:

Since I'm primarily a php person, here's the function I'm using to get the pageToken for a given page:

function token($limit, $page) {
    $start = 1 + ($page - 1) * $limit;
    $third_chars = array_merge(
            range("A","Z",4),
            range("c","z",4),
            range(0,9,4));
    return 'C'.
           chr(ord('A') + floor($start / 16)).
           $third_chars[($start % 16) - 1].
           'QAA';
}
$limit = 1;
echo "With $limit result(s) per page...".PHP_EOL;
for ($i = 1; $i < 6; ++$i) {
    echo "The token for page $i is ".token($limit, $i).PHP_EOL;
}

请在您的项目中测试此功能,如果您发现缺陷或改进,请更新我们其他人,因为 YouTube 没有为我们提供简单的方法来做到这一点.

Please test this function in your project and update the rest of us if you find a flaw or an enhancement since YouTube hasn't provided us with an easy way to do this.

YouTube API v3 的页面令牌序列已更改,此系统将不再工作.有关最新和工作页面令牌的示例,请参阅 这个页面.

The page token sequence for YouTube API v3 has been changed, and this system will no longer work. For an example of the most up-to-date and working page tokens, see this page.

这篇关于用于任意页面的 Youtube Data API v3 pageToken的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:用于任意页面的 Youtube Data API v3 pageToken

基础教程推荐