Jack 1 rok temu
rodzic
commit
da4d494b80
1 zmienionych plików z 35 dodań i 28 usunięć
  1. 35 28
      message/message_coin_detail.py

+ 35 - 28
message/message_coin_detail.py

@@ -8,7 +8,7 @@ import httpx
 from datetime import datetime
 from utils.utils_send_gotify import *
 
-retry_count = 10
+retry_count = 5
 
 
 def fetch_coin_data(target):
@@ -28,8 +28,10 @@ def fetch_coin_data(target):
             response = client.post(url, headers=headers, data=payload, timeout=3)
         except Exception as e:
             # print(f"Target: {target} failed to fetch data. error: {str(e)}")
+            client.close()
             return False
         if response.status_code != 200:
+            client.close()
             # print(f"{target} failed to fetch data. status code: {response.status_code}")
             return False
         else:
@@ -80,8 +82,10 @@ def fetch_vix_data():
             response = client.post(url, headers=headers, data=payload, timeout=3)
         except Exception as e:
             # print(f"failed to fetch VIX data. error: {str(e)}")
+            client.close()
             return False
         if response.status_code != 200:
+            client.close()
             # print(f"Failed to fetch VIX data. status code: {response.status_code}")
             return False
         else:
@@ -112,33 +116,36 @@ def fetch_gas_data():
         "User-Agent": "MathGas/1.0.1 (MathWallet.MathGas; build:3; macOS 13.5.0) Alamofire/5.4.4"
     }
 
-    response = httpx.get(url, headers=headers)
-    if response.status_code != 200:
-        print("Error:", response.status_code)
-        return False
-
-    if not response.json():
-        print("Not Find GAS Data. Error: No response")
-        return False
-
-    remove_last_n_chars = lambda n, n_chars=9: int(str(n)[:-n_chars]) if len(str(n)) > n_chars else n
-
-    result = '\nGAS:\n'
-
-    try:
-        data = response.json()['data']
-
-        fastest = remove_last_n_chars(data['fastest']['price'])
-        fast = remove_last_n_chars(data['fast']['price'])
-        standard = remove_last_n_chars(data['standard']['price'])
-        low = remove_last_n_chars(data['low']['price'])
-        base = remove_last_n_chars(data['base']['price'])
-        print(f'fastest: {fastest} - fast: {fast} - standard: {standard} - low: {low} - base: {base}')
-        result += f'fastest: {fastest}\nfast: {fast}\nstandard: {standard}\nlow: {low}\nbase: {base}'
-        return result
-    except Exception as e:
-        print(e)
-        return False
+    with httpx.Client() as client:
+        response = client.get(url, headers=headers)
+        if response.status_code != 200:
+            client.close()
+            print("Error:", response.status_code)
+            return False
+
+        if not response.json():
+            client.close()
+            print("Not Find GAS Data. Error: No response")
+            return False
+
+        remove_last_n_chars = lambda n, n_chars=9: int(str(n)[:-n_chars]) if len(str(n)) > n_chars else n
+
+        result = '\nGAS:\n'
+
+        try:
+            data = response.json()['data']
+
+            fastest = remove_last_n_chars(data['fastest']['price'])
+            fast = remove_last_n_chars(data['fast']['price'])
+            standard = remove_last_n_chars(data['standard']['price'])
+            low = remove_last_n_chars(data['low']['price'])
+            base = remove_last_n_chars(data['base']['price'])
+            print(f'fastest: {fastest} - fast: {fast} - standard: {standard} - low: {low} - base: {base}')
+            result += f'fastest: {fastest}\nfast: {fast}\nstandard: {standard}\nlow: {low}\nbase: {base}'
+            return result
+        except Exception as e:
+            print(e)
+            return False
 
 
 def main():