Browse Source

Add alert

Foppe Hemminga 6 năm trước cách đây
mục cha
commit
d9d55dffcb
2 tập tin đã thay đổi với 14 bổ sung17 xóa
  1. 2 2
      main.py
  2. 12 15
      view.py

+ 2 - 2
main.py

@@ -57,11 +57,11 @@ if __name__ == '__main__':
         print(t)
         status['time_string'] = t
 
-        if 0 < minutes_to_go < 6:
+        if minutes_to_go < 20 and minutes_to_go % 5 == 0:
             name_alert = status['name']
             id_alert = status['id']
             url_alert = f'https://www.torn.com/profiles.php?XID={id_alert}'
-            alert = f"Alert: {minutes_to_go} to attack [{name_alert} \[{id_alert}\]]({url_alert})!"
+            alert = f"Alert: {minutes_to_go} to attack [{name_alert} \[{id_alert}\]]({url_alert}). Be there {attack_time_time}!"
             view.send_alert(alert)
 
         if send_message:

+ 12 - 15
view.py

@@ -7,41 +7,38 @@ from dotenv import load_dotenv
 import roman
 import requests
 
+base_url = 'https://discordapp.com/api/webhooks/'
+
 
 def send_message(status):
-    base_url = 'https://discordapp.com/api/webhooks/'
     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}) → Loot level changed to "+roman.toRoman(status['integer'])
+    status_text = f"[{name} \[{its_id}\]](https://www.torn.com/profiles.php?XID={its_id}) → " + \
+                  "Loot level changed to "+roman.toRoman(status['integer'])
     status_text += "\n" + status['time_string']
     this_json = {'content': status_text}
     pprint(this_json)
-    for webhook in webhooks:
-        url = base_url+webhook
-        response = requests.post(url, json=this_json)
-        if response.status_code in [200, 204]:
-            print("Webhook executed")
-        else:
-            print("status code {}: {}".format(response.status_code, response.content.decode("utf-8")))
-            if response.status_code == int(429):
-                sys.exit(1)
+    _send(webhooks, this_json)
 
 
 def send_alert(this_alert):
-    base_url = 'https://discordapp.com/api/webhooks/'
     load_dotenv()
     webhooks_json = os.environ["DISCORD_TOKENS"]
     webhooks = json.loads(webhooks_json)
     this_json = {'content': this_alert}
-    for webhook in webhooks:
+    _send(webhooks, this_json)
+
+
+def _send(send_webhooks, send_json):
+    for webhook in send_webhooks:
         url = base_url+webhook
-        response = requests.post(url, json=this_json)
+        response = requests.post(url, json=send_json)
         if response.status_code in [200, 204]:
             print("Webhook executed")
         else:
             print("status code {}: {}".format(response.status_code, response.content.decode("utf-8")))
             if response.status_code == int(429):
-                sys.exit(1)
+                sys.exit(1)