Ви можете використовувати цей API для отримання загальнодоступної інформації користувача instagram:
https://api.lityapp.com/instagrams/thebrainscoop?limit=2
Якщо не встановити граничний параметр, публікації за замовчуванням обмежені на 12.
Це api було зроблено у SpringBoot з HtmlUnit, як ви бачите у коді:
public JSONObject getPublicInstagramByUserName(String userName, Integer limit) {
String html;
WebClient webClient = new WebClient();
try {
webClient.getOptions().setCssEnabled(false);
webClient.getOptions().setJavaScriptEnabled(false);
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getCookieManager().setCookiesEnabled(true);
Page page = webClient.getPage("https://www.instagram.com/" + userName);
WebResponse response = page.getWebResponse();
html = response.getContentAsString();
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException("Ocorreu um erro no Instagram");
}
String prefix = "static/bundles/es6/ProfilePageContainer.js";
String sufix = "\"";
String script = html.substring(html.indexOf(prefix));
script = script.substring(0, script.indexOf(sufix));
try {
Page page = webClient.getPage("https://www.instagram.com/" + script);
WebResponse response = page.getWebResponse();
script = response.getContentAsString();
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException("Ocorreu um erro no Instagram");
}
prefix = "l.pagination},queryId:\"";
String queryHash = script.substring(script.indexOf(prefix) + prefix.length());
queryHash = queryHash.substring(0, queryHash.indexOf(sufix));
prefix = "<script type=\"text/javascript\">window._sharedData = ";
sufix = ";</script>";
html = html.substring(html.indexOf(prefix) + prefix.length());
html = html.substring(0, html.indexOf(sufix));
JSONObject json = new JSONObject(html);
JSONObject entryData = json.getJSONObject("entry_data");
JSONObject profilePage = (JSONObject) entryData.getJSONArray("ProfilePage").get(0);
JSONObject graphql = profilePage.getJSONObject("graphql");
JSONObject user = graphql.getJSONObject("user");
JSONObject response = new JSONObject();
response.put("id", user.getString("id"));
response.put("username", user.getString("username"));
response.put("fullName", user.getString("full_name"));
response.put("followedBy", user.getJSONObject("edge_followed_by").getLong("count"));
response.put("following", user.getJSONObject("edge_follow").getLong("count"));
response.put("isBusinessAccount", user.getBoolean("is_business_account"));
response.put("photoUrl", user.getString("profile_pic_url"));
response.put("photoUrlHD", user.getString("profile_pic_url_hd"));
JSONObject edgeOwnerToTimelineMedia = user.getJSONObject("edge_owner_to_timeline_media");
JSONArray posts = new JSONArray();
try {
loadPublicInstagramPosts(webClient, queryHash, user.getString("id"), posts, edgeOwnerToTimelineMedia, limit == null ? 12 : limit);
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException("Você fez muitas chamadas, tente mais tarde");
}
response.put("posts", posts);
return response;
}
private void loadPublicInstagramPosts(WebClient webClient, String queryHash, String userId, JSONArray posts, JSONObject edgeOwnerToTimelineMedia, Integer limit) throws IOException {
JSONArray edges = edgeOwnerToTimelineMedia.getJSONArray("edges");
for (Object elem : edges) {
if (limit != null && posts.length() == limit) {
return;
}
JSONObject node = ((JSONObject) elem).getJSONObject("node");
if (node.getBoolean("is_video")) {
continue;
}
JSONObject post = new JSONObject();
post.put("id", node.getString("id"));
post.put("shortcode", node.getString("shortcode"));
JSONArray captionEdges = node.getJSONObject("edge_media_to_caption").getJSONArray("edges");
if (captionEdges.length() > 0) {
JSONObject captionNode = ((JSONObject) captionEdges.get(0)).getJSONObject("node");
post.put("caption", captionNode.getString("text"));
} else {
post.put("caption", (Object) null);
}
post.put("photoUrl", node.getString("display_url"));
JSONObject dimensions = node.getJSONObject("dimensions");
post.put("photoWidth", dimensions.getLong("width"));
post.put("photoHeight", dimensions.getLong("height"));
JSONArray thumbnailResources = node.getJSONArray("thumbnail_resources");
JSONArray thumbnails = new JSONArray();
for (Object elem2 : thumbnailResources) {
JSONObject obj = (JSONObject) elem2;
JSONObject thumbnail = new JSONObject();
thumbnail.put("photoUrl", obj.getString("src"));
thumbnail.put("photoWidth", obj.getLong("config_width"));
thumbnail.put("photoHeight", obj.getLong("config_height"));
thumbnails.put(thumbnail);
}
post.put("thumbnails", thumbnails);
posts.put(post);
}
JSONObject pageInfo = edgeOwnerToTimelineMedia.getJSONObject("page_info");
if (!pageInfo.getBoolean("has_next_page")) {
return;
}
String endCursor = pageInfo.getString("end_cursor");
String variables = "{\"id\":\"" + userId + "\",\"first\":12,\"after\":\"" + endCursor + "\"}";
String url = "https://www.instagram.com/graphql/query/?query_hash=" + queryHash + "&variables=" + URLEncoder.encode(variables, "UTF-8");
Page page = webClient.getPage(url);
WebResponse response = page.getWebResponse();
String content = response.getContentAsString();
JSONObject json = new JSONObject(content);
loadPublicInstagramPosts(webClient, queryHash, userId, posts, json.getJSONObject("data").getJSONObject("user").getJSONObject("edge_owner_to_timeline_media"), limit);
}
Це приклад відповіді:
{
"id": "290482318",
"username": "thebrainscoop",
"fullName": "Official Fan Page",
"followedBy": 1023,
"following": 6,
"isBusinessAccount": false,
"photoUrl": "https://scontent-gru2-1.cdninstagram.com/vp/447ffd0262082f373acf3d467435f130/5C709C77/t51.2885-19/11351770_612904665516559_678168252_a.jpg",
"photoUrlHD": "https://scontent-gru2-1.cdninstagram.com/vp/447ffd0262082f373acf3d467435f130/5C709C77/t51.2885-19/11351770_612904665516559_678168252_a.jpg",
"posts": [
{
"id": "1430331382090378714",
"shortcode": "BPZjtBUly3a",
"caption": "If I have any active followers anymore; hello! I'm Brianna, and I created this account when I was just 12 years old to show my love for The Brain Scoop. I'm now nearly finished high school, and just rediscovered it. I just wanted to see if anyone is still active on here, and also correct some of my past mistakes - being a child at the time, I didn't realise I had to credit artists for their work, so I'm going to try to correct that post haste. Also; the font in my bio is horrendous. Why'd I think that was a good idea? Anyway, this is a beautiful artwork of the long-tailed pangolin by @chelsealinaeve . Check her out!",
"photoUrl": "https://scontent-gru2-1.cdninstagram.com/vp/ab823331376ca46136457f4654bf2880/5CAD48E4/t51.2885-15/e35/16110915_400942200241213_3503127351280009216_n.jpg",
"photoWidth": 640,
"photoHeight": 457,
"thumbnails": [
{
"photoUrl": "https://scontent-gru2-1.cdninstagram.com/vp/43b195566d0ef2ad5f4663ff76d62d23/5C76D756/t51.2885-15/e35/c91.0.457.457/s150x150/16110915_400942200241213_3503127351280009216_n.jpg",
"photoWidth": 150,
"photoHeight": 150
},
{
"photoUrl": "https://scontent-gru2-1.cdninstagram.com/vp/ae39043a7ac050c56d741d8b4355c185/5C93971C/t51.2885-15/e35/c91.0.457.457/s240x240/16110915_400942200241213_3503127351280009216_n.jpg",
"photoWidth": 240,
"photoHeight": 240
},
{
"photoUrl": "https://scontent-gru2-1.cdninstagram.com/vp/ae7a22d09e3ef98d0a6bbf31d621a3b7/5CACBBA6/t51.2885-15/e35/c91.0.457.457/s320x320/16110915_400942200241213_3503127351280009216_n.jpg",
"photoWidth": 320,
"photoHeight": 320
},
{
"photoUrl": "https://scontent-gru2-1.cdninstagram.com/vp/1439dc72b70e7c0c0a3afcc30970bb13/5C8E2923/t51.2885-15/e35/c91.0.457.457/16110915_400942200241213_3503127351280009216_n.jpg",
"photoWidth": 480,
"photoHeight": 480
},
{
"photoUrl": "https://scontent-gru2-1.cdninstagram.com/vp/1439dc72b70e7c0c0a3afcc30970bb13/5C8E2923/t51.2885-15/e35/c91.0.457.457/16110915_400942200241213_3503127351280009216_n.jpg",
"photoWidth": 640,
"photoHeight": 640
}
]
},
{
"id": "442527661838057235",
"shortcode": "YkLJBXJD8T",
"caption": null,
"photoUrl": "https://scontent-gru2-1.cdninstagram.com/vp/dc94b38da679826b9ac94ccd2bcc4928/5C7CDF93/t51.2885-15/e15/11327349_860747310663863_2105199307_n.jpg",
"photoWidth": 612,
"photoHeight": 612,
"thumbnails": [
{
"photoUrl": "https://scontent-gru2-1.cdninstagram.com/vp/c1153c6513c44a6463d897e14b2d8f06/5CB13ADD/t51.2885-15/e15/s150x150/11327349_860747310663863_2105199307_n.jpg",
"photoWidth": 150,
"photoHeight": 150
},
{
"photoUrl": "https://scontent-gru2-1.cdninstagram.com/vp/47e60ec8bca5a1382cd9ac562439d48c/5CAE6A82/t51.2885-15/e15/s240x240/11327349_860747310663863_2105199307_n.jpg",
"photoWidth": 240,
"photoHeight": 240
},
{
"photoUrl": "https://scontent-gru2-1.cdninstagram.com/vp/da0ee5b666ab40e4adc1119e2edca014/5CADCB59/t51.2885-15/e15/s320x320/11327349_860747310663863_2105199307_n.jpg",
"photoWidth": 320,
"photoHeight": 320
},
{
"photoUrl": "https://scontent-gru2-1.cdninstagram.com/vp/02ee23571322ea8d0992e81e72f80ef2/5C741048/t51.2885-15/e15/s480x480/11327349_860747310663863_2105199307_n.jpg",
"photoWidth": 480,
"photoHeight": 480
},
{
"photoUrl": "https://scontent-gru2-1.cdninstagram.com/vp/dc94b38da679826b9ac94ccd2bcc4928/5C7CDF93/t51.2885-15/e15/11327349_860747310663863_2105199307_n.jpg",
"photoWidth": 640,
"photoHeight": 640
}
]
}
]
}