Skip to content

Commit

Permalink
Still bug: login needs to be performed when creating
Browse files Browse the repository at this point in the history
  • Loading branch information
zdeneklapes committed Jan 28, 2024
1 parent 81c0778 commit 7c4babb
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 36 deletions.
62 changes: 54 additions & 8 deletions bazos/scrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,31 @@ def click_submit_by_value(submits: [WebElement], value: str):
submit.click()


def get_files(credentials_path: path, country: str, phone_number: str) -> tuple:
cookies_file = credentials_path / f"{settings.COOKIES_FILE}_{country}_{phone_number}.pkl"
local_storage_file = credentials_path / f"{settings.LOCAL_STORAGE_FILE}_{country}_{phone_number}.pkl"
return (cookies_file, local_storage_file)


class XPathsBazos:
select_rubrik = "//div[@class='maincontent']/div[1]/form/select"
select_category = "//div[@class='maincontent']/form/div[1]/select"
user_inputs = "//div[@class='maincontent']/form/input"
delete_pwd_input = "//div[@class='maincontent']/div[2]/form/input[1]" # nosec
delete_submit = "//div[@class='maincontent']/div[2]/form/input[4]"

auth_phone_input = "//div[@class='maincontent']/form/input[2]"
auth_phone_input = "//div[@class='maincontent']/form[2]/input[2]"
auth_phone_check_submit = "//div[@class='maincontent']/form/input[4]"
auth_code_input = "//div[@class='maincontent']/div[1]/form/input[1]"
auth_code_submit = "//div[@class='maincontent']/div[1]/form/input[3]"

auth_condition = "//*[@id='podminky']"
auth_within_pridat_phone_input = "//*[@id='teloverit']"
auth_within_pridat_button = "/html/body/div/div[3]/div[2]/div/form/input[3]"

is_auth1 = "//*[@id='teloverit']"
is_auth2 = "//*[@id='podminky']"

product_submit = "//div[@class='maincontent']/form/div/input[6]"
product_rubric = "//div[@class='maincontent']/div[1]/form/select"
product_category = "//div[@class='maincontent']/form/div[1]/select"
Expand All @@ -54,6 +67,10 @@ def base_url(country: Literal["cz", "sk"]) -> str:
def moje_inzeraty_url(country: Literal["cz", "sk"]) -> str:
return path.join(BazosUrls.base_url(country), 'moje-inzeraty.php')

@staticmethod
def get_url(country: Literal["cz", "sk"]) -> str:
return path.join(BazosUrls.base_url(country), 'pridat-inzerat.php')


class BazosDriver:
def __init__(self, country: str, args: dict):
Expand Down Expand Up @@ -135,7 +152,7 @@ def exists_user_credentials(self, raise_exception: bool = False) -> None:
if raise_exception:
raise FileNotFoundError("User files not found, please login - login flag: --login")

def is_authentication(self) -> bool:
def is_authenticated(self) -> bool:
user_input = self.driver.find_elements(By.XPATH, XPathsBazos.user_inputs)
if len(user_input) != 3: # Mean is Authenticated, because there is not "Overit" button
return False
Expand All @@ -157,8 +174,11 @@ def authenticate(self) -> None:
self.driver.find_element(By.XPATH, XPathsBazos.auth_code_submit).click() # Submit

def save_user_credentials(self) -> None:
cookies_file = self.args["credentials_path"] / f"{settings.COOKIES_FILE}_{self.country}.pkl"
local_storage_file = self.args["credentials_path"] / f"{settings.LOCAL_STORAGE_FILE}_{self.country}.pkl"
cookies_file, local_storage_file = get_files(
credentials_path=self.args["credentials_path"],
country=self.country,
phone_number=getattr(self, 'phone_number')
)
cookies = self.driver.get_cookies()
local_storage = self.driver.execute_script("return window.localStorage;")
pickle.dump(cookies, file=open(cookies_file.__str__(), "wb")) # nosec
Expand Down Expand Up @@ -201,12 +221,22 @@ def print_all_rubrics_and_categories(self):

print(_dict)

def load_page_with_cookies(self) -> None:
self.driver.get(BazosUrls.moje_inzeraty_url(self.country))
cookies_file = self.args["credentials_path"] / f"{settings.COOKIES_FILE}_{self.country}.pkl"
def load_page_with_cookies(self, page: Literal["my_adds", "create"] = "my_adds") -> None:
cookies_file, _ = get_files(credentials_path=self.args["credentials_path"],
country=self.country,
phone_number=getattr(self.user, 'phone_number'))
if page == "my_adds":
self.driver.get(BazosUrls.moje_inzeraty_url(self.country))
elif page == "create":
self.driver.get(BazosUrls.get_url(self.country))

for cookie_dict in pickle.load(open(cookies_file, 'rb')): # nosec
self.driver.add_cookie(cookie_dict)
self.driver.get(BazosUrls.moje_inzeraty_url(self.country))

if page == "my_adds":
self.driver.get(BazosUrls.moje_inzeraty_url(self.country))
elif page == "create":
self.driver.get(BazosUrls.get_url(self.country))

def delete_advertisement(self):
del_btn = self.driver.find_element(By.CLASS_NAME, 'inzeratydetdel').find_element(By.TAG_NAME, 'a')
Expand Down Expand Up @@ -286,6 +316,22 @@ def create_all_advertisements(self) -> None:
self.driver.find_element(By.CLASS_NAME, 'pridati').click() # go to add page

self.driver.find_elements(By.CLASS_NAME, 'iconstblcell')[0].click()
# self.load_page_with_cookies(page="create")

# TODO: Fix authentification
if self.user.is_authenticated():
self.user.authenticate()
# self.driver.find_element(By.XPATH, XPathsBazos.auth_condition).click()
# self.driver.find_element(By.XPATH, XPathsBazos.auth_within_pridat_phone_input).clear()
# self.driver.find_element(
# By.XPATH, XPathsBazos.auth_within_pridat_phone_input
# ).send_keys(getattr(self.user, 'phone_number'))
# self.driver.find_element(By.XPATH, XPathsBazos.auth_within_pridat_button).click()
# self.driver.find_element(By.XPATH, XPathsBazos.auth_code_input).clear()
# (self.driver.find_element(By.XPATH, XPathsBazos.auth_code_input)
# .send_keys(input('Please provide authentification code sended to your phone: ')))
# self.driver.find_element(By.XPATH, XPathsBazos.auth_code_submit).click()

self.create_advertisement(product=product)

def product_already_advertised(self, product: Product) -> bool:
Expand Down
14 changes: 14 additions & 0 deletions examples-data/plysak/info.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
>>RUBRIC
Děti

>>CATEGORY
Hračky

>>TITLE
Plysak Pes

>>PRICE
1

>>DESCRIPTION
Dobrý den, nabízím plysaka.
Binary file added examples-data/plysak/photos/1.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions examples-data/user_cz.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: 'YourName'
phone_number: '+420602856098'
email: '[email protected]'
psc: '60200'
password: '0000'
15 changes: 15 additions & 0 deletions runtests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

python -m venv venv
source venv/bin/activate
pip3 install -r requirements.txt
pip3 install $(pip3 list --outdated | awk '{print $1}'|tail -n 7) --upgrade
PYTHONPATH=$PWD python3 bazos/__init__.py \
--items-path $PWD/examples-data/ \
--credentials-path $PWD/tokens/ \
--verbose=1 \
--remote=0 \
--create-all=1 \
--delete-all=1 \
--country cz sk \
--mode=fast
23 changes: 0 additions & 23 deletions tests/images/apple-watch-reminek/info.txt

This file was deleted.

Binary file not shown.
Binary file not shown.
5 changes: 0 additions & 5 deletions tests/images/user_cz.yml

This file was deleted.

0 comments on commit 7c4babb

Please sign in to comment.