summaryrefslogtreecommitdiff
path: root/java/com/android/dialer/lookup/dastelefonbuch/TelefonbuchApi.java
blob: ab1eb408516c6aa5099dbba3b9ce58884b462bb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
 * Copyright (C) 2014 Danny Baumann <dannybaumann@web.de>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.dialer.lookup.dastelefonbuch;

import android.content.Context;
import android.net.Uri;

import com.android.dialer.lookup.LookupUtils;

import java.io.IOException;

public class TelefonbuchApi {
  private static final String TAG = TelefonbuchApi.class.getSimpleName();

  private static final String REVERSE_LOOKUP_URL =
      "https://www.dastelefonbuch.de/?s=a20000" +
      "&cmd=search&sort_ok=0&sp=55&vert_ok=0&aktion=23";

  private static String NAME_REGEX ="<a id=\"name0.*?>\\s*\n?(.*?)\n?\\s*</a>";
  private static String NUMBER_REGEX = "<span\\s+class=\"ico fon.*>.*<span>(.*?)</span><br/>";
  private static String ADDRESS_REGEX = "<address.*?>\n?(.*?)</address>";

  private TelefonbuchApi() {
  }

  public static ContactInfo reverseLookup(Context context, String number) throws IOException {
    Uri uri = Uri.parse(REVERSE_LOOKUP_URL)
        .buildUpon()
        .appendQueryParameter("kw", number)
        .build();
    // Cut out everything we're not interested in (scripts etc.) to
    // speed up the subsequent matching.
    String output = LookupUtils.firstRegexResult(
        LookupUtils.httpGet(uri.toString(), null), ": Treffer(.*)Ende Treffer", true);

    String name = parseValue(output, NAME_REGEX, true, false);
    if (name == null) {
      return null;
    }

    String phoneNumber = parseValue(output, NUMBER_REGEX, false, true);
    String address = parseValue(output, ADDRESS_REGEX, true, true);

    ContactInfo info = new ContactInfo();
    info.name = name;
    info.address = address;
    info.formattedNumber = phoneNumber != null ? phoneNumber : number;
    info.website = uri.toString();

    return info;
  }

  private static String parseValue(String output, String regex,
      boolean dotall, boolean removeSpans) {
    String result = LookupUtils.firstRegexResult(output, regex, dotall);
    if (result != null && removeSpans) {
      // completely remove hidden spans (including contents) ...
      result = result.replaceAll("<span class=\"hide\".*?\\/span>", "");
      // ... and remove span wrappers around data content
      result = result.replaceAll("</?span.*?>", "");
    }
    return LookupUtils.fromHtml(result);
  }

  public static class ContactInfo {
    String name;
    String address;
    String formattedNumber;
    String website;
  }
}