<reponame>PSPMonster/Facebook-LeadsGorilla-Generator
from dearpygui.simple import window
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import *
from dearpygui.core import set_value
from selenium.webdriver.common.keys import Keys
import time
import os

# set propert chromdriver.exe path
current_directory = os.getcwd()
add = "\\chromedriver.exe"
fullDir = current_directory + add

# user data
mail = "YOUR_EMAIL_HERE"
password = "<PASSWORD>"


# login
def Login():
    username = driver.find_element_by_id('user-name')
    username.send_keys(mail)
    username = driver.find_element_by_id('user-password')
    username.send_keys(password)
    time.sleep(0.2)
    loginbutton = driver.find_element_by_class_name('btn-lg')
    loginbutton.click()


# input phrases
def ReadPhrases(sender, data):
    global input_phrases
    input_value = core.get_value("<<< ")
    input_phrases = input_value.split(' / ')
    if not len(input_value):
        with window("Uwaga!", width=240, height=150, no_resize=True,  no_collapse=True):
            core.add_text("\nFraza pusta badz \nnie zaakceptowana!", color=[255, 0, 0])
    else:
        print("Input phrases: ")
        print(input_phrases)


# input city
def ReadCities():
    global input_cities
    input_value1 = core.get_value("<<<")
    input_cities = input_value1.split(' / ')
    print("Input cities: ")
    print(input_cities)


# clicking, sorting, searching
def GenerateContacts():
    percentages = 1 / len(input_cities)
    isFirst = True
    isFirstClick = True
    counter = 0

    for f in input_phrases:
        overallResults = 0
        phrases.send_keys(f)
        for m in input_cities:
            set_value("Progres", value=counter)
            print('Now ' + m)
            counter += percentages
            print("Progres", counter)
            location.send_keys(m + ', YOUR_COUNTRY')
            time.sleep(1.5)
            autoSuggestionList = driver.find_elements(By.XPATH, "/html/body/div[5]")

            for ele in autoSuggestionList:
                if ele.text == m + 'YOUR_COUNTRY':
                    print(m + 'YOUR_COUNTRY')
                    ele.click()
                    break

            time.sleep(0.5)
            search.click()

            results = wait2.until(EC.visibility_of_element_located(
                (By.XPATH, "/html/body/div[2]/div/strong/div[2]/section/div[2]/div[2]/p/label"))).get_attribute("value")

            try:
                results = int(float(resultsCount.text))
            except:
                return

            if isFirst:
                emailWait = wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/div/strong/div["
                                                                             "2]/section/div[2]/div[3]/div/div/div["
                                                                             "1]/div/div/div[6]/span")))
                emailWait.click()
                isFirst = False

            if isFirst and (results <= 5 or results == '' or results == ' '):
                emailWait = wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/div/strong/div["
                                                                             "2]/section/div[2]/div[3]/div/div/div["
                                                                             "1]/div/div/div[6]/span")))
                emailWait.click()
                print("is first and less than 5 results")
                location.clear()
                isFirst = False
                continue

            if results <= 5 or results == '' or results == ' ':
                print("less than 5 results")
                location.clear()
                continue
            else:
                loadMore = wait.until(EC.element_to_be_clickable((By.ID, "more-btn")))
                loadMore.click()

            while True:
                results = int(float(resultsCount.text))
                print(results)

                try:
                    if results >= 350:
                        haha = driver.find_element_by_id('error')
                    else:
                        loadMore = wait.until(EC.element_to_be_clickable((By.ID, "more-btn")))
                        loadMore.click()
                except:
                    overallResults += results
                    driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.HOME)
                    time.sleep(1.3)

                    if results < 5:
                        print("less than 5 results")
                        location.clear()
                    else:
                        if isFirstClick:
                            selectAll.click()
                            time.sleep(0.3)
                            export.click()
                            time.sleep(3)
                            location.clear()
                            isFirstClick = False
                        else:
                            selectAll.click()
                            time.sleep(0.3)
                            selectAll.click()
                            time.sleep(0.3)
                            export.click()
                            time.sleep(3)
                            location.clear()
                    break
        print("All results for", f, overallResults)
        set_value("Progres", value=0)
        phrases.clear()


# set all cities city input
def default():
    input_cities = arrCities
    print("Default search")
    print(input_cities)
    simple.set_item_height("<<<", height=200)


# information about cities
cities = """YOUR_CITIES_HERE (PATERN: Warsaw / Cracow / ... / .."""
arrCities = cities.split(' / ')
howManyCities = len(arrCities)

# phrases
phrases = "YOUR_PHRASES_HERE (PATERN: Monke / Car / Tree / .."
arrPhrases = phrases.split(' / ')

# setting and launching driver
driver = webdriver.Chrome(fullDir)
driver.maximize_window()
driver.get("https://app.leadsgorilla.net/fbsearch")

# Fluent Wait
wait = WebDriverWait(driver, 11, poll_frequency=0.5, ignored_exceptions=[ElementNotVisibleException, ElementNotSelectableException, StaleElementReferenceException, TimeoutException])
wait2 = WebDriverWait(driver, 14, poll_frequency=0.5, ignored_exceptions=[ElementNotVisibleException, ElementNotSelectableException, StaleElementReferenceException, TimeoutException])

Login()

# button connection
phrases = driver.find_element_by_id('search_keyword')
location = driver.find_element_by_id('search_city')
search = driver.find_element_by_id('search-leads')
email = driver.find_element(By.XPATH, "/html/body/div[2]/div/strong/div[2]/section/div[2]/div[3]/div/div/div[1]/div/div/div[6]/span")
selectAll = driver.find_element(By.XPATH, "/html/body/div[2]/div/strong/div[2]/section/div[2]/div[3]/div/div/div[2]/div[2]/button[3]")
export = driver.find_element(By.XPATH, "/html/body/div[2]/div/strong/div[2]/section/div[2]/div[3]/div/div/div[2]/div[2]/button[2]")
resultsCount = driver.find_element(By.XPATH, "/html/body/div[2]/div/strong/div[2]/section/div[2]/div[2]/p/label")



#GUI IN POLISH!!
from dearpygui import core, simple

# setting up core window
core.set_main_window_size(535, 470)
core.set_main_window_resizable(False)
core.set_global_font_scale(1.25)
core.set_theme("Gold")
core.set_main_window_title("Generator Kontaktow Facebook")

# setting up phrases window
with simple.window("Frazy", width=520, height=150, no_resize=True, no_close=True, no_move=True, no_collapse=True):
    simple.set_window_pos("Frazy", 0, 0)
    core.add_text("Wpisz frazy/e", color=[233, 163, 33])
    core.add_spacing(count=6)


    core.add_input_text(name="<<< ", width=470)
    core.add_button("Potwierdz frazy/e", callback=ReadPhrases)

# setting up cities window
with simple.window("Miasta", width=520, height=150, no_resize=True, no_close=True, no_move=True, no_collapse=True,
                   no_focus_on_appearing=True):
    simple.set_window_pos("Miasta", 0, 152)
    core.add_text("Wpisz miasta/o", color=[233, 163, 33])
    core.add_spacing(count=9)

    core.add_input_text(name="<<<", width=470, default_value=cities)
    core.add_button("Potwierdz miasta/o", callback=ReadCities)
    core.add_spacing(count=6)

    core.add_button("Domyslnie wszystkie miasta", callback=default)

# setting up confirmation window
with simple.window("Potwierdzenie", width=520, height=150, no_resize=True, no_close=True, no_move=True, no_collapse=True,
                   no_focus_on_appearing=True):
    simple.set_window_pos("Potwierdzenie", 0, 300)

    core.add_button("Generuj", callback=GenerateContacts)
    core.add_spacing(count=5)
    core.add_progress_bar("Progres", default_value=0.0, overlay="Progres")


core.start_dearpygui()
