Last active
July 19, 2024 21:53
-
-
Save shikhir-arora/f2ee994409175eada2dc11c648e61fa1 to your computer and use it in GitHub Desktop.
A simple up-to-date script (2024 July) to check stock for a product ID at all Micro Center locations in Bash and Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from bs4 import BeautifulSoup | |
# Prompt user for the product ID | |
product_id = input("Enter the product ID (default is 0): ") or "0" | |
# URL template of the Micro Center product page | |
url_template = f"https://www.microcenter.com/product/{product_id}/v?storeid={{}}" | |
# List of store IDs | |
store_ids = { | |
"Tustin, CA": 101, | |
"Denver, CO": 181, | |
"Duluth, GA": 65, | |
"Marietta, GA": 41, | |
"Chicago, IL": 151, | |
"Westmont, IL": 25, | |
"Indianapolis, IN": 165, | |
"Overland Park, KS": 191, | |
"Cambridge, MA": 121, | |
"Rockville, MD": 85, | |
"Parkville, MD": 125, | |
"Madison Heights, MI": 55, | |
"St. Louis Park, MN": 45, | |
"Brentwood, MO": 95, | |
"Charlotte, NC": 175, | |
"North Jersey, NJ": 75, | |
"Westbury, NY": 171, | |
"Brooklyn, NY": 115, | |
"Flushing, NY": 145, | |
"Yonkers, NY": 105, | |
"Columbus, OH": 141, | |
"Mayfield Heights, OH": 51, | |
"Sharonville, OH": 71, | |
"St. Davids, PA": 61, | |
"Houston, TX": 155, | |
"Dallas, TX": 131, | |
"Fairfax, VA": 81, | |
"Shippable Items": 29 # This entry represents "Shippable Items" if needed | |
} | |
# Function to fetch and parse the product page for a given store ID | |
def fetch_product_page(store_id): | |
try: | |
url = url_template.format(store_id) | |
response = requests.get(url) | |
response.raise_for_status() # Raise an exception for HTTP errors | |
return BeautifulSoup(response.content, 'html.parser') | |
except requests.RequestException as e: | |
print(f"\033[91mError fetching the product page for store ID {store_id}: {e}\033[0m") | |
return None | |
# Function to extract availability information | |
def check_availability(soup): | |
availability_info = [] | |
try: | |
inventory_div = soup.find('div', id='pnlInventory') | |
if inventory_div: | |
# Check for the existence of store name element | |
store_name_element = inventory_div.find('span', class_='storeName') | |
store_name = store_name_element.text.strip() if store_name_element else 'Unknown Store' | |
# Check for the existence of inventory status element | |
inventory_status_element = inventory_div.find('span', class_='inventoryCnt') | |
status_text = inventory_status_element.text.strip() if inventory_status_element else 'SOLD OUT' | |
availability_info.append({"Store": store_name, "Availability": status_text}) | |
else: | |
print("\033[93mNo inventory information found on the page.\033[0m") | |
except Exception as e: | |
print(f"\033[91mError parsing the product page: {e}\033[0m") | |
return availability_info | |
# Function to print formatted availability information | |
def print_availability_info(availability_info): | |
in_stock = [info for info in availability_info if 'IN STOCK' in info['Availability']] | |
sold_out = [info for info in availability_info if 'SOLD OUT' in info['Availability']] | |
print("\n\033[94m==================== Availability Information ====================\033[0m\n") | |
print("\033[92mIn Stock:\033[0m\n") | |
for info in in_stock: | |
print(f"Store: {info['Store']}\nAvailability: \033[92m{info['Availability']}\033[0m\n") | |
print("\033[91mSold Out:\033[0m\n") | |
for info in sold_out: | |
print(f"Store: {info['Store']}\nAvailability: \033[91m{info['Availability']}\033[0m\n") | |
print("\033[94m==================================================================\033[0m\n") | |
# Fetch the product page and availability information for each store | |
all_availability_info = [] | |
for store_name, store_id in store_ids.items(): | |
print(f"\033[96mChecking availability for {store_name} (Store ID: {store_id})...\033[0m") | |
soup = fetch_product_page(store_id) | |
if soup: | |
availability_info = check_availability(soup) | |
if availability_info: | |
all_availability_info.extend(availability_info) | |
else: | |
print(f"\033[93mNo availability information found for {store_name}.\033[0m") | |
else: | |
print(f"\033[91mFailed to fetch the product page for store ID {store_id}.\033[0m") | |
# Print all collected availability information | |
if all_availability_info: | |
print_availability_info(all_availability_info) | |
else: | |
print("\033[91mNo availability information found for any store.\033[0m") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Prompt user for the product ID | |
read "product_id?Enter the product ID (default is 0): " | |
product_id=${product_id:-0} | |
# URL template of the Micro Center product page | |
url_template="https://www.microcenter.com/product/${product_id}/v?storeid=" | |
# List of store IDs and their names | |
store_ids=("101:Tustin, CA" "181:Denver, CO" "65:Duluth, GA" "41:Marietta, GA" "151:Chicago, IL" | |
"25:Westmont, IL" "165:Indianapolis, IN" "191:Overland Park, KS" "121:Cambridge, MA" | |
"85:Rockville, MD" "125:Parkville, MD" "55:Madison Heights, MI" "45:St. Louis Park, MN" | |
"95:Brentwood, MO" "175:Charlotte, NC" "75:North Jersey, NJ" "171:Westbury, NY" | |
"115:Brooklyn, NY" "145:Flushing, NY" "105:Yonkers, NY" "141:Columbus, OH" | |
"51:Mayfield Heights, OH" "71:Sharonville, OH" "61:St. Davids, PA" "155:Houston, TX" | |
"131:Dallas, TX" "81:Fairfax, VA" "29:Shippable Items") | |
# Function to fetch and parse the product page for a given store ID | |
fetch_product_page() { | |
local store_id=$1 | |
local url="${url_template}${store_id}" | |
curl -s "$url" | |
} | |
# Function to extract availability information | |
check_availability() { | |
local response=$1 | |
local store_name=$2 | |
local availability_info=$(echo "$response" | grep -o 'class="inventoryCnt">[^<]*' | head -1 | sed 's/class="inventoryCnt">//') | |
local store_display=$(echo "$response" | grep -o 'class="storeName">[^<]*' | head -1 | sed 's/class="storeName">//') | |
if [[ -z "$availability_info" || "$availability_info" == *"SOLD OUT"* ]]; then | |
availability_info="SOLD OUT" | |
fi | |
if [ -z "$store_display" ]; then | |
store_display="$store_name" | |
fi | |
echo -e "${store_display}\t${availability_info}" | |
} | |
# Fetch the product page and availability information for each store | |
all_availability_info=() | |
for store in "${store_ids[@]}"; do | |
store_id=${store%%:*} | |
store_name=${store#*:} | |
echo -e "\033[96mChecking availability for ${store_name} (Store ID: ${store_id})...\033[0m" | |
response=$(fetch_product_page "$store_id") | |
availability_info=$(check_availability "$response" "$store_name") | |
all_availability_info+=("$availability_info") | |
done | |
# Print all collected availability information | |
echo -e "\n\033[94m==================== Availability Information ====================\033[0m\n" | |
echo -e "\033[92mIn Stock:\033[0m\n" | |
for info in "${all_availability_info[@]}"; do | |
if [[ "$info" != *"SOLD OUT"* ]]; then | |
echo -e "\033[92m$info\033[0m" | |
fi | |
done | |
echo -e "\n\033[91mSold Out:\033[0m\n" | |
for info in "${all_availability_info[@]}"; do | |
if [[ "$info" == *"SOLD OUT"* ]]; then | |
echo -e "\033[91m$info\033[0m" | |
fi | |
done | |
echo -e "\n\033[94m==================================================================\033[0m\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment