summaryrefslogtreecommitdiffstats
path: root/src/com/android/dialer/lookup/yellowpages/YellowPagesApi.java
blob: 75078b92f63c55ba1343ad6e8c2cb61a15449ff5 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
 * Copyright (C) 2014 Xiao-Long Chen <chillermillerlong@hotmail.com>
 *
 * 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.yellowpages;

import com.android.dialer.lookup.LookupSettings;

import android.content.Context;
import android.text.Html;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

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

    private static final String LOOKUP_URL_UNITED_STATES =
            "http://www.yellowpages.com/phone?phone_search_terms=";
    private static final String LOOKUP_URL_CANADA =
            "http://www.yellowpages.ca/search/si/1/";

    private String mProvider = null;
    private String mNumber = null;
    private String mOutput = null;
    private ContactInfo mInfo = null;
    private String mLookupUrl = null;

    public YellowPagesApi(Context context, String number) {
        mProvider = LookupSettings.getReverseLookupProvider(context);
        mNumber = number;

        if (mProvider.equals(LookupSettings.RLP_YELLOWPAGES)) {
            mLookupUrl = LOOKUP_URL_UNITED_STATES;
        } else if (mProvider.equals(LookupSettings.RLP_YELLOWPAGES_CA)) {
            mLookupUrl = LOOKUP_URL_CANADA;
        }
    }

    private void fetchPage() throws IOException {
        mOutput = httpGet(mLookupUrl + mNumber);
    }

    private String httpGet(String url) throws IOException {
        HttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet(url);

        HttpResponse response = client.execute(get);
        int status = response.getStatusLine().getStatusCode();

        // Android's org.apache.http doesn't have the RedirectStrategy class
        if (status == HttpStatus.SC_MOVED_PERMANENTLY
                || status == HttpStatus.SC_MOVED_TEMPORARILY) {
            Header[] headers = response.getHeaders("Location");

            if (headers != null && headers.length != 0) {
                String newUrl = headers[headers.length - 1].getValue();
                return httpGet(newUrl);
            } else {
                return null;
            }
        }

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        response.getEntity().writeTo(out);

        return new String(out.toByteArray());
    }

    private String getPhotoUrl(String website) throws IOException {
        String output = httpGet(website);

        Matcher m;

        Pattern regexGallery = Pattern.compile(
                "href=\"([^\"]+gallery\\?lid=[^\"]+)\"", Pattern.DOTALL);

        String galleryUrl = null;

        m = regexGallery.matcher(output);
        if (m.find()) {
            galleryUrl = "http://www.yellowpages.com" + m.group(1).trim();
        }

        if (galleryUrl == null) {
            return null;
        }

        // Get first image

        output = httpGet(galleryUrl);

        Pattern regexPhoto = Pattern.compile(
                "\"type\":\"image\",\"src\":\"([^\"]+)\"", Pattern.DOTALL);

        String photoUrl = null;

        m = regexPhoto.matcher(output);
        if (m.find()) {
            photoUrl = m.group(1).trim();
        }

        return photoUrl;
    }

    private String[] parseNameWebsiteUnitedStates() {
        Matcher m;

        Pattern regexNameAndWebsite = Pattern.compile(
                "<a href=\"([^>]+?)\"[^>]+?class=\"url[^>]+?>([^<]+)</a>",
                Pattern.DOTALL);
        String name = null;
        String website = null;

        m = regexNameAndWebsite.matcher(mOutput);
        if (m.find()) {
            website = m.group(1).trim();
            name = m.group(2).trim();
        }

        return new String[] { name, website };
    }

    private String[] parseNameWebsiteCanada() {
        Matcher m;

        Pattern regexNameAndWebsite = Pattern.compile(
                "class=\"ypgListingTitleLink utagLink\".*?href=\"(.*?)\">"
                        + "(<span\\s+class=\"listingTitle\">.*?</span>)",
                Pattern.DOTALL);
        String name = null;
        String website = null;

        m = regexNameAndWebsite.matcher(mOutput);
        if (m.find()) {
            website = m.group(1).trim();
            name = m.group(2).trim();
        }

        if (name != null) {
            name = Html.fromHtml(name).toString().trim();
        }

        if (website != null) {
            website = "http://www.yellowpages.ca" + website;
        }

        return new String[] { name, website };
    }

    private String parseNumberUnitedStates() {
        Matcher m;

        Pattern regexPhoneNumber = Pattern.compile(
                "business-phone.*?>\n*([^\n<]+)\n*<", Pattern.DOTALL);
        String phoneNumber = null;

        m = regexPhoneNumber.matcher(mOutput);
        if (m.find()) {
            phoneNumber = m.group(1).trim();
        }

        return phoneNumber;
    }

    private String parseNumberCanada() {
        Matcher m;

        Pattern regexPhoneNumber = Pattern.compile(
                "<div\\s+class=\"phoneNumber\">(.*?)</div>", Pattern.DOTALL);
        String phoneNumber = null;

        m = regexPhoneNumber.matcher(mOutput);
        if (m.find()) {
            phoneNumber = m.group(1).trim();
        }

        return phoneNumber;
    }

    private String parseAddressUnitedStates() {
        Matcher m;

        Pattern regexAddressStreet = Pattern.compile(
                "street-address.*?>\n*([^\n<]+)\n*<", Pattern.DOTALL);
        Pattern regexAddressCity = Pattern.compile(
                "locality.*?>\n*([^\n<]+)\n*<", Pattern.DOTALL);
        Pattern regexAddressState = Pattern.compile(
                "region.*?>\n*([^\n<]+)\n*<", Pattern.DOTALL);
        Pattern regexAddressZip = Pattern.compile(
                "postal-code.*?>\n*([^\n<]+)\n*<", Pattern.DOTALL);

        String addressStreet = null;
        String addressCity = null;
        String addressState = null;
        String addressZip = null;

        m = regexAddressStreet.matcher(mOutput);
        if (m.find()) {
            addressStreet = m.group(1).trim();
            if (addressStreet.endsWith(",")) {
                addressStreet = addressStreet.substring(0,
                        addressStreet.length() - 1);
            }
        }

        m = regexAddressCity.matcher(mOutput);
        if (m.find()) {
            addressCity = m.group(1).trim();
        }

        m = regexAddressState.matcher(mOutput);
        if (m.find()) {
            addressState = m.group(1).trim();
        }

        m = regexAddressZip.matcher(mOutput);
        if (m.find()) {
            addressZip = m.group(1).trim();
        }

        StringBuilder sb = new StringBuilder();

        if (addressStreet != null && addressStreet.length() != 0) {
            sb.append(addressStreet);
        }
        if (addressCity != null && addressCity.length() != 0) {
            sb.append(", ");
            sb.append(addressCity);
        }
        if (addressState != null && addressState.length() != 0) {
            sb.append(", ");
            sb.append(addressState);
        }
        if (addressZip != null && addressZip.length() != 0) {
            sb.append(", ");
            sb.append(addressZip);
        }

        String address = sb.toString();
        if (address.length() == 0) {
            address = null;
        }

        return address;
    }

    private String parseAddressCanada() {
        Matcher m;

        Pattern regexAddress = Pattern.compile(
                "<div\\s+class=\"address\">(.*?)</div>", Pattern.DOTALL);
        String address = null;

        m = regexAddress.matcher(mOutput);
        if (m.find()) {
            address = m.group(1).trim();
        }

        if (address != null) {
            address = Html.fromHtml(address).toString().trim();
        }

        return address;
    }

    private void buildContactInfo() throws IOException {
        Matcher m;

        String name = null;
        String website = null;
        String phoneNumber = null;
        String address = null;
        String photoUrl = null;

        if (mProvider.equals(LookupSettings.RLP_YELLOWPAGES)) {
            String[] ret = parseNameWebsiteUnitedStates();
            name = ret[0];
            website = ret[1];
            phoneNumber = parseNumberUnitedStates();
            address = parseAddressUnitedStates();
            if (website != null) {
                photoUrl = getPhotoUrl(website);
            }
        } else if (mProvider.equals(LookupSettings.RLP_YELLOWPAGES_CA)) {
            String[] ret = parseNameWebsiteCanada();
            name = ret[0];
            website = ret[1];
            phoneNumber = parseNumberCanada();
            address = parseAddressCanada();
            // AFAIK, Canada's YellowPages doesn't have photos
        }

        ContactInfo info = new ContactInfo();
        info.name = name;
        info.address = address;
        info.formattedNumber = phoneNumber != null ? phoneNumber : mNumber;
        info.website = website;
        info.photoUrl = photoUrl;
        mInfo = info;
    }

    public ContactInfo getContactInfo() throws IOException {
        if (mInfo == null) {
            fetchPage();

            buildContactInfo();
        }

        return mInfo;
    }

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