解析电视剧(包括剧集)的 iTunes JSON

发布于 2024-11-30 13:15:28 字数 1530 浏览 0 评论 0原文

我正在开发一个个人项目来帮助组织我的 iTunes 库,为此我使用 iTunes 搜索 API

我目前拥有的代码允许我自动解析该系列本身的信息,但我正在尝试更改它,以便它能够识别一个季节中有多少集并解析它们的信息,而无需我编辑 php 为一季 13 集,然后为下一季编辑 27 集。

这是我当前使用的代码;

$response = file_get_contents($itune_url);
$obj = utf8_decode($response);
$results = json_decode($obj);

$artwork = $results->results[0]->artworkUrl100;
$collectionViewUrl = $results->results[0]->collectionViewUrl;
$collectionType = $results->results[0]->collectionType;
$collectionId = $results->results[0]->collectionId;
$collectionName = $results->results[0]->collectionName;
$trackCount = $results->results[0]->trackCount;
$collectionPrice = $results->results[0]->collectionPrice;
$genre = $results->results[0]->primaryGenreName;

$trackNumber1 = $results->results[1]->trackNumber;
$trackName1 = $results->results[1]->trackName;
$shortDescription1 = $results->results[1]->shortDescription;
$longDescription1 = $results->results[1]->longDescription;

无论如何,有没有办法设置我的 PHP,使其识别 $trackCount 并自动生成下一组结果?例如

$trackNumber1 = $results->results[1]->trackNumber;
$trackName1 = $results->results[1]->trackName;

:然后:

$trackNumber2 = $results->results[2]->trackNumber;
$trackName2 = $results->results[2]->trackName;

任何帮助/建议将不胜感激,谢谢!

I'm working on a personal project to help organise my iTunes library, to do this i'm using the iTunes search api.

The code i currently have allows me to parse the information for the Series itself automatically, but i'm trying to change it so it will recognise how many episodes there is in a season and parse the information for them as well, without me having to edit the php for 13 episodes for one season, then 27 for the next.

This is the code i am currently using;

$response = file_get_contents($itune_url);
$obj = utf8_decode($response);
$results = json_decode($obj);

$artwork = $results->results[0]->artworkUrl100;
$collectionViewUrl = $results->results[0]->collectionViewUrl;
$collectionType = $results->results[0]->collectionType;
$collectionId = $results->results[0]->collectionId;
$collectionName = $results->results[0]->collectionName;
$trackCount = $results->results[0]->trackCount;
$collectionPrice = $results->results[0]->collectionPrice;
$genre = $results->results[0]->primaryGenreName;

$trackNumber1 = $results->results[1]->trackNumber;
$trackName1 = $results->results[1]->trackName;
$shortDescription1 = $results->results[1]->shortDescription;
$longDescription1 = $results->results[1]->longDescription;

Is there anyway to set my PHP so it recognises the $trackCount and automatically generates the next set of results? e.g.

$trackNumber1 = $results->results[1]->trackNumber;
$trackName1 = $results->results[1]->trackName;

Then:

$trackNumber2 = $results->results[2]->trackNumber;
$trackName2 = $results->results[2]->trackName;

Any help/advice would be appreciated, thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷心人i 2024-12-07 13:15:28

您可以充分利用 foreach (docs) 来循环 JSON 结果,并且嵌套数组(docs)来保存所有剧集信息。这可以将理解 JSON 结果的阶段缩短为如下所示。

$url  = 'http://itunes.apple.com/search?term=firefly&media=tvShow&entity=tvEpisode&attribute=showTerm';
$json = file_get_contents($url);
$data = json_decode($json);

$results = array();
foreach ($data->results as $r) {
    $season  = $r->collectionName;
    $episode = $r->trackNumber;
    $results[$season][$episode] = $r;
}

结果数组具有以下结构。

array (
  'Firefly, Season 1' => 
  array (
    1 => 
    array (
      'wrapperType' => 'track',
      'kind' => 'tv-episode',
      'artistId' => 151198837,
      'collectionId' => 151801083,
      'trackId' => 151961947,
      'artistName' => 'Firefly',
      'collectionName' => 'Firefly, Season 1',
      'trackName' => 'Serenity',
      ...
    ),
    ...
  ),
)

这意味着您可以按编号访问各个剧集,例如剧集 Bushwhacked$results['Firefly, Season 1'][3],也可以循环播放一季:

foreach ($results['Firefly, Season 1'] as $episode) {
    echo $episode['trackName'], PHP_EOL, $episode['longDescription'], PHP_EOL;
}

当然,如果您不关心季节(您的所有结果都返回一个节目的单个季节?)或者想要在结果中获得多个节目(您在搜索什么?!),那么可以使用相同的过程已就业,只是标准不同等等(或更少)结果数组结构的深度。

You could make good use of foreach (docs) to loop over the JSON results, and nested arrays (docs) to hold all of the episode information. That could cut down your making-sense-of-the-JSON-result phase into something like the following.

$url  = 'http://itunes.apple.com/search?term=firefly&media=tvShow&entity=tvEpisode&attribute=showTerm';
$json = file_get_contents($url);
$data = json_decode($json);

$results = array();
foreach ($data->results as $r) {
    $season  = $r->collectionName;
    $episode = $r->trackNumber;
    $results[$season][$episode] = $r;
}

The results array has the following structure.

array (
  'Firefly, Season 1' => 
  array (
    1 => 
    array (
      'wrapperType' => 'track',
      'kind' => 'tv-episode',
      'artistId' => 151198837,
      'collectionId' => 151801083,
      'trackId' => 151961947,
      'artistName' => 'Firefly',
      'collectionName' => 'Firefly, Season 1',
      'trackName' => 'Serenity',
      ...
    ),
    ...
  ),
)

This means that you can access individual episodes by number, like $results['Firefly, Season 1'][3] for the episode Bushwhacked or you can loop over a season:

foreach ($results['Firefly, Season 1'] as $episode) {
    echo $episode['trackName'], PHP_EOL, $episode['longDescription'], PHP_EOL;
}

Of course, if you don't care about the season (all your results return a single season of a show?) or want to get multiple shows in the results (what are you searching on?!) then the same process can be employed, just with different criteria and more (or less) depth of the results array structure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文