1
\$\begingroup\$

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);
    }
\$\endgroup\$
3
  • \$\begingroup\$ Could you please elaborate a bit: what is the desired output and what is the current output? Thanks. \$\endgroup\$
    – alecxe
    Jul 13, 2017 at 14:20
  • \$\begingroup\$ @alecxe Desired output and current output is the same, the program is working fine, I'm just wondering if maybe I can refactor something and if the style of code is alright since I have never work in a team and in my opinion my code tends to be a little messy \$\endgroup\$
    – Vhalad
    Jul 13, 2017 at 15:13
  • \$\begingroup\$ And to explain a little bit: Instagram shows photos in a grid of 4 rows and 3 columns, this algorithm checks if the first grid is enough or it has to load more grids and then iterates over the grids and gets the link of every photo \$\endgroup\$
    – Vhalad
    Jul 13, 2017 at 15:17

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.