Skip to content

Commit

Permalink
Merge pull request #1 from eth-educators/hardware-unavailable-ping
Browse files Browse the repository at this point in the history
only ping when no unavailable options
  • Loading branch information
hanniabu authored Aug 1, 2024
2 parents 341524f + f1247a7 commit 93e5758
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
20 changes: 16 additions & 4 deletions _scripts/hardware.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

def get_hardware_data():
if utilities.use_test_data:
response = {'status': 200, 'attempts': 1, 'data': [{'name': 'Modem / Router', 'index': 1, 'products': [{'id': None, 'name': 'Your existing modem/router', 'price': 0}], 'usage': 'required', 'min_specs': None, 'guide': None}, {'name': 'Smart Plug', 'index': 2, 'products': [{'id': 'B091699Z3W', 'name': 'Kasa Smart Plug Ultra Mini', 'details': None, 'link': 'https://www.amazon.com/dp/B091699Z3W', 'price': 10}], 'usage': 'optional', 'min_specs': None, 'guide': None}, {'name': 'Computer', 'index': 3, 'products': [{'id': 'B09KNK27M2', 'name': 'Intel NUC 11 NUC11PAHi7 Barebone Mini-PC', 'details': 'Core i7-1165G7', 'link': 'https://www.amazon.com/dp/B09KNK27M2/', 'price': 395}, {'id': 'N82E16856110250', 'name': 'ASUS Ryzen 7 Barebone Mini-PC', 'details': 'AMD Ryzen 7 4700U', 'link': 'https://www.newegg.com/p/N82E16856110250', 'price': 358}, {'id': '1VK-001S-02DH9', 'name': 'ASUS ExpertCenter PN64 Barebone Mini-PC', 'details': 'Intel Core i3-1220P', 'link': 'https://www.newegg.com/p/1VK-001S-02DH9', 'price': 358}], 'usage': 'required', 'min_specs': None, 'guide': None}, {'name': 'UPS', 'index': 4, 'products': [{'id': 'B0030SL08A', 'name': 'CyberPower CP425SLG Standby UPS System', 'details': None, 'link': 'https://www.amazon.com/dp/B0030SL08A/', 'price': 60}], 'usage': 'recommended', 'min_specs': None, 'guide': 'https://github.com/trevhub/guides/blob/main/CheapUPS.md'}, {'name': 'SSD', 'index': 5, 'products': [{'id': 'B08RK2SR23', 'name': 'Samsung 980 PRO', 'details': '2TB, PCIe 4.0, M2', 'link': 'https://www.amazon.com/dp/B08RK2SR23/', 'price': 167}, {'id': 'N82E16820147796', 'name': 'Samsung 980 PRO', 'details': '2TB, PCIe 4.0, M2', 'link': 'https://www.newegg.com/p/N82E16820147796', 'price': 167}], 'usage': 'required', 'min_specs': '>=2TB', 'guide': 'https://www.youtube.com/watch?v=04K8OUOMHGM'}, {'name': 'RAM', 'index': 6, 'products': [{'id': 'B07ZLC7VNH', 'name': 'Crucial RAM CT32G4SFD832A', 'details': '32GB DDR4 3200MHz CL22', 'link': 'https://www.amazon.com/dp/B07ZLC7VNH', 'price': 68}], 'usage': 'required', 'min_specs': '>=32GB', 'guide': 'https://www.youtube.com/watch?v=04K8OUOMHGM'}]}
response = {'status': 200, 'attempts': 1, 'data': [{"name":"Modem / Router","index":1,"products":[{"id":None,"name":"Your existing modem/router","price":0}],"usage":"required","min_specs":None,"guide":None},{"name":"Computer","index":2,"products":[{"id":"B09KNK27M2","name":"Intel NUC 11 NUC11PAHi7 Barebone Mini-PC","details":"Core i7-1165G7","link":"https://www.amazon.com/dp/B09KNK27M2/","price":395},{"id":"N82E16856110250","name":"ASUS ExpertCenter PN64 Barebone Mini-PC","details":"Intel Core i3-1220P","link":"https://www.newegg.com/p/N82E16856110250","price":358}],"usage":"required","min_specs":None,"guide":None},{"name":"SSD","index":3,"products":[{"id":"B08RK2SR23","name":"Samsung 980 PRO","details":"2TB, PCIe 4.0, M2","link":"https://www.amazon.com/dp/B08RK2SR23/","price":167},{"id":"N82E16820147796","name":"Samsung 980 PRO","details":"2TB, PCIe 4.0, M2","link":"https://www.newegg.com/p/N82E16820147796","price":167}],"usage":"required","min_specs":">=2TB","guide":"https://www.youtube.com/watch?v=04K8OUOMHGM"},{"name":"RAM","index":4,"products":[{"id":"B07ZLC7VNH","name":"Crucial RAM CT32G4SFD832A","details":"32GB DDR4 3200MHz CL22","link":"https://www.amazon.com/dp/B07ZLC7VNH","price":68}],"usage":"required","min_specs":">=32GB","guide":"https://www.youtube.com/watch?v=04K8OUOMHGM"},{"name":"UPS","index":5,"products":[{"id":"B0030SL08A","name":"CyberPower CP425SLG Standby UPS System","details":None,"link":"https://www.amazon.com/dp/B0030SL08A/","price":60},{"id":"N82E16842102117","name":"CyberPower CP425SLG Standby UPS System","details":None,"link":"https://www.newegg.com/p/N82E16842102117","price":74},{"id":"cp550slg","name":"CyberPower CP425SLG Standby UPS System","details":None,"link":"https://www.cyberpowersystems.com/product/ups/standby/cp550slg/","price":74}],"usage":"recommended","min_specs":None,"guide":"https://github.com/trevhub/guides/blob/main/CheapUPS.md"},{"name":"Smart Plug","index":6,"products":[{"id":"B091699Z3W","name":"Kasa Smart Plug Ultra Mini","details":None,"link":"https://www.amazon.com/dp/B091699Z3W","price":10}],"usage":"optional","min_specs":None,"guide":"https://www.wintips.org/setup-computer-to-auto-power-on-after-power-outage/"}]}
utilities.log(response, context=f"{script_id}__get_hardware_data")
return response
else:
Expand All @@ -26,26 +26,38 @@ def get_unavailable_products():

def process_hardware_data(hardware_data, unavailable_products):
updated_unavailable_products = ""
components_unavailable = []
for component in hardware_data:
component_products_unavailable = 0
for product in component["products"]:
if "link" in product:
response = requests.get(product["link"])
if (response.status_code == 200):
if "amazon.com" in product["link"]:
if "We don't know when or if this item will be back in stock.".encode() in response.content:
updated_unavailable_products += f'{product["id"]}, '
component_products_unavailable += 1
elif "newegg.com" in product["link"]:
if "out of stock".encode() in response.content:
updated_unavailable_products += f'{product["id"]}, '
component_products_unavailable += 1
elif (product["id"] in unavailable_products):
updated_unavailable_products += f'{product["id"]}, '
component_products_unavailable += 1
if component_products_unavailable == len(component["products"]):
components_unavailable.append(component["name"])
utilities.log(updated_unavailable_products, context=f"{script_id}__updated_unavailable_products")
# if len(updated_unavailable_products) > 0:
# msg = f"Hardware unavailable: {updated_unavailable_products}"
# utilities.sendDiscordMsg(utilities.DISCORD_WEBSITE_WEBHOOK, msg)
if len(components_unavailable) > 0:
msg = f"Hardware unavailable: {', '.join(components_unavailable)}"
utilities.sendDiscordMsg(utilities.DISCORD_WEBSITE_WEBHOOK, msg)
else:
print("no hardware unavailable")
return updated_unavailable_products

def save_unavailable_products_data(updated_unavailable_products):
if len(updated_unavailable_products) > 0:
msg = f"Hardware unavailable: {updated_unavailable_products}"
utilities.sendDiscordMsg(utilities.DISCORD_WEBSITE_WEBHOOK, msg)
utilities.save_to_file(f"_data/hardware-unavailable.txt", updated_unavailable_products, context=f"{script_id}__save_unavailable_products_data")


Expand Down
31 changes: 17 additions & 14 deletions _scripts/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,19 +193,22 @@ def pprint(data):


def sendDiscordMsg(webhook, msg):
data = {"content": msg}
attempts = 0
status = 0
try:
while (attempts < 3) and (status < 200 or status >= 300):
attempts += 1
r = requests.post(webhook, json=data)
status = r.status_code
except Exception as error:
report_error(error, f"sendDiscordMsg: {msg}")
if exit_on_report_error:
raise SystemExit(error)
else:
print(error)
if use_test_data:
print(msg)
else:
data = {"content": msg}
attempts = 0
status = 0
try:
while (attempts < 3) and (status < 200 or status >= 300):
attempts += 1
r = requests.post(webhook, json=data)
status = r.status_code
except Exception as error:
report_error(error, f"sendDiscordMsg: {msg}")
if exit_on_report_error:
raise SystemExit(error)
else:
print(error)


0 comments on commit 93e5758

Please sign in to comment.