Search in sources :

Example 1 with Result

use of com.biglybt.core.metasearch.Result in project BiglyBT by BiglySoftware.

the class SubscriptionDownloader method download.

protected void download() throws SubscriptionException {
    log("Downloading");
    Map map = JSONUtils.decodeJSON(subs.getJSON());
    Long engine_id = (Long) map.get("engine_id");
    String search_term = (String) map.get("search_term");
    String networks = (String) map.get("networks");
    Map filters = (Map) map.get("filters");
    Engine engine = manager.getEngine(subs, map, false);
    if (engine == null) {
        throw (new SubscriptionException("Download failed, search engine " + engine_id + " not found"));
    }
    List sps = new ArrayList();
    if (search_term != null) {
        sps.add(new SearchParameter("s", search_term));
        log("    Using search term '" + search_term + "' for engine " + engine.getString());
    }
    if (networks != null && networks.length() > 0) {
        sps.add(new SearchParameter("n", networks));
    }
    /*
		if ( mature != null ){

			sps.add( new SearchParameter( "m", mature.toString()));
		}
		*/
    SearchParameter[] parameters = (SearchParameter[]) sps.toArray(new SearchParameter[sps.size()]);
    SubscriptionHistoryImpl history = (SubscriptionHistoryImpl) subs.getHistory();
    try {
        Map context = new HashMap();
        context.put(Engine.SC_SOURCE, "subscription");
        Result[] results = engine.search(parameters, context, -1, -1, null, null);
        log("    Got " + results.length + " results");
        SubscriptionResultFilterImpl result_filter = new SubscriptionResultFilterImpl(subs, filters);
        results = result_filter.filter(results);
        log("    Post-filter: " + results.length + " results");
        SubscriptionResultImpl[] s_results = new SubscriptionResultImpl[results.length];
        for (int i = 0; i < results.length; i++) {
            SubscriptionResultImpl s_result = new SubscriptionResultImpl(history, results[i]);
            s_results[i] = s_result;
        }
        SubscriptionResultImpl[] all_results = history.reconcileResults(engine, s_results);
        checkAutoDownload(all_results);
        history.setLastError(null, false);
    } catch (Throwable e) {
        log("    Download failed", e);
        history.setLastError(Debug.getNestedExceptionMessage(e), e instanceof SearchLoginException);
        throw (new SubscriptionException("Search failed", e));
    }
}
Also used : SubscriptionException(com.biglybt.core.subs.SubscriptionException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SearchLoginException(com.biglybt.core.metasearch.SearchLoginException) Result(com.biglybt.core.metasearch.Result) List(java.util.List) ArrayList(java.util.ArrayList) SearchParameter(com.biglybt.core.metasearch.SearchParameter) Map(java.util.Map) HashMap(java.util.HashMap) Engine(com.biglybt.core.metasearch.Engine)

Example 2 with Result

use of com.biglybt.core.metasearch.Result in project BiglyBT by BiglySoftware.

the class SubscriptionResultFilterImpl method filter.

public Result[] filter(Result[] results) {
    List<Result> filteredResults = new ArrayList<>(results.length);
    for (int i = 0; i < results.length; i++) {
        Result result = results[i];
        String name = result.getName();
        // Results need a name, or they are by default invalid
        if (name == null) {
            continue;
        }
        name = name.toLowerCase();
        boolean valid = true;
        for (int j = 0; j < textFilters.length; j++) {
            // and mark the result as not valid
            if (!name.contains(textFilters[j])) {
                // double check against reg-expr if exists
                Pattern p = textFilterPatterns[j];
                if (p == null || !p.matcher(name).find()) {
                    valid = false;
                    break;
                }
            }
        }
        // if invalid after name check, let's get to the next result
        if (!valid) {
            continue;
        }
        for (int j = 0; j < excludeTextFilters.length; j++) {
            // and mark the result as not valid
            if (name.contains(excludeTextFilters[j])) {
                valid = false;
                break;
            } else {
                Pattern p = excludeTextFilterPatterns[j];
                if (p != null && p.matcher(name).find()) {
                    valid = false;
                    break;
                }
            }
        }
        // if invalid after name check, let's get to the next result
        if (!valid) {
            continue;
        }
        long size = result.getSize();
        if (minSize > -1) {
            if (minSize > size) {
                continue;
            }
        }
        if (maxSize > -1) {
            if (maxSize < size) {
                continue;
            }
        }
        if (minSeeds > -1) {
            if (minSeeds < result.getNbSeeds()) {
                continue;
            }
        }
        if (categoryFilter != null) {
            String category = result.getCategory();
            if (category == null || !category.equalsIgnoreCase(categoryFilter)) {
                continue;
            }
        }
        // All filters are ok, let's add the results to the filtered results
        filteredResults.add(result);
    }
    Result[] fResults = (Result[]) filteredResults.toArray(new Result[filteredResults.size()]);
    return fResults;
}
Also used : Pattern(java.util.regex.Pattern) ArrayList(java.util.ArrayList) Result(com.biglybt.core.metasearch.Result)

Aggregations

Result (com.biglybt.core.metasearch.Result)2 ArrayList (java.util.ArrayList)2 Engine (com.biglybt.core.metasearch.Engine)1 SearchLoginException (com.biglybt.core.metasearch.SearchLoginException)1 SearchParameter (com.biglybt.core.metasearch.SearchParameter)1 SubscriptionException (com.biglybt.core.subs.SubscriptionException)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Pattern (java.util.regex.Pattern)1