diff --git a/lnbits/extensions/diagonalley/crud.py b/lnbits/extensions/diagonalley/crud.py index 43403cfc..df7caebe 100644 --- a/lnbits/extensions/diagonalley/crud.py +++ b/lnbits/extensions/diagonalley/crud.py @@ -1,11 +1,18 @@ from base64 import urlsafe_b64encode from uuid import uuid4 from typing import List, Optional, Union - +import requests from lnbits.db import open_ext_db - +from lnbits.settings import WALLET from .models import Products, Orders, Indexers - +import re +regex = re.compile( + r'^(?:http|ftp)s?://' # http:// or https:// + r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' + r'localhost|' + r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' + r'(?::\d+)?' + r'(?:/?|[/?]\S+)$', re.IGNORECASE) ###Products @@ -23,6 +30,16 @@ def create_diagonalleys_product(*, wallet_id: str, product: str, categories: st return get_diagonalleys_product(product_id) +def update_diagonalleys_product(product_id: str, **kwargs) -> Optional[Indexers]: + q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()]) + + with open_ext_db("diagonalley") as db: + db.execute(f"UPDATE products SET {q} WHERE id = ?", (*kwargs.values(), product_id)) + row = db.fetchone("SELECT * FROM products WHERE id = ?", (product_id,)) + + return get_diagonalleys_indexer(product_id) + + def get_diagonalleys_product(product_id: str) -> Optional[Products]: with open_ext_db("diagonalley") as db: row = db.fetchone("SELECT * FROM products WHERE id = ?", (product_id,)) @@ -50,25 +67,46 @@ def delete_diagonalleys_product(product_id: str) -> None: ###Indexers -def create_diagonalleys_indexer(*, wallet_id: str, shopname: str, indexeraddress: str, shippingzone1: str, shippingzone2: str, zone1cost: int, zone2cost: int, email: str) -> Indexers: +def create_diagonalleys_indexer(wallet_id: str, shopname: str, indexeraddress: str, shippingzone1: str, shippingzone2: str, zone1cost: int, zone2cost: int, email: str) -> Indexers: with open_ext_db("diagonalley") as db: indexer_id = urlsafe_b64encode(uuid4().bytes_le).decode('utf-8') - rating_key = urlsafe_b64encode(uuid4().bytes_le).decode('utf-8') db.execute( """ - INSERT INTO indexers (id, wallet, shopname, indexeraddress, ratingkey, rating, shippingzone1, shippingzone2, zone1cost, zone2cost, email) + INSERT INTO indexers (id, wallet, shopname, indexeraddress, online, rating, shippingzone1, shippingzone2, zone1cost, zone2cost, email) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) """, - (indexer_id, wallet_id, shopname, indexeraddress, rating_key, 100, shippingzone1, shippingzone2, zone1cost, zone2cost, email), + (indexer_id, wallet_id, shopname, indexeraddress, False, 0, shippingzone1, shippingzone2, zone1cost, zone2cost, email), ) + return get_diagonalleys_indexer(indexer_id) + + +def update_diagonalleys_indexer(indexer_id: str, **kwargs) -> Optional[Indexers]: + q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()]) + + with open_ext_db("diagonalley") as db: + db.execute(f"UPDATE indexers SET {q} WHERE id = ?", (*kwargs.values(), indexer_id)) + row = db.fetchone("SELECT * FROM indexers WHERE id = ?", (indexer_id,)) return get_diagonalleys_indexer(indexer_id) def get_diagonalleys_indexer(indexer_id: str) -> Optional[Indexers]: + with open_ext_db("diagonalley") as db: + roww = db.fetchone("SELECT * FROM indexers WHERE id = ?", (indexer_id,)) + try: + x = requests.get(roww["indexeraddress"] + "/" + roww["ratingkey"]) + if x.status_code == 200: + print(x) + print("poo") + with open_ext_db("diagonalley") as db: + db.execute("UPDATE indexers SET online = ? WHERE id = ?", (True, indexer_id,)) + else: + with open_ext_db("diagonalley") as db: + db.execute("UPDATE indexers SET online = ? WHERE id = ?", (False, indexer_id,)) + except: + print("An exception occurred") with open_ext_db("diagonalley") as db: row = db.fetchone("SELECT * FROM indexers WHERE id = ?", (indexer_id,)) - return Indexers(**row) if row else None @@ -80,6 +118,20 @@ def get_diagonalleys_indexers(wallet_ids: Union[str, List[str]]) -> List[Indexer q = ",".join(["?"] * len(wallet_ids)) rows = db.fetchall(f"SELECT * FROM indexers WHERE wallet IN ({q})", (*wallet_ids,)) + for r in rows: + try: + x = requests.get(r["indexeraddress"] + "/" + r["ratingkey"]) + if x.status_code == 200: + with open_ext_db("diagonalley") as db: + db.execute("UPDATE indexers SET online = ? WHERE id = ?", (True, r["id"],)) + else: + with open_ext_db("diagonalley") as db: + db.execute("UPDATE indexers SET online = ? WHERE id = ?", (False, r["id"],)) + except: + print("An exception occurred") + with open_ext_db("diagonalley") as db: + q = ",".join(["?"] * len(wallet_ids)) + rows = db.fetchall(f"SELECT * FROM indexers WHERE wallet IN ({q})", (*wallet_ids,)) return [Indexers(**row) for row in rows] @@ -119,7 +171,12 @@ def get_diagonalleys_orders(wallet_ids: Union[str, List[str]]) -> List[Orders]: with open_ext_db("diagonalley") as db: q = ",".join(["?"] * len(wallet_ids)) rows = db.fetchall(f"SELECT * FROM orders WHERE wallet IN ({q})", (*wallet_ids,)) - + for r in rows: + PAID = WALLET.get_invoice_status(r["invoiceid"]).paid + if PAID: + with open_ext_db("diagonalley") as db: + db.execute("UPDATE orders SET paid = ? WHERE id = ?", (True, r["id"],)) + rows = db.fetchall(f"SELECT * FROM orders WHERE wallet IN ({q})", (*wallet_ids,)) return [Orders(**row) for row in rows]