I wrote this methods that store instagram links in an ArrayList based on a hashtag or list of hashtags and the number of desired links. Something to notice is that if I tell the program to get 10 links it will get 12 links instead because of how Instagram places the photos in a grid style but after 10 iterations it stops and the ArrayList is cleared, is this considered bad or it doesnt matter?
private void getLinks(TextArea logbox) throws InterruptedException {
utils.addLogLine(logbox, "[+] Obteniendo links con los tags especificados...");
int numberOfRows = (int) Math.ceil(((float) numberOfLikes / 3));
int numberOfRowsPerTag = (int) Math.ceil(((float) numberOfRows / tags.size()));
//Page needs to load more images after first load
if (numberOfRowsPerTag > 4) {
for (int x = 0; x < tags.size(); x++) {
searchTag(tags.get(x));
int numberOfPageDowns = (int) Math.ceil(((float) numberOfRowsPerTag / 4));
WebElement loadMoreBtn = utils.fluentWait(By.xpath("//*[@id=\"react-root\"]/section/main/article/div[2]/a"), driver);
loadMoreBtn.click();
WebElement pageBody = utils.fluentWait(By.tagName("body"), driver);
for (int y = 0; y < numberOfPageDowns; y++) {
pageBody.sendKeys(Keys.PAGE_DOWN);
pageBody.sendKeys(Keys.PAGE_DOWN);
utils.waitGivenSeconds(2);
}
for (int z = 0; z < numberOfRowsPerTag; z++) {
int row = z + 1;
getPhotolinksFromRow(row);
}
utils.addLogLine(logbox, "[+] Links para #" + tags.get(x) + " añadidos!");
}
} else {
for (int x = 0; x < tags.size(); x++) {
searchTag(tags.get(x));
for (int z = 0; z < numberOfRowsPerTag; z++) {
int row = z + 1;
getPhotolinksFromRow(row);
}
utils.addLogLine(logbox, "[+] Links para #" + tags.get(x) + " añadidos!");
}
}
}
private void getPhotolinksFromRow(int row) {
WebElement firstPhoto = utils.fluentWait(By.xpath("//*[@id=\"react-root\"]/section/main/article/div[2]/div[1]/div[" + row + "]/div[1]/a"), driver);
WebElement secondPhoto = utils.fluentWait(By.xpath("//*[@id=\"react-root\"]/section/main/article/div[2]/div[1]/div[" + row + "]/div[2]/a"), driver);
WebElement thirdPhoto = utils.fluentWait(By.xpath("//*[@id=\"react-root\"]/section/main/article/div[2]/div[1]/div[" + row + "]/div[3]/a"), driver);
String firstLink = firstPhoto.getAttribute("href");
String secondLink = secondPhoto.getAttribute("href");
String thirdLink = thirdPhoto.getAttribute("href");
String[] firstLinkStripped = firstLink.split("\\?");
String[] secondLinkStripped = secondLink.split("\\?");
String[] thirdLinkStripped = thirdLink.split("\\?");
List<String> tempLinks = Arrays.asList(firstLinkStripped[0], secondLinkStripped[0], thirdLinkStripped[0]);
links.addAll(tempLinks);
}