Skip to content

Commit

Permalink
Merge pull request #53 from igor-suhorukov/master
Browse files Browse the repository at this point in the history
fix media parsing ( issue #51)  in case of image/video carousel
  • Loading branch information
raiym authored Nov 14, 2017
2 parents 8c210e0 + a7fa476 commit ba99927
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
31 changes: 16 additions & 15 deletions src/main/java/me/postaddict/instagram/scraper/domain/Media.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,22 +141,20 @@ public static Media fromMediaPage(Map pageMap) {
instance.videoUrls.standard = (String) pageMap.get("video_url");
instance.videoViews = ((Double)pageMap.get("video_view_count")).intValue();
}
if(pageMap.containsKey("carousel_media")){
if(pageMap.containsKey("edge_sidecar_to_children")){
instance.type = TYPE_CAROUSEL;
instance.carouselMedia = new ArrayList<CarouselMedia>();
for(Map<String, Object> carouselMap : ((List<Map>) pageMap.get("carousel_media"))){
List<Map<String, Object>> carousel = (List<Map<String, Object>>) ((Map) pageMap.get("edge_sidecar_to_children")).get("edges");
for(Map<String, Object> carouselMap : carousel){
carouselMap = (Map<String, Object>) carouselMap.get("node");
CarouselMedia carouselMedia = new CarouselMedia();
carouselMedia.type = (String) carouselMap.get("type");
carouselMedia.type = (String) carouselMap.get("__typename");

if(carouselMap.containsKey("images")){
Map carouselImages = (Map) carouselMap.get("images");
fillCarouselImageUrls(carouselMedia, (String)((Map)carouselImages.get("standard_resolution")).get("url"));
}
if (carouselMedia.type.equals(TYPE_VIDEO) && carouselMap.containsKey("videos")) {
Map carouselVideos = (Map) carouselMap.get("videos");
carouselMedia.videoUrls.low = (String) ((Map) carouselVideos.get("low_resolution")).get("url");
carouselMedia.videoUrls.standard = (String) ((Map) carouselVideos.get("standard_resolution")).get("url");
carouselMedia.videoUrls.lowBandwidth = (String) ((Map) carouselVideos.get("low_bandwidth")).get("url");
if (carouselMedia.type.equals(TYPE_VIDEO) && carouselMap.containsKey("video_url")) {
carouselMedia.videoUrls.standard = (String) carouselMap.get("video_url");
carouselMedia.videoViews = ((Double)carouselMap.get("video_view_count")).intValue();
} else {
fillCarouselImageUrls(carouselMedia, (String)((Map)carouselMap).get("display_url"));
}
instance.carouselMedia.add(carouselMedia);
}
Expand All @@ -176,9 +174,12 @@ public static Media fromMediaPage(Map pageMap) {
}
instance.likesCount = ((Double)((Map) pageMap.get("edge_media_preview_like")).get("count")).intValue();
fillImageUrls(instance, (String) pageMap.get("display_url"));
String caption = (String)((Map)((Map)((List)((Map)pageMap.get("edge_media_to_caption")).get("edges")).get(0)).get("node")).get("text");
if (caption != null) {
instance.caption = caption;
List captions = (List) ((Map) pageMap.get("edge_media_to_caption")).get("edges");
if(captions.size()>0) {
String caption = (String) ((Map) ((Map) captions.get(0)).get("node")).get("text");
if (caption != null) {
instance.caption = caption;
}
}
if (pageMap.containsKey("location") && pageMap.get("location") != null) {
Map location = (Map) pageMap.get("location");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,19 @@ public void testGetMediaByCode() throws Exception {
System.out.println(media);
}

@Test
public void testGetMediaByCodeCarousel() throws Exception {
Media media = client.getMediaByCode("BaKLiFugkQa");
assertEquals("kevin", media.owner.username);
assertTrue(checkMedia(media));
assertEquals(4, media.carouselMedia.size());
assertEquals("https://scontent.cdninstagram.com/t/22351958_144137916205565_6923513639366295552_n.jpg", media.carouselMedia.get(0).imageUrls.high);
assertEquals("https://scontent.cdninstagram.com/t/22352110_177635979467060_738026920783904768_n.jpg", media.carouselMedia.get(1).imageUrls.high);
assertEquals("https://scontent.cdninstagram.com/t/22352158_840551796104469_8224611081893445632_n.jpg", media.carouselMedia.get(2).imageUrls.high);
assertEquals("https://scontent.cdninstagram.com/t/22427115_152159245386313_6593256479942246400_n.jpg", media.carouselMedia.get(3).imageUrls.high);
System.out.println(media);
}

@Test
public void testGetLocationMediasById() throws Exception {
String locationId = "17326249";
Expand Down

0 comments on commit ba99927

Please sign in to comment.