Youtube Data API v3 pageToken任意页面 [英] Youtube Data API v3 pageToken for arbitrary page

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

问题描述

关于SO的另一个问题表明,只要页码和maxResults设置相同,则对于不同的搜索,pageTokens是相同的.

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允许您通过设置开始位置转到任意页面,但是v3仅提供下一个和上一个标记.即使您知道结果有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". 字符2-3:已编码的开始位置 char 4-5:我见过的总是"QA". 字符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"或"CAIQAQ"来访问第2页.第一种表示从结果编号2(用字符2-3"AE"表示)开始并列出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是奇怪的基数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 Data API v3 pageToken任意页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆