Skip to content

Commit

Permalink
Fixed headless
Browse files Browse the repository at this point in the history
  • Loading branch information
zdeneklapes committed Dec 25, 2023
1 parent 63dddba commit a0f8ca5
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 134 deletions.
82 changes: 68 additions & 14 deletions bazos/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import argparse
from pathlib import Path
import sys
from typing import Dict, Any

from bazos.main import bazos_main as bz, BazosScrapper
from bazos.main import BazosScrapper, BazosUser

__version__ = "0.1.0"
__apiversion__ = "0.1.0"
Expand All @@ -12,10 +13,11 @@

def parse_cli_argument() -> Dict[str, Any]:
parser = argparse.ArgumentParser()
# true/false
parser.add_argument('--login',
action='store_true',
help='Login to bazos')
parser.add_argument('-b', '--bazos',
parser.add_argument('--bazos',
action='store_true',
help='Use bazos')
parser.add_argument('--add-only',
Expand All @@ -24,33 +26,85 @@ def parse_cli_argument() -> Dict[str, Any]:
parser.add_argument('--print-rubrics',
action='store_true',
help='Print rubrics')
parser.add_argument("--verbose",
action='store_true',
default=True,
help='Verbose')
parser.add_argument("--delete-all",
action='store_true',
default=True,
help='Verbose')
parser.add_argument("--create-all",
action='store_true',
default=True,
help='Verbose')
# ?
parser.add_argument('--items-path',
type=Path,
required=True,
nargs='?',
help='Path to products directory')
parser.add_argument('--credentials-path',
type=Path,
required=True,
nargs='?',
help='Path to products directory')
# +
parser.add_argument('--country',
nargs="+",
help="What bazos country to use",
default=['cz', 'sk'])
parser.add_argument('-p', '--path',
help='Path to products directory')
parser.add_argument("--update-credentials",
action='store_true',
help='Update credentials')
cli_args = vars(parser.parse_args())
return cli_args


def main():
cli_args = parse_cli_argument()

if cli_args['verbose']:
# print(f"==> CLI args: {cli_args}")
print(' '.join(sys.argv))

if cli_args['login']:
bazos_scrapper = BazosScrapper(country=cli_args['country'][0], cli_args=cli_args)
# bazos_scrapper.check_user_files_available()
bazos_scrapper.save_authentication()
bazos_scrapper.load_page_with_cookies()
bazos_scrapper.check_authentication()
return
bazos_user = BazosUser(
country='cz', items_path=cli_args['items_path'],
credentials_path=cli_args['credentials_path']
)
bazos_user.authenticate()
bazos_user.save_user_credentials()

if cli_args['bazos']:
bz(cli_args=cli_args)
for country in cli_args['country']:
if cli_args['verbose']:
print(f"==> Processing country: {country}")

# Check if user credentials exists
user = BazosUser(
country='cz', items_path=cli_args['items_path'],
credentials_path=cli_args['credentials_path']
)
try:
user.exists_user_credentials(raise_exception=True)
except FileNotFoundError as e:
print(e)
sys.exit(1)

if cli_args['print_rubrics']:
bazos_scrapper = BazosScrapper(country=country, cli_args=cli_args, user=user)
# bazos_scrapper.check_user_files_available()
bazos_scrapper.load_page_with_cookies()
# bazos_scrapper.check_authentication()
bazos_scrapper.print_all_rubrics_and_categories()
return

bazos_scrapper = BazosScrapper(country=country, cli_args=cli_args, user=user)
bazos_scrapper.load_page_with_cookies()

# Restore advertisements
if cli_args['delete_all']:
bazos_scrapper.delete_advertisements()
if cli_args['create_all']:
bazos_scrapper.create_advertisements()
sys.exit()


Expand Down
5 changes: 2 additions & 3 deletions bazos/core/settings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
from os import path
from pathlib import Path

ROOT_DIR = Path(os.getcwd())
Expand All @@ -11,8 +10,8 @@
os.makedirs(TOKENS_DIR)


COOKIES_FILE = path.join(TOKENS_DIR, 'cookies')
LOCAL_STORAGE_FILE = path.join(TOKENS_DIR, 'local_storage')
COOKIES_FILE = 'cookies'
LOCAL_STORAGE_FILE = 'local_storage'

if __name__ == '__main__':
print('run')
Loading

0 comments on commit a0f8ca5

Please sign in to comment.