Skip to content

Instantly share code, notes, and snippets.

@asilichenko
Created September 11, 2023 05:38
Show Gist options
  • Save asilichenko/b0000eb1562c9e4e75b0d43d799260bc to your computer and use it in GitHub Desktop.
Save asilichenko/b0000eb1562c9e4e75b0d43d799260bc to your computer and use it in GitHub Desktop.
Obtain Place location by Google Maps CID
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.io.IOException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
public class MetadataExtractor {
private static final String META_ITEMPROP_NAME = "meta[itemprop=name]";
private static final String META_PROPERTY_OG_TITLE = "meta[property=og:title]";
private static final String META_PROPERTY_OG_SITE_NAME = "meta[property=og:site_name]";
private static final String CONTENT = "content";
private final OkHttpClient httpClient;
public MetadataExtractor() {
this.httpClient = new OkHttpClient();
}
/**
* Send request, retrieve response, parse response body to extract meta-data with place name.
*
* @param url <ul>
* <li>https://maps.google.com/?cid=13226830714359798441</li>
* <li>https://www.google.com/maps?cid=13226830714359798441</li>
* </ul>
*/
public Metadata extract(String url) {
// send request
final Request request = new Request.Builder().url(url).build();
try (final Response response = httpClient.newCall(request).execute()) {
if (!response.isSuccessful()) {
throw new RuntimeException("Request failed: " + response.code());
}
Metadata retval = null;
final ResponseBody body = response.body();
if (null != body) {
final String html = body.string();
final Document doc = Jsoup.parse(html);
final String name = getMetaContent(doc, META_ITEMPROP_NAME);
final String title = getMetaContent(doc, META_PROPERTY_OG_TITLE);
final String siteName = getMetaContent(doc, META_PROPERTY_OG_SITE_NAME);
retval = new Metadata(name, title, siteName);
}
return retval;
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
}
/**
* Get an attribute value from the first matched element that has the attribute.
*/
private String getMetaContent(Document doc, String selector) {
return doc.select(selector).attr(CONTENT);
}
public static class Metadata {
public final String name;
public final String title;
public final String siteName;
public Metadata(String name, String title, String siteName) {
this.name = name;
this.title = title;
this.siteName = siteName;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment