コード例 #1
0
ファイル: __init__.py プロジェクト: Saectar/foofind-web
def download_search(file_data, file_text, fallback):
    '''
    Intenta buscar una cadena a buscar cuando viene download
    '''
    search_texts=[]
    if file_data:
        mds = file_data['file']['md']
        for key in ['audio:artist', 'audio:album', 'video:series', 'video:title', 'image:title', 'audio:title','application:name', 'application:title', 'book:title', 'torrent:title']:
            if key in mds and isinstance(mds[key], basestring) and len(mds[key])>1:
                search_texts.append((u(mds[key]), False))

        search_texts.append((file_data["view"]["fn"], True))
        if file_data['view']["tags"]:
            fallback = "(%s)"%file_data['view']["tags"][0]
        elif file_data['view']['file_type']:
            fallback = "(%s)"%file_data['view']['file_type']

    if file_text:
        search_texts.append((file_text, True))

    best_candidate = None
    best_points = 10000
    for main_position, (search_text, is_filename) in enumerate(search_texts):
        phrases = split_phrase(search_text, is_filename)

        for inner_position, phrase in enumerate(phrases):
            candidate = [part for part in phrase.split(" ") if part.strip()]

            count = sum(1 for word in candidate if len(word)>1)
            is_numeric = len(candidate)==1 and candidate[0].isdecimal()
            candidate_points = main_position+inner_position+(50 if count==0 else 5 if count==1 and is_numeric else 20 if count>15 else 0)

            if candidate_points<best_points:
                best_points = candidate_points
                best_candidate = candidate

        if best_candidate and best_points<2:
            break

    if best_candidate:
        return " ".join(best_candidate[:5])

    return fallback
コード例 #2
0
ファイル: search.py プロジェクト: kultus/foofind-web
def get_id_server_from_search(mongoid, file_name):
    uri1, uri2, uri3 = struct.unpack("III", mid2bin(mongoid))
    sph = sphinxapi2.SphinxClient()
    sph.SetMatchMode(sphinxapi2.SPH_MATCH_ALL)
    sph.SetServer(current_app.config["SERVICE_SPHINX"], current_app.config["SERVICE_SPHINX_PORT"])
    sph.SetConnectTimeout(current_app.config["SERVICE_SPHINX_CONNECT_TIMEOUT"])
    sph.SetMaxQueryTime(current_app.config["SERVICE_SPHINX_MAX_QUERY_TIME"])
    sph.SetLimits(0, 1, 1, 1)
    sph.SetFilter("uri1", [uri1])
    sph.SetFilter("uri2", [uri2])
    sph.SetFilter("uri3", [uri3])

    if file_name:
        query = " ".join(split_phrase(file_name, True)[0])
        query = sph.EscapeString(query)
    else:
        query = ""
    query = sph.Query(query, "idx_files")
    warn = error = ret = None
    if query and "total" in query and query["total"] == 1:
        if query["warning"]:
            warn = query["warning"]
        if query["error"]:
            error = query["error"]
        ret = query["matches"][0]["id"] / 0x100000000
    else:
        warn = sph.GetLastWarning()
        error = sph.GetLastError()
    if warn:
        logging.warn(
            "Warning on a Sphinx response",
            extra={"method": "get_id_server_from_search", "id": mongoid, "orig_msg": warn},
        )
    if error:
        logging.error(
            "Error on a Sphinx response",
            extra={"method": "get_id_server_from_search", "id": mongoid, "orig_msg": error},
        )
    sph.Close()
    feedbackdb.notify_indir(mongoid, ret)
    return ret