-
Notifications
You must be signed in to change notification settings - Fork 2
/
integration.py
66 lines (52 loc) · 2.4 KB
/
integration.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""API integration for syncing inventories from AlphaBroder to Shopify."""
import os
from api import API
from dotenv import load_dotenv
from datetime import datetime, timedelta
import click
load_dotenv()
last_run = None
def check_should_run():
"""Check if it is the next day and SanMar should run again."""
global last_run
now = datetime.utcnow() - timedelta(hours=7)
if last_run is None or last_run.day != now.day:
last_run = now
return True
@click.command()
@click.option('--continuous', '-c', is_flag=True, help='Run continuously. Relevant to updating inventory only.')
@click.option('--limit', '-l', default=5, help='Limit number of imported products.')
@click.option('--download', '-d', is_flag=True, help="Download Files.")
@click.option('--verbose', '-v', is_flag=True, help="Verbose - Show debug statements.")
@click.option('--existing', '-e', is_flag=True, help="Include existing products and re-import them.")
@click.option('--products', '-p', is_flag=True, help="Update products.")
@click.option('--inventory', '-i', is_flag=True, help="Update inventory.")
@click.option('--sanmar', '-s', is_flag=True, help="SanMar. If flag is not present, AlphaBroder settings will be used. "
"Relevant to updating products only.")
def main(continuous, limit, download, verbose, existing, products, inventory, sanmar):
"""Integration"""
# TODO: Use flags instead of .env settings for ONLY_THESE
# 600
print(f'<{datetime.now().strftime("%Y-%m-%d %H:%M:%S")}>: Begin inventory update.')
files = 'files'
if not os.path.exists(files):
os.mkdir(files)
images = 'images'
if not os.path.exists(images):
os.mkdir(images)
api = API(download, verbose)
if products:
api.update_products(limit=limit, sanmar=sanmar, skip_existing=not existing)
if inventory:
if continuous:
while True:
# if check_should_run():
# print(f'<{datetime.now().strftime("%Y-%m-%d %H:%M:%S")}>: Sanmar inventory update.')
# api.update_inventory()
print(f'<{datetime.now().strftime("%Y-%m-%d %H:%M:%S")}>: All inventory update.')
api.update_inventory()
else:
api.update_inventory()
print(f'<{datetime.now().strftime("%Y-%m-%d %H:%M:%S")}>: Finished inventory update.')
if __name__ == '__main__':
main()