gpt4 book ai didi

objective-c - 如何在苹果iTunes搜索API中结合实体类型搜索

转载 作者:行者123 更新时间:2023-12-04 12:51:10 24 4
gpt4 key购买 nike

在itunes搜索api doc有一个搜索名为 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

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

    想法?

    最佳答案

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

    这是一个 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:@""];
    }

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

    关于objective-c - 如何在苹果iTunes搜索API中结合实体类型搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29426735/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com