【问题标题】:How to combine entity type searches in apple itunes search api如何在 Apple iTunes 搜索 API 中结合实体类型搜索
【发布时间】:2015-06-08 05:19:16
【问题描述】:

在 iTunes 搜索 apidoc 中有一个搜索名为 maroon 的艺术家的示例,网址如下:

https://itunes.apple.com/search?term=maroon&entity=allArtist&attribute=allArtistTerm

这会返回 50 多个这样开始的结果:

{
    "resultCount": 50,
    "results": [
        {
            "wrapperType": "artist",
            "artistType": "Artist",
            "artistName": "Maroon 5",
            "artistLinkUrl": "https://itunes.apple.com/us/artist/maroon-5/id1798556?uo=4",
            "artistId": 1798556,
            "amgArtistId": 529962,
            "primaryGenreName": "Pop",
            "primaryGenreId": 14,
            "radioStationUrl": "https://itunes.apple.com/station/idra.1798556"
        },
        {
            "wrapperType": "artist",
            "artistType": "Software Artist",
            "artistName": "MaroonEntertainment",
            "artistLinkUrl": "https://itunes.apple.com/us/artist/maroonentertainment/id537029262?uo=4",
            "artistId": 537029262,
            "radioStationUrl": "https://itunes.apple.com/station/idra.537029262"
        },

这很好。但是,这是我的问题:我想创建一个尽可能具体的搜索查询,方法是将搜索艺术家和歌曲名称以及专辑名称结合起来。..

例如,我得到了这首歌:

  • 歌曲:跨越大鸿沟
  • 专辑:大鸿沟
  • 艺术家: Semisonic

我只能搜索艺术家姓名:

https://itunes.apple.com/search?term=Semisonic&entity=allArtist&attribute=allArtistTerm

我只能搜索歌曲词:

https://itunes.apple.com/search?term=Across the Great Divide&entity=song&attribute=songTerm

我只能搜索专辑名称:

https://itunes.apple.com/search?term=Great Divide&entity=album&attribute=albumTerm

但是这些人都没有给我我想要的结果(我可以在其他 50 人中找到我正在寻找的结果.. 但我只是希望搜索查询足够具体以避免任何客户端过滤类型东西)。

如何组合这些搜索?如果我只是将两个搜索添加在一起(在本例中,我正在搜索歌曲 艺术家):

https://itunes.apple.com/search?term=Across the Great Divide&entity=song&attribute=songTerm&term=Semisonic&entity=allArtist&attribute=allArtistTerm

然后苹果将简单地忽略第一个搜索类型(即歌曲)并仅返回艺术家的结果)。

想法?

【问题讨论】:

    标签: objective-c itunes itunes-sdk itunes-search-api


    【解决方案1】:

    嗯,这更像是一个“解决方法”的答案..但这是我正在使用的解决方案..所以不妨传播爱吧?

    这是一个 100% 的客户端解决方案(即整个 iTunes 音乐数据库可以下载到我自己的服务器中。然后我可以围绕它创建所有搜索包装器。但这本身就是一个项目)。

    这是我得到的:

    // this is just a wrapper around the apple search api.. it makes your 
    // average joe http get request
    [[AppleServer shared] searchForSongWithTitle:track.title andAlbumName:track.albumName completion:^(NSArray *results, NSError *error){
        if ([results count] >0) {
            NSLog(@"[%d] unfiltered songs retrieved from apple search api", [results count]);
            NSDictionary *filteredResult = [[self class] filterResults:results ToMatchTrack:track];
            if (!filteredResult) {
                NSLog(@"Filtering may be too strict, we got [%d] results from apple search api but none past our filter", [results count]);
                return;
            }
    
            .. process results
    
    
    + (NSDictionary *)filterResults:(NSArray *)results ToMatchTrack:(VBSong *)track
    {
    
        NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(NSDictionary *evaluatedTrack, NSDictionary *bindings){    
            BOOL result =
             ([track.title isLooselyEqualToString:evaluatedTrack[@"trackName"]] &&
              [track.artistName isLooselyEqualToString:evaluatedTrack[@"artistName"]] &&
              [track.albumName isLooselyEqualToString:evaluatedTrack[@"collectionName"]]);
    
            NSLog(@"match?[%d]", result);
    
            return result;
        }];
    
        return [[results filteredArrayUsingPredicate:predicate] firstObject];
    }
    

    这里的关键方法是isLooselyEqualToString.. 它定义在一个NSString 类别中,如下所示:

    /**
     * Tests if one string equals another substring, relaxing the following contraints
     *   - one string can be a substring of another
     *   - it's a case insensitive comparison
     *   - all special characters are removed from both strings
     *
     *     ie this should return true for this comparison:
     *     - comparing self:"Circus One (Presented By Doctor P and Flux Pavilion)" 
                    and str:"Circus One presented by Doctor P"
     *
     * @param str string to compare self against
     * @return if self is the same as str, relaxing the contraints described above
     */
    - (BOOL)isLooselyEqualToString:(NSString *)str
    {
        return [[self removeSpecialCharacters] containSubstringBothDirections:[str removeSpecialCharacters]];
    }
    
    /**
     * Tests if one string is a substring of another
     *     ie this should return true for both these comparisons:
     *     - comparing self:"Doctor P & Flux Pavilion" and substring:"Flux Pavilion"
     *     - comparing self:"Flux Pavilion" and substring:"Doctor P & Flux Pavilion"
     *
     * @param substring to compare self against
     * @return if self is a substring of substring
     */
    -(BOOL)containSubstringBothDirections:(NSString*)substring
    {
        if (substring == nil) return self.length == 0;
    
        if ([self rangeOfString:substring options:NSCaseInsensitiveSearch].location == NSNotFound) {
            if ([substring rangeOfString:self options:NSCaseInsensitiveSearch].location == NSNotFound) {
                return NO;
            } else {
                return YES;
            }
        } else {
            return YES;
        }
    }
    
    - (NSString *)removeSpecialCharacters
    {
        NSMutableCharacterSet *specialCharsSet = [[NSCharacterSet letterCharacterSet] mutableCopy];
        [specialCharsSet formUnionWithCharacterSet:[NSCharacterSet whitespaceCharacterSet]];
        return [[self componentsSeparatedByCharactersInSet:[specialCharsSet invertedSet]] componentsJoinedByString:@""];
    }
    

    奖金 这是我们目前正在使用的解决方案..我完全意识到可能会出现一些破坏该算法的术语..因此我们对此进行了单元测试,我们逐步添加术语以确保我们继续改进我们的算法而不是导致回归错误..如果我在这个答案上获得足够的投票,我会发布它。呵呵。

    【讨论】:

      【解决方案2】:

      阿布,

      对不起,你不能从这里到那里! (除非其他人发现了新东西。)

      我目前正在开发一个应用程序,它将结合多个查询的结果。

      对于更具冒险精神的人,Apple 向附属合作伙伴提供“来自 iTunes 和 App Store 的完整元数据集的数据馈送”。为了使用它,我会在云中的某处放置一个数据库服务,并使用它来进行更详细的查询并显示搜索 API 未返回的详细信息。

      如果我完成了我的应用并且它实际上被超过 5 个人使用,我可能会考虑做整个数据库版本。

      大卫

      【讨论】:

      • @DavidReich 你好,大卫,你有没有在使用 Apple DataFeed 时遇到过困难?如果是这样,您需要做什么才能获得附属合作伙伴?
      • @ThreadPitt 我从未完成该应用程序。我发现 Apple 使用了 URL 搜索 API 返回的记录中不存在且 DataFeed 中不存在的关键字。例如......我做了一个返回有声读物记录的搜索。那些记录没有我使用的搜索词!我也不认为这在 DataFeed 中。 (现在是几年前的事了。)我在亚马逊上查找了有声读物。搜索词是“叙述者”的名字。后来我放弃了!附属公司也是几年前的事了。除了常规开发者帐户之外,还有更多步骤。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-04
      • 2012-10-02
      • 2012-02-17
      • 1970-01-01
      • 1970-01-01
      • 2017-06-30
      相关资源
      最近更新 更多