Better comment and logging version at start
This commit is contained in:
@ -4,15 +4,23 @@ import logging
|
|||||||
import json
|
import json
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
VERSION = "4.0.0"
|
||||||
|
|
||||||
# Logger setup
|
# Logger setup
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
level=logging.INFO,
|
level=logging.INFO,
|
||||||
format="%(asctime)s [%(levelname)s] %(message)s",
|
format="%(asctime)s [%(levelname)s] %(message)s",
|
||||||
)
|
)
|
||||||
logging.info("Script started")
|
|
||||||
|
|
||||||
try:
|
# Logging starter
|
||||||
|
logging.info("=" * 60)
|
||||||
|
logging.info("🟩 Nvidia Stock Bot - Version %s", VERSION)
|
||||||
|
logging.info("Source: https://git.djeex.fr/Djeex/nvidia-stock-bot")
|
||||||
|
logging.info("Mirror: https://github.com/Djeex/nvidia-stock-bot")
|
||||||
|
logging.info("=" * 60)
|
||||||
|
|
||||||
# Env variables
|
# Env variables
|
||||||
|
try:
|
||||||
DISCORD_WEBHOOK_URL = os.environ['DISCORD_WEBHOOK_URL']
|
DISCORD_WEBHOOK_URL = os.environ['DISCORD_WEBHOOK_URL']
|
||||||
DISCORD_SERVER_NAME = os.environ.get('DISCORD_SERVER_NAME', 'Shared for free')
|
DISCORD_SERVER_NAME = os.environ.get('DISCORD_SERVER_NAME', 'Shared for free')
|
||||||
DISCORD_ROLES = os.environ.get('DISCORD_ROLES')
|
DISCORD_ROLES = os.environ.get('DISCORD_ROLES')
|
||||||
@ -20,6 +28,8 @@ try:
|
|||||||
REFRESH_TIME = int(os.environ.get('REFRESH_TIME') or 30)
|
REFRESH_TIME = int(os.environ.get('REFRESH_TIME') or 30)
|
||||||
TEST_MODE = os.environ.get('TEST_MODE', 'False').lower() == 'true'
|
TEST_MODE = os.environ.get('TEST_MODE', 'False').lower() == 'true'
|
||||||
PRODUCT_NAMES = os.environ['PRODUCT_NAMES']
|
PRODUCT_NAMES = os.environ['PRODUCT_NAMES']
|
||||||
|
|
||||||
|
# Errors and warning
|
||||||
except KeyError as e:
|
except KeyError as e:
|
||||||
logging.error(f"Missing environment variable: {e}")
|
logging.error(f"Missing environment variable: {e}")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
@ -27,6 +37,9 @@ except ValueError:
|
|||||||
logging.error("REFRESH_TIME must be a valid integer.")
|
logging.error("REFRESH_TIME must be a valid integer.")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
if TEST_MODE:
|
||||||
|
logging.warning("🚧 Test mode is active. No real alerts will be sent.")
|
||||||
|
|
||||||
if not PRODUCT_NAMES:
|
if not PRODUCT_NAMES:
|
||||||
logging.error("❌ PRODUCT_NAMES is required but not defined.")
|
logging.error("❌ PRODUCT_NAMES is required but not defined.")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
@ -53,7 +66,7 @@ else:
|
|||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
DISCORD_ROLE_MAP[name] = role
|
DISCORD_ROLE_MAP[name] = role
|
||||||
|
|
||||||
# Masked webhook for display
|
# Masked webhook in terminal
|
||||||
match = re.search(r'/(\d+)/(.*)', DISCORD_WEBHOOK_URL)
|
match = re.search(r'/(\d+)/(.*)', DISCORD_WEBHOOK_URL)
|
||||||
if match:
|
if match:
|
||||||
webhook_id = match.group(1)
|
webhook_id = match.group(1)
|
||||||
@ -64,10 +77,6 @@ if match:
|
|||||||
else:
|
else:
|
||||||
wh_masked_url = "[Invalid webhook URL]"
|
wh_masked_url = "[Invalid webhook URL]"
|
||||||
|
|
||||||
# Test mode
|
|
||||||
if TEST_MODE:
|
|
||||||
logging.warning("🚧 Test mode is active. No real alerts will be sent.")
|
|
||||||
|
|
||||||
# HTTP headers
|
# HTTP headers
|
||||||
HEADERS = {
|
HEADERS = {
|
||||||
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) "
|
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) "
|
||||||
|
@ -4,17 +4,21 @@ from env_config import HEADERS, PRODUCT_NAMES, API_URL_SKU, API_URL_STOCK, PRODU
|
|||||||
from notifier import send_discord_notification, send_out_of_stock_notification, send_sku_change_notification
|
from notifier import send_discord_notification, send_out_of_stock_notification, send_sku_change_notification
|
||||||
from requests.adapters import HTTPAdapter, Retry
|
from requests.adapters import HTTPAdapter, Retry
|
||||||
|
|
||||||
|
# HTTP session
|
||||||
session = requests.Session()
|
session = requests.Session()
|
||||||
retries = Retry(total=5, backoff_factor=1, status_forcelist=[500, 502, 503, 504])
|
retries = Retry(total=5, backoff_factor=1, status_forcelist=[500, 502, 503, 504])
|
||||||
session.mount('https://', HTTPAdapter(max_retries=retries))
|
session.mount('https://', HTTPAdapter(max_retries=retries))
|
||||||
|
|
||||||
|
# Keeping memory of last run
|
||||||
last_sku_dict = {}
|
last_sku_dict = {}
|
||||||
global_stock_status_dict = {}
|
global_stock_status_dict = {}
|
||||||
first_run_dict = {name: True for name in PRODUCT_NAMES}
|
first_run_dict = {name: True for name in PRODUCT_NAMES}
|
||||||
|
|
||||||
|
# Stock check function
|
||||||
def check_rtx_50_founders():
|
def check_rtx_50_founders():
|
||||||
global last_sku_dict, global_stock_status_dict, first_run_dict
|
global last_sku_dict, global_stock_status_dict, first_run_dict
|
||||||
|
|
||||||
|
# Fetching nvidia API data
|
||||||
try:
|
try:
|
||||||
response = session.get(API_URL_SKU, headers=HEADERS, timeout=10)
|
response = session.get(API_URL_SKU, headers=HEADERS, timeout=10)
|
||||||
logging.info(f"SKU API response: {response.status_code}")
|
logging.info(f"SKU API response: {response.status_code}")
|
||||||
@ -24,6 +28,7 @@ def check_rtx_50_founders():
|
|||||||
logging.error(f"SKU API error: {e}")
|
logging.error(f"SKU API error: {e}")
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# Checking productSKU and productUPC for all GPU set in PRODUCT_NAME
|
||||||
all_products = data['searchedProducts']['productDetails']
|
all_products = data['searchedProducts']['productDetails']
|
||||||
|
|
||||||
for product_name in PRODUCT_NAMES:
|
for product_name in PRODUCT_NAMES:
|
||||||
@ -42,6 +47,7 @@ def check_rtx_50_founders():
|
|||||||
if not isinstance(product_upc, list):
|
if not isinstance(product_upc, list):
|
||||||
product_upc = [product_upc]
|
product_upc = [product_upc]
|
||||||
|
|
||||||
|
# Detect SKU changes
|
||||||
old_sku = last_sku_dict.get(product_name)
|
old_sku = last_sku_dict.get(product_name)
|
||||||
if old_sku and old_sku != product_sku and not first_run_dict[product_name]:
|
if old_sku and old_sku != product_sku and not first_run_dict[product_name]:
|
||||||
logging.warning(f"⚠️ SKU changed for {product_name}: {old_sku} → {product_sku}")
|
logging.warning(f"⚠️ SKU changed for {product_name}: {old_sku} → {product_sku}")
|
||||||
@ -50,6 +56,7 @@ def check_rtx_50_founders():
|
|||||||
last_sku_dict[product_name] = product_sku
|
last_sku_dict[product_name] = product_sku
|
||||||
first_run_dict[product_name] = False
|
first_run_dict[product_name] = False
|
||||||
|
|
||||||
|
# Check product availability in API_URL_STOCK for each SKU
|
||||||
api_stock_url = API_URL_STOCK + product_sku
|
api_stock_url = API_URL_STOCK + product_sku
|
||||||
logging.info(f"[{product_name}] Checking stock: {api_stock_url}")
|
logging.info(f"[{product_name}] Checking stock: {api_stock_url}")
|
||||||
|
|
||||||
@ -62,6 +69,7 @@ def check_rtx_50_founders():
|
|||||||
logging.error(f"Stock API error: {e}")
|
logging.error(f"Stock API error: {e}")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
# Retrieve availibilty and price for each SKU
|
||||||
products = stock_data.get("listMap", [])
|
products = stock_data.get("listMap", [])
|
||||||
products_price = "Price not available"
|
products_price = "Price not available"
|
||||||
if isinstance(products, list) and len(products) > 0:
|
if isinstance(products, list) and len(products) > 0:
|
||||||
@ -80,6 +88,7 @@ def check_rtx_50_founders():
|
|||||||
if is_active and any(upc.upper() in gpu_name for upc in product_upc):
|
if is_active and any(upc.upper() in gpu_name for upc in product_upc):
|
||||||
found_in_stock.add(gpu_name)
|
found_in_stock.add(gpu_name)
|
||||||
|
|
||||||
|
# Comparing previous state and notify
|
||||||
for upc in product_upc:
|
for upc in product_upc:
|
||||||
upc_upper = upc.upper()
|
upc_upper = upc.upper()
|
||||||
currently_in_stock = upc_upper in found_in_stock
|
currently_in_stock = upc_upper in found_in_stock
|
||||||
|
Reference in New Issue
Block a user