2

以下のコードを使用して完全に機能しているクエリを使用して、単純にビデオを検索しようとしています。

  // Create a service object for executing queries
GTLServiceYouTube *service = [[GTLServiceYouTube alloc] init];
// Services which do not require sign-in may need an API key from the
// API Console
service.APIKey = @"AIzaSyA-e4NldR2o8vYPpL6IcAcMH3HSnEpPPJY";
// Create a query
GTLQueryYouTube *query = [GTLQueryYouTube queryForSearchListWithPart:@"id,snippet"];
query.maxResults = 10;
query.q = searchBar.text;
query.videoEmbeddable = @"true";
query.type = @"video";
//query.country = @"US";
// Execute the query
GTLServiceTicket *ticket = [service executeQuery:query
                               completionHandler:^(GTLServiceTicket *ticket, id object, NSError *error) {
                                   // This callback block is run when the fetch completes
                                   if (error == nil) {
                                       GTLYouTubeSearchListResponse *products = object;

                                       [videoArray removeAllObjects];
                                       // iteration of items and subscript access to items.
                                       for (GTLYouTubeSearchResult *item in products) {



                                           NSMutableDictionary *dictionary = [item JSONValueForKey:@"id"];

                                           NSLog(@"%@", [dictionary objectForKey:@"videoId"]);
                                           YoutubeVideo *video = [[YoutubeVideo alloc]init];
                                           [video setLblTitle:item.snippet.title];

                                           //Get youtube video image
                                           [video setImgIconURL:[NSURL URLWithString:item.snippet.thumbnails.defaultProperty.url]];


                                           [video setLblVideoURL:[dictionary objectForKey:@"videoId"]];

                                           [video setLblChannelTitle:item.snippet.channelTitle];
                                           [videoArray addObject:video];


                                       }
                                       reloadData = YES;
                                       [tableView reloadData];

                                       //Download images asynchronously
                                       [NSThread detachNewThreadSelector:@selector(downloadImages)
                                                                toTarget:self
                                                              withObject:nil];
                                   }else{
                                       NSLog(@"Error: %@", error.description);
                                   }
                               }];

ただし、ビデオに関する特定の情報を表示したいと思います。私が得ることができるこの情報のいくつか

  item.snippet

ただし、動画の長さと再生回数も取得する必要があります。Youtube API 3.0 を使用してそれらを取得するにはどうすればよいですか?? これだけのために GData を使用するというアイデアもありましたが、使用するロード時間が文字通り 3 倍になります

  NSString *JSONString = [NSString stringWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://gdata.youtube.com/feeds/api/videos/%@?v=2&alt=json", [video lblVideoURL]]] encoding:NSUTF8StringEncoding error:nil ];

動画の長さと動画の再生回数を取得するにはどうすればよいですか?

4

3 に答える 3

0

検索 API から ID を収集し、別のビデオ リスト API 呼び出しを行うことは、達成したいことを行うための適切な方法です。動画リスト API 呼び出しでは、同じ呼び出しで複数の動画 ID をカンマで区切ることができます。これはAPI v3で予期される動作であるため、追加の呼び出しは使い果たされるとは見なされません。

プロジェクト メンバー #1 je...@google.com

これは予想される動作であり、変更される可能性はありません。search.list() メソッドはチャンネル、ビデオ、およびプレイリストを返すことができるため、これらすべてのリソース タイプに対して意味のあるプロパティのみが検索応答で返されます。その他のプロパティを取得する必要がある場合は、videos.list() などにフォローアップ リクエストを行う必要があります。最大 50 個のビデオ ID を videos.list() に渡すことができるため、1 回の video.list() 呼び出しでページ全体の search.list() の結果を効果的に検索できます。

https://developers.google.com/youtube/v3/docs/videos/list#try-itを試してみると、 contentDetails,statisticsを一部として設定すると、次の結果が得られるはずです。

"contentDetails": { "duration": "PT20M38S", "dimension": "2d", "definition": "hd", "caption": "false", "licensedContent": false },

"statistics": { "viewCount": "191", "likeCount": "7", "dislikeCount": "0", "favoriteCount": "0", "commentCount": "0" }

PT20M38Sは、ISO 8601 ( http://en.wikipedia.org/wiki/ISO_8601 )に基づく 20 分 38 秒を意味します。

于 2013-04-05T07:57:28.327 に答える
0

これを行う最良の方法は次のとおりです。

if (!service) {
        service = [[GTLServiceYouTube alloc] init];
        service.shouldFetchNextPages = YES;
        service.shouldFetchInBackground = YES;
        service.retryEnabled = YES;
        service.APIKey = @"AIzaSyDSO2JPnM_r9VcDrDJJs_d_7Li2Ttk2AuU";
    }
    [youtubeList removeAllObjects];

    GTLQueryYouTube *query = [GTLQueryYouTube queryForSearchListWithPart:@"id"];
    query.maxResults = 50;
    query.q = withText;
    query.fields = @"items(id)";
    query.order = @"viewCount";
    query.type = @"video";
//    query.videoDuration = @"long";//any-long-medium-short

    __block NSInteger incrementRequest = 0;
    [service executeQuery:query completionHandler:^(GTLServiceTicket *ticket, id object, NSError *error) {
        if (error) {
            NSLog(@"Error is!! = %@", error.localizedDescription);
            return;
        }
        GTLYouTubeVideoListResponse *idsResponse = object;
        for (GTLYouTubeVideoListResponse *videoInfo in object) {
            [youtubeList addObject:videoInfo.JSON];

            GTLQueryYouTube *query2 = [GTLQueryYouTube queryForVideosListWithIdentifier:[[videoInfo.JSON valueForKey:@"id"] valueForKey:@"videoId"] part:@"id,contentDetails,snippet,statistics"];
            query2.maxResults = 1;
            query2.fields = @"items(id,contentDetails,snippet,statistics)";
            query2.order = @"viewCount";

            [service executeQuery:query2 completionHandler:^(GTLServiceTicket *ticket, id object, NSError *error) {
                if (error) {
                    NSLog(@"Error is!! = %@", error.localizedDescription);
                    return;
                }
                GTLYouTubeVideoListResponse *detalhe = object;

                for (NSMutableDictionary *tmpDict in youtubeList) {
                    if ([[[tmpDict valueForKey:@"id"] valueForKey:@"videoId"] isEqualToString:[[[detalhe.JSON valueForKey:@"items"] objectAtIndex:0] valueForKey:@"id"]]) {
                        [tmpDict removeObjectForKey:@"id"];
                        //condition personal
                        if (![Utils parseISO8601TimeIsGrater30:[[[[detalhe.JSON valueForKey:@"items"] objectAtIndex:0] valueForKey:@"contentDetails"] valueForKey:@"duration"]]) {
                            BOOL isBlockedInUs = NO;
                            for (NSString *countryRestric in [[[[[detalhe.JSON valueForKey:@"items"] objectAtIndex:0] valueForKey:@"contentDetails"] valueForKey:@"regionRestriction"] valueForKey:@"blocked"]) {
                                if ([countryRestric isEqualToString:@"US"]) {
                                    isBlockedInUs = YES;
                                    break;
                                }
                            }
                            if (!isBlockedInUs) {
                                [tmpDict addEntriesFromDictionary:detalhe.JSON];
                                [tmpDict setValue:[[[[detalhe.JSON valueForKey:@"items"] objectAtIndex:0] valueForKey:@"snippet"] valueForKey:@"publishedAt"] forKey:@"publishedAt"];
                            } else {
                                [youtubeList removeObject:tmpDict];
                            }
                        } else {
                            [youtubeList removeObject:tmpDict];
                        }

                        break;
                    }
                }

                incrementRequest ++;
                if ([idsResponse.items count] == incrementRequest) {
                    //Finish
                    [self.tableView reloadData];
                }
            }];
        }
    }];
于 2015-10-27T22:11:22.240 に答える