Skip to content

Commit

Permalink
Added new function to fix images
Browse files Browse the repository at this point in the history
  • Loading branch information
zdeneklapes committed Feb 23, 2024
1 parent 3e622f7 commit 5889d53
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 16 deletions.
2 changes: 1 addition & 1 deletion bazos/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def parse_cli_argument() -> Dict[str, Any]:
def loop_country(args, bazos_driver, callback: Callable):
for country in args['country']:
if args['verbose']:
print(f"==> Processing country: {country}")
print(f"\n==> Processing country: {country} =============================")

bazos_user = BazosUser(country=country, args=args, driver=bazos_driver.driver)
bazos_scrapper = BazosScrapper(country=country, args=args, user=bazos_user, driver=bazos_driver.driver)
Expand Down
8 changes: 6 additions & 2 deletions bazos/info/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ def __init__(self, product_path: str):
self.currency_rates = CurrencyRates()

def get_images(self):
images = sorted(map(lambda x: path.join(self.product_path, 'photos', x),
next(os.walk(path.join(self.product_path, 'photos')))[2]))
photos_folder = path.join(self.product_path, 'photos')
try:
all_files = next(os.walk(photos_folder))[2]
except StopIteration:
raise Exception(f"Directory not found: {photos_folder}")
images = sorted(map(lambda x: path.join(photos_folder, x), all_files))
return [filename for filename in images if
filename.lower().endswith(('.png', '.jpg', '.jpeg', '.tiff', '.bmp', '.gif'))]

Expand Down
13 changes: 9 additions & 4 deletions bazos/scrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,10 @@ def authenticate(self) -> None:
# Authenticate
code_input = self.driver.find_element(By.XPATH, XPathsBazos.auth_code_input)
code_input.clear()
code_input.send_keys(input('Please provide authentification code sended to your phone: '))
self.driver.find_element(By.XPATH, XPathsBazos.auth_code_submit).click() # Submit
received_auth_code = input('Please provide authentification code sended to your phone: ')
code_input.send_keys(received_auth_code)
submit_button = self.driver.find_element(By.XPATH, XPathsBazos.auth_code_submit)
submit_button.click() # Submit

def save_user_credentials(self) -> None:
cookies_file, local_storage_file = get_files(
Expand All @@ -197,6 +199,8 @@ def save_user_credentials(self) -> None:
local_storage = self.driver.execute_script("return window.localStorage;")
pickle.dump(cookies, file=open(cookies_file.__str__(), "wb")) # nosec
pickle.dump(local_storage, file=open(local_storage_file, "wb")) # nosec
if self.args['verbose']:
print(f"User credentials saved to: {cookies_file}, {local_storage_file}")


class BazosScrapper:
Expand Down Expand Up @@ -266,7 +270,7 @@ def delete_all_advertisements(self):

verbose_print(
args=self.args,
message=f"Removing {self.size_new_products} old advertisements"
message=f"Removing {self.size_new_products} advertisements"
)

for i in range(self.size_new_products):
Expand Down Expand Up @@ -308,7 +312,8 @@ def create_advertisement(self, product: Product):
self.driver.find_element(By.ID, 'heslobazar').send_keys(getattr(self.user, 'password'))

wait_random_time(args=self.args, coef=2)
self.driver.find_element(By.XPATH, XPathsBazos.product_submit).click()
button_create = self.driver.find_element(By.XPATH, XPathsBazos.product_submit)
button_create.click()

def create_all_advertisements(self) -> None:
new_products = load_products(products_path=self.args["items_path"], country=self.country)
Expand Down
52 changes: 43 additions & 9 deletions make.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ RM="rm -rfd"
RED='\033[0;31m'
NC='\033[0m'
GREEN='\033[0;32m'
DEBUG=1
ZIP_NAME='TODO.zip'
VPS_URI='TODO'

Expand All @@ -15,17 +14,17 @@ function prune_docker() {
docker system prune --all --force --volumes

# Remove all volumes: not just dangling ones
for i in $(docker volume ls --format json | jq -r ".Name"); do
docker volume rm -f ${i}
for photos_dir in $(docker volume ls --format json | jq -r ".Name"); do
docker volume rm -f ${photos_dir}
done
}

function docker_show_ipaddress() {
# Show ip address of running containers
for docker_container in $(docker ps -aq); do
CMD1="$(docker ps -a | grep "${docker_container}" | grep --invert-match "Exited\|Created" | awk '{print $2}'): "
if [ ${CMD1} != ": " ]; then
printf "${CMD1}"
change_photo_to_reusable_format_cmd="$(docker ps -a | grep "${docker_container}" | grep --invert-match "Exited\|Created" | awk '{print $2}'): "
if [ ${change_photo_to_reusable_format_cmd} != ": " ]; then
printf "${change_photo_to_reusable_format_cmd}"
printf "$(docker inspect ${docker_container} | grep "IPAddress" | tail -n 1)\n"
fi
done
Expand Down Expand Up @@ -163,6 +162,42 @@ function entrypoint() {
/usr/bin/supervisord -c /etc/supervisor/supervisord.conf
}

function prepare_images() {
# Change photos to reusable format and remove unnecessary user data
# png files are too large so we need to use jpg
# ENVIRONMENT VARIABLES
# DEBUG: 1/0
# ITEMS_PWD: photos root directory
# WANT_FORMAT: jpg
# Examples: ./make.sh

# Check variables are set
if [ ${DEBUG} == "" ]; then DEBUG="1"; fi
if [ ${ITEMS_PWD} == "" ]; then die "ITEMS_PWD is not set"; fi
if [ ${WANT_FORMAT} == "" ]; then WANT_FORMAT="jpg"; fi

WANT_FORMAT="jpg"
for photos_dir in $(find "$ITEMS_PWD" -type d -iname "*photos*"); do
echo "Processing photos in: ${photos_dir}"
for photo in $(find "$photos_dir" -type f -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.JPG" -o -iname "*.JPEG" -o -iname "*.png"); do
echo "Processing photo: ${photo}"
change_photo_to_reusable_format_cmd="mogrify -format ${WANT_FORMAT} ${photo}"
remove_photo_cmd="rm ${photo}"
if [ "$DEBUG" -eq "1" ]; then echo ${change_photo_to_reusable_format_cmd}; else eval ${change_photo_to_reusable_format_cmd} || die "Error while converting photos"; fi
# if photo is pgn then skip removeing
local extension="${photo##*.}"
if [ "$extension" != "$WANT_FORMAT" ]; then
if [ "$DEBUG" -eq "1" ]; then echo ${remove_photo_cmd}; else eval ${remove_photo_cmd} || die "Error while removing photos"; fi
fi
done
remove_unnecessary_user_data_cmd="exiftool -all= $photos_dir/*.$WANT_FORMAT"
if [ "$DEBUG" -eq "1" ]; then echo ${remove_unnecessary_user_data_cmd}; else eval ${remove_unnecessary_user_data_cmd} || die "Error while removing unnecessary user data"; fi
remove_originals_cmd="rm $photos_dir/*_original"
if [ "$DEBUG" -eq "1" ]; then echo ${remove_originals_cmd}; else eval ${remove_originals_cmd} || die "Error while removing originals"; fi
# exit
done
}

function send() {
# Send zipped project to VPS and then remove the zip file
scp "${ZIP_NAME}" "${VPS_URI}"
Expand All @@ -172,7 +207,7 @@ function send() {
function help() {
# Print usage on stdout
echo "Available functions:"
for file in ./scripts/*.sh; do
for file in ${BASH_SOURCE[0]}; do
function_names=$(cat ${file} | grep -E "(\ *)function\ +.*\(\)\ *\{" | sed -E "s/\ *function\ +//" | sed -E "s/\ *\(\)\ *\{\ *//")
for func_name in ${function_names[@]}; do
printf " $func_name\n"
Expand All @@ -189,8 +224,7 @@ function usage() {

function die() {
# Print error message on stdout and exit
printf "${RED}ERROR: $1${NC}\n"
help
printf "${RED}ERROR: %s for help run: \n./make.sh help${NC}\n" "$1" >&2
exit 1
}

Expand Down

0 comments on commit 5889d53

Please sign in to comment.