3 ms·
I built a simple bash/javascript to get the AMP URL for a given URL, did reduce my data usage by tons. Look here: https://developers.google.com/apis-explorer/?
by whyagaindavid 9y ago
I built a simple bash/javascript to get the AMP URL for a given URL, did reduce my data usage by tons. Look here:
https://developers.google.com/apis-explorer/?hl=en_US#p/acceleratedmobilepageurl/v1/acceleratedmobilepageurl.ampUrls.batchGet https://developers.google.com/apis-explorer/?hl=en_US#p/acce...
curl -X POST -H "Content-Type: application/json" -d "{urls: ['$1'] }"
https://content-acceleratedmobilepageurl.googleapis.com
/v1/ampUrls:batchGet?key=YOUR_AMP_API_KEY
For android app use:
public static String GET(String url){
String result = "";
String ampString = "https://content-acceleratedmobilepageurl.googleapis.com/v1/ampUrls:batchGet?key=YOUR_AMP_API_KEY";
HttpURLConnection conn = null;
try {
URL ampurl = new URL(ampString);
conn = (HttpURLConnection) ampurl.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("urls", url);
OutputStream os = conn.getOutputStream();
os.write(jsonObject.toString().getBytes("UTF-8"));
os.close();
InputStream istream = null;
istream = conn.getInputStream();
InputStream in = new BufferedInputStream(istream);
if(in != null){
result = convertInputStreamToString(in);
}
}
catch (Exception e) {
Log.d("News exception", result);
}finally {
// Close Stream TODO
// and disconnect HTTPS connection.
if (conn != null) {
conn.disconnect();
}
}
private static String convertInputStreamToString(InputStream inputStream) throws IOException {
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
String line = "";
String result = "";
String id = "";
String ampUrls = "";
while((line = bufferedReader.readLine()) != null){
result += line;
}
inputStream.close();
try {
JSONObject jsonObj = new JSONObject(result);
// check if jsonObj has ampUrls string - how to do it?
if (!jsonObj.isNull("ampUrls")){
JSONArray contacts = jsonObj.getJSONArray("ampUrls");
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
id = c.getString("cdnAmpUrl");
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return id;
} //end of convertInputStreamToString
- niutech 9y agoYou can try the AMP Desktop Extension, which uses this API: https://chrome.google.com/webstore/detail/amp-browser-extension/mccnchmofleakpdohkmljohfckgpdehb https://chrome.google.com/webstore/detail/amp-browser-extens...