Просмотр исходного кода

Refactor Discord tokens into a dict

Foppe Hemminga 6 лет назад
Родитель
Сommit
1ad179657e
2 измененных файлов с 11 добавлено и 11 удалено
  1. 3 1
      model.py
  2. 8 10
      view.py

+ 3 - 1
model.py

@@ -1,5 +1,5 @@
 import json
-# from pprint import pprint
+from pprint import pprint
 import re
 
 import arrow
@@ -47,6 +47,8 @@ def _get_status(this_id):
     # pprint(this_status)
     status_return = None
     if 'alert' not in this_status:
+        print('alert not in this_status')
+        pprint(this_status)
         this_status['alert'] = {'15': False, '10': False, '5': False, '1': False}
     if this_status:
         status_return = this_status['status'], this_status['name'], this_status['timestamp'], this_status['alert']

+ 8 - 10
view.py

@@ -11,9 +11,6 @@ base_url = 'https://discordapp.com/api/webhooks/'
 
 
 def send_message(status):
-    load_dotenv()
-    webhooks_json = os.environ["DISCORD_TOKENS"]
-    webhooks = json.loads(webhooks_json)
     name = status['name']
     its_id = status['id']
     status_text = f"[{name} [{its_id}]](https://www.torn.com/profiles.php?XID={its_id}) → " + \
@@ -21,19 +18,20 @@ def send_message(status):
     status_text += "\n" + status['time_string']
     this_json = {'content': status_text}
     pprint(this_json)
-    _send(webhooks, this_json)
+    _send(this_json)
 
 
 def send_alert(this_alert):
-    load_dotenv()
-    webhooks_json = os.environ["DISCORD_TOKENS"]
-    webhooks = json.loads(webhooks_json)
     this_json = {'content': this_alert}
-    _send(webhooks, this_json)
+    _send(this_json)
 
 
-def _send(send_webhooks, send_json):
-    for webhook in send_webhooks:
+def _send(send_json):
+    load_dotenv()
+    webhooks_json = os.environ["DISCORD_TOKENS"]
+    webhooks = json.loads(webhooks_json)
+    for webhook_key in webhooks:
+        webhook = webhooks[webhook_key]
         url = base_url+webhook
         response = requests.post(url, json=send_json)
         if response.status_code in [200, 204]: