示例#1
0
def _convertCategorySubcategory(category, subcategory):
    kinds   = None
    types   = None

    if subcategory is not None:
        kinds = mapSubcategoryToKinds(subcategory)
        types = mapSubcategoryToTypes(subcategory)

    elif category is not None:
        kinds = mapCategoryToKinds(category)
        types = mapCategoryToTypes(category)

    logs.info('KINDS: %s' % kinds)
    logs.info('TYPES: %s' % types)

    return kinds, types
示例#2
0
    def searchLite(self, queryCategory, queryText, timeout=None, coords=None, logRawResults=False):
        if queryCategory not in ('music', 'film', 'app', 'book'):
            raise NotImplementedError()

        supportedProxyTypes = {
            'music': (iTunesArtist, iTunesAlbum, iTunesTrack),
            'film': (iTunesMovie, iTunesTVShow),
            'app': (iTunesApp,),
            'book': (iTunesBook,),
        }[queryCategory]

        types = mapCategoryToTypes(queryCategory)
        iTunesTypes = []
        typesMap = dict(self.__types_to_itunes_strings)
        for entityType in types:
            iTunesTypes.append(typesMap[entityType])

        pool = Pool(len(iTunesTypes))
        rawResults = {}
        for iTunesType in iTunesTypes:
            pool.spawn(self.__searchEntityTypeLite, iTunesType, queryText, rawResults, timeout)
        pool.join(timeout=timeout)

        if logRawResults:
            logComponents = ["\n\n\nITUNES RAW RESULTS\nITUNES RAW RESULTS\nITUNES RAW RESULTS\n\n\n"]

        searchResultsByType = {}
        # Convert from JSON objects to entity proxies. Pass through actual parsing errors, but report & drop the result
        # if we just see a type we aren't expecting. (Music search will sometimes return podcasts, for instance.)
        for (iTunesType, rawTypeResults) in rawResults.items():
            processedResults = []
            for rawResult in rawTypeResults:
                try:
                    if logRawResults:
                        logComponents.extend(['\n\n', pformat(rawResult), '\n\n'])
                    proxy = self.__createEntityProxy(rawResult, maxLookupCalls=0)
                    if not any(isinstance(proxy, proxyType) for proxyType in supportedProxyTypes):
                        logs.warning('Dropping iTunes proxy of unsupported type %s for queryCategory %s:\n\n%s\n\n' %
                                     (proxy.__class__.__name__, queryCategory, str(proxy)))
                        continue
                    processedResults.append(self.__createEntityProxy(rawResult, maxLookupCalls=0))
                except UnknownITunesTypeError:
                    logs.report()
                    pass

            if len(processedResults) > 0:
                searchResultsByType[iTunesType] = self.__scoreResults(iTunesType, processedResults, queryText)

        if logRawResults:
            logComponents.append("\n\n\nEND RAW ITUNES RESULTS\n\n\n")
            logs.debug(''.join(logComponents))

        if len(searchResultsByType) == 0:
            # TODO: Throw exception to avoid cache?
            return []
        if len(searchResultsByType) == 1:
            return searchResultsByType.values()[0]
        if queryCategory == 'music':
            # We have to separately request songs, albums, and artists because iTunes does a terrible job blending
            # results between the three. So we need to blend, but it's hard to know how to. We do a little work on the
            # string matching side, but
            self.__augmentAlbumAndArtistResultsWithSongs(searchResultsByType.get('album', []),
                searchResultsByType.get('musicArtist', []),
                searchResultsByType.get('song', []))
        return interleaveResultsByRelevance(searchResultsByType.values())