Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit cd7d0f93 authored by Martin Herndl's avatar Martin Herndl
Browse files

Lookup: improve "Auskunft" (AT) API.

This is a follow-up on Ib57a4aca35bcd80f320c5a2a44faac4c9c9d3026
which improves the parsing process by working the search response of
section by section to not miss any incomplete data sets.
The old process was kind of error prone because it searched for all
names, numbers and addresses and tried to match those which of
course failed if there was e.g. an address missing.

Change-Id: Ib685477916e06e63e9b63f2fff7f2bf47a9d9ba3
parent 2c80a616
Loading
Loading
Loading
Loading
+32 −29
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import com.android.dialer.lookup.ContactBuilder;
import com.android.dialer.lookup.LookupUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public final class AuskunftApi {
@@ -34,7 +35,7 @@ public final class AuskunftApi {
            "https://auskunft.at/suche";

    private static final String SEARCH_RESULTS_REGEX =
            "(?i)class=[\"']?search-list(.*?)class=[\"']?pagination";
            "(?i)<section[\\s]+class=[\"']?search-entry(.*?)?</section";
    private static final String NAME_REGEX =
            "(?i)<h1[\\s]+itemprop=[\"']?name[\"']?>(.*?)</h1";
    private static final String NUMBER_REGEX =
@@ -47,7 +48,7 @@ public final class AuskunftApi {
    private AuskunftApi() {
    }

    public static ContactInfo[] query(String filter, int lookupType, String normalizedNumber,
    public static List<ContactInfo> query(String filter, int lookupType, String normalizedNumber,
            String formattedNumber) throws IOException {
        // build URI
        Uri uri = Uri.parse(PEOPLE_LOOKUP_URL)
@@ -55,38 +56,36 @@ public final class AuskunftApi {
                .appendQueryParameter("query", filter)
                .build();

        // get search results from HTML to speedup subsequent matching and avoid errors
        String output = LookupUtils.firstRegexResult(LookupUtils.httpGet(uri.toString(), null),
                SEARCH_RESULTS_REGEX, true);
        // get all search entry sections
        List<String> entries = LookupUtils.allRegexResults(LookupUtils.httpGet(uri.toString(),
                null), SEARCH_RESULTS_REGEX, true);

        // get all names, abort lookup if nothing found
        List<String> names = LookupUtils.allRegexResults(output, NAME_REGEX, true);
        if (names == null || names.isEmpty()) {
        // abort lookup if nothing found
        if (entries == null || entries.isEmpty()) {
            Log.w(TAG, "nothing found");
            return null;
        }

        // get all numbers and addresses
        List<String> numbers = LookupUtils.allRegexResults(output, NUMBER_REGEX, true);
        List<String> addresses = LookupUtils.allRegexResults(output, ADDRESS_REGEX, true);

        // abort on invalid data (all the data arrays must have the same size because we query
        // all the results at once and expect them to be in the right order)
        if (numbers == null || addresses == null || names.size() != numbers.size() ||
                numbers.size() != addresses.size()) {
            Log.w(TAG, "names, numbers and address data do not match");
            return null;
        // build response by iterating through the search entries and parsing their HTML data
        List<ContactInfo> infos = new ArrayList<ContactInfo>();
        for (String entry : entries) {
            // parse wanted data and replace null values
            String name = replaceNullResult(LookupUtils.firstRegexResult(entry, NAME_REGEX, true));
            String address = replaceNullResult(LookupUtils.firstRegexResult(
                    entry, ADDRESS_REGEX, true));
            String number = replaceNullResult(LookupUtils.firstRegexResult(
                    entry, NUMBER_REGEX, true));
            // ignore entry if name or number is empty (should not occur)
            // missing addresses won't be a problem (but do occur)
            if (name.isEmpty() || number.isEmpty()) {
                continue;
            }

        // build and return contact list
        ContactInfo[] details = new ContactInfo[names.size()];
        for (int i = 0; i < names.size(); i++) {
            // figure out if we have a business contact
            boolean isBusiness = names.get(i).contains(BUSINESS_IDENTIFIER);
            boolean isBusiness = name.contains(BUSINESS_IDENTIFIER);
            // cleanup results
            String name = cleanupResult(names.get(i));
            String number = cleanupResult(numbers.get(i));
            String address = cleanupResult(addresses.get(i));
            name = cleanupResult(name);
            number = cleanupResult(number);
            address = cleanupResult(address);
            // set normalized and formatted number if we're not doing a reverse lookup
            if (lookupType != ContactBuilder.REVERSE_LOOKUP) {
                normalizedNumber = formattedNumber = number;
@@ -100,9 +99,9 @@ public final class AuskunftApi {
            builder.addWebsite(ContactBuilder.WebsiteUrl.createProfile(uri.toString()));
            builder.addAddress(ContactBuilder.Address.createFormattedHome(address));
            builder.setIsBusiness(isBusiness);
            details[i] = builder.build();
            infos.add(builder.build());
        }
        return details;
        return infos;
    }

    private static String cleanupResult(String result) {
@@ -119,4 +118,8 @@ public final class AuskunftApi {

        return result;
    }

    private static String replaceNullResult(String result) {
        return (result == null) ? "" : result;
    }
}
+4 −2
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import com.android.dialer.lookup.ContactBuilder;
import com.android.dialer.lookup.PeopleLookup;

import java.io.IOException;
import java.util.List;

public class AuskunftPeopleLookup extends PeopleLookup {
    private static final String TAG = AuskunftPeopleLookup.class.getSimpleName();
@@ -33,12 +34,13 @@ public class AuskunftPeopleLookup extends PeopleLookup {

    @Override
    public ContactInfo[] lookup(Context context, String filter) {
        ContactInfo[] infos = null;
        List<ContactInfo> infos = null;
        try {
            infos = AuskunftApi.query(filter, ContactBuilder.PEOPLE_LOOKUP, null, null);
        } catch (IOException e) {
            Log.e(TAG, "People lookup failed", e);
        }
        return infos;
        return (infos != null && !infos.isEmpty())
                ? infos.toArray(new ContactInfo[infos.size()]) : null;
    }
}
+3 −2
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import com.android.dialer.lookup.ContactBuilder;
import com.android.dialer.lookup.ReverseLookup;

import java.io.IOException;
import java.util.List;

public class AuskunftReverseLookup extends ReverseLookup {
    private static final String TAG = AuskunftReverseLookup.class.getSimpleName();
@@ -39,8 +40,8 @@ public class AuskunftReverseLookup extends ReverseLookup {
        }

        // query the API and return null if nothing found or general error
        ContactInfo[] infos = AuskunftApi.query(normalizedNumber, ContactBuilder.REVERSE_LOOKUP,
        List<ContactInfo> infos = AuskunftApi.query(normalizedNumber, ContactBuilder.REVERSE_LOOKUP,
                normalizedNumber, formattedNumber);
        return (infos != null && infos.length != 0) ? infos[0] : null;
        return (infos != null && !infos.isEmpty()) ? infos.get(0) : null;
    }
}