Преглед изворни кода

Added Leslie, refactor to cater multiple targets

Foppe Hemminga пре 6 година
родитељ
комит
2098cf4850
5 измењених фајлова са 81 додато и 56 уклоњено
  1. 40 38
      main.py
  2. 19 16
      model.py
  3. 10 0
      status-15.json
  4. 10 0
      status-4.json
  5. 2 2
      view.py

+ 40 - 38
main.py

@@ -8,42 +8,44 @@ import view
 
 
 if __name__ == '__main__':
-    status = model.get_loot_level()
-    # print(status)
-    old_status = 0
-    write_file_next = False
-    try:
-        old_status_file = open('status.json', 'r')
-        if old_status_file.mode == 'r':
-            old_status_raw = old_status_file.read()
-            if old_status_raw:
-                old_status_json = json.loads(old_status_raw)
-                old_status = int(old_status_json['integer'])
-            else:
-                write_file_next = True
-        old_status_file.close()
-    except FileNotFoundError:
-        empty_status_file = open('status.json', 'w+')
-        print('Created new file ... Exiting')
-        empty_status_file.close()
-        sys.exit(1)
-    # print(old_status)
-    send_message = False
-    # print('if old_status {} != status[1] {}'.format(old_status, status[1]))
-    assert type(old_status) is int, f"old_status {old_status} is not an integer: {type(old_status)}"
-    assert type(status['integer']) is int, f"status['integer'] {status['integer']} is not an integer: {type(status['integer'])}"
-    if old_status != status['integer']:
-        write_file_next = True
-        send_message = True
-    # print('write_file_next: {}'.format(write_file_next))
-    if write_file_next:
-        # summary = {'text': status["status"], 'roman': status["roman"], 'integer': status["integer"]}
-        pprint(status)
-        summary_json = json.dumps(status, indent=2)
-        print(summary_json)
-        write_file = open('status.json', 'w')
-        write_file.write(summary_json)
-        write_file.close()
+    for id in [4, 15]:
+        status = model.get_loot_level(id)
+        # print(status)
+        old_status = 0
+        write_file_next = False
+        try:
+            old_status_file = open(f'status-{id}.json', 'r')
+            if old_status_file.mode == 'r':
+                old_status_raw = old_status_file.read()
+                if old_status_raw:
+                    old_status_json = json.loads(old_status_raw)
+                    old_status = int(old_status_json['integer'])
+                else:
+                    write_file_next = True
+            old_status_file.close()
+        except FileNotFoundError:
+            empty_status_file = open(f'status-{id}.json', 'w+')
+            print('Created new file ... Exiting')
+            empty_status_file.close()
+            sys.exit(1)
+        # print(old_status)
+        send_message = False
+        # print('if old_status {} != status[1] {}'.format(old_status, status[1]))
+        assert type(old_status) is int, f"old_status {old_status} is not an integer: {type(old_status)}"
+        assert type(status['integer']) is int, f"status['integer'] {status['integer']} is not an integer: {type(status['integer'])}"
+        if old_status != status['integer']:
+            write_file_next = True
+            send_message = True
+        # print('write_file_next: {}'.format(write_file_next))
+        if write_file_next:
+            # summary = {'text': status["status"], 'roman': status["roman"], 'integer': status["integer"]}
+            pprint(status)
+            summary_json = json.dumps(status, indent=2)
+            print(summary_json)
+            write_file = open(f'status-{id}.json', 'w')
+            write_file.write(summary_json)
+            write_file.close()
+            write_file.close()
 
-    if send_message:
-        view.send_message(status['integer'])
+        if send_message:
+            view.send_message(status)

+ 19 - 16
model.py

@@ -1,5 +1,5 @@
 import json
-from pprint import pprint
+# from pprint import pprint
 
 from dotenv import load_dotenv
 import os
@@ -18,32 +18,32 @@ def _get_api_key():
     return torn_api_key
 
 
-def _get_url():
+def _get_url(this_id):
     torn_api_key = _get_api_key()
-    url = "https://api.torn.com/user/4?selections=basic&key="
+    url = f"https://api.torn.com/user/{this_id}?selections=basic&key="
     url = url+torn_api_key
     return url
 
 
-def _get_json():
-    this_url = _get_url()
+def _get_json(this_id):
+    this_url = _get_url(this_id)
     response = requests.get(this_url)
     return response
 
 
-def _load_json():
-    this_response = _get_json()
+def _load_json(this_id):
+    this_response = _get_json(this_id)
     this_json = json.loads(this_response.text)
     return this_json
 
 
-def _get_status():
-    this_json = _load_json()
+def _get_status(this_id):
+    this_json = _load_json(this_id)
     this_status = this_json
     # pprint(this_status)
     status_return = None
     if this_status:
-        status_return = this_status['status']
+        status_return = this_status['status'], this_status['name']
     return status_return
 
 
@@ -59,18 +59,21 @@ def _from_roman_to_integer(roman_number):
         integer = 4
     elif roman_number == 'V':
         integer = 5
+    elif roman_number == '0' or roman_number == 0 or roman_number == 'N':
+        integer = 0
     else:
         print('Unknown Roman number: {}'.format(roman_number))
     return integer
 
 
-def get_loot_level():
+def get_loot_level(this_id):
     this_loot_level = 0
-    this_status = _get_status()
-    if this_status and len(this_status) > 1:
-        if this_status[0] == 'Okay' and 'Loot level' in this_status[1]:
-            this_loot_level = this_status[1][11:].strip()
+    this_status_status, this_status_name = _get_status(this_id)
+    if this_status_status and len(this_status_status) > 1:
+        if this_status_status[0] == 'Okay' and 'Loot level' in this_status_status[1]:
+            this_loot_level = this_status_status[1][11:].strip()
     # print(this_loot_level)
     this_integer = _from_roman_to_integer(this_loot_level)
-    this_status_dict = {"integer": this_integer, "roman": this_loot_level, "text": this_status}
+    this_status_dict = {"id": this_id, "name": this_status_name, "integer": this_integer, "roman": this_loot_level,
+                        "text": this_status_status}
     return this_status_dict

+ 10 - 0
status-15.json

@@ -0,0 +1,10 @@
+{
+  "id": 15,
+  "name:": "Leslie",
+  "integer": 3,
+  "roman": "III",
+  "text": [
+    "Okay",
+    "Loot level III"
+  ]
+}

+ 10 - 0
status-4.json

@@ -0,0 +1,10 @@
+{
+  "id": 4,
+  "name:": "Duke",
+  "integer": 0,
+  "roman": 0,
+  "text": [
+    "In hospital for 1 hrs 23 mins ",
+    "Defeated by <a href=\"profiles.php?XID=2157516\">AlexSosa</a>"
+  ]
+}

+ 2 - 2
view.py

@@ -13,8 +13,8 @@ def send_message(status):
     load_dotenv()
     webhooks_json = os.environ["DISCORD_TOKENS"]
     webhooks = json.loads(webhooks_json)
-
-    status_text = 'Duke Loot level changed to '+roman.toRoman(status)
+    name = status['name']
+    status_text = f"{name} Loot level changed to "+roman.toRoman(status['integer'])
     this_json = {'content': status_text}
     pprint(this_json)
     for webhook in webhooks: