diff options
| author | Martin Herndl <martin.herndl@gmail.com> | 2015-11-30 14:33:33 +0100 |
|---|---|---|
| committer | doc HD <doc.divxm@gmail.com> | 2015-12-03 12:59:03 +0200 |
| commit | 0f06be93077fd583e35f34db81b689743dfd1dc1 (patch) | |
| tree | be5bb3f0cb606fc1495b0054b160b44786d510e7 | |
| parent | 1f76cd3198ba1a37f5f574656cbdb4aa9cd942c3 (diff) | |
Lookup: improve "Auskunft" (AT) API.lp5.1
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
3 files changed, 39 insertions, 33 deletions
diff --git a/src/com/android/dialer/lookup/auskunft/AuskunftApi.java b/src/com/android/dialer/lookup/auskunft/AuskunftApi.java index 91c2ecd4a..7b7c98cce 100644 --- a/src/com/android/dialer/lookup/auskunft/AuskunftApi.java +++ b/src/com/android/dialer/lookup/auskunft/AuskunftApi.java @@ -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 and return contact list - ContactInfo[] details = new ContactInfo[names.size()]; - for (int i = 0; i < names.size(); i++) { + // 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; + } // 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; + } } diff --git a/src/com/android/dialer/lookup/auskunft/AuskunftPeopleLookup.java b/src/com/android/dialer/lookup/auskunft/AuskunftPeopleLookup.java index 2898fc64d..970f2d0ef 100644 --- a/src/com/android/dialer/lookup/auskunft/AuskunftPeopleLookup.java +++ b/src/com/android/dialer/lookup/auskunft/AuskunftPeopleLookup.java @@ -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; } } diff --git a/src/com/android/dialer/lookup/auskunft/AuskunftReverseLookup.java b/src/com/android/dialer/lookup/auskunft/AuskunftReverseLookup.java index 894fd3b4c..383b4d53d 100644 --- a/src/com/android/dialer/lookup/auskunft/AuskunftReverseLookup.java +++ b/src/com/android/dialer/lookup/auskunft/AuskunftReverseLookup.java @@ -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; } } |
