浏览代码

Working version

Foppe Hemminga 6 年之前
父节点
当前提交
8a0fe7a6b8
共有 6 个文件被更改,包括 149 次插入4 次删除
  1. 2 0
      .gitignore
  2. 44 0
      main.py
  3. 71 3
      model.py
  4. 3 1
      requirements.txt
  5. 1 0
      status.json
  6. 28 0
      view.py

+ 2 - 0
.gitignore

@@ -2,3 +2,5 @@
 venv/
 .idea/$CACHE_FILE$
 .idea/inspectionProfiles/
+__pycache__/
+.idea/

+ 44 - 0
main.py

@@ -0,0 +1,44 @@
+#! venv/bin/python
+import json
+import sys
+
+import view as view
+
+import model
+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
+    if old_status != status[1]:
+        write_file_next = True
+        send_message = True
+    if write_file_next:
+        summary = {'text': status[2], 'roman': status[0], 'integer': status[1]}
+        summary_json = json.dumps(summary, indent=2)
+        write_file = open('status.json', 'w')
+        write_file.write(summary_json)
+        write_file.close()
+
+    if send_message:
+        view.send_message(status['integer'])

+ 71 - 3
model.py

@@ -1,7 +1,75 @@
+import json
+from pprint import pprint
+
 from dotenv import load_dotenv
 import os
-
+import random
+import requests
 
 # Develop only
-load_dotenv()
-torn_api_key = os.environ('TORN_API_KEY')
+load_dotenv(verbose=True)
+
+
+def _get_api_key():
+    torn_api_keys = json.loads(os.environ['TORN_API_KEY'])
+    pprint(torn_api_keys)
+    torn_api_key = random.choice(torn_api_keys)
+    print(torn_api_key)
+    return torn_api_key
+
+
+def _get_url():
+    torn_api_key = _get_api_key()
+    url = "https://api.torn.com/user/4?selections=basic&key="
+    url = url+torn_api_key
+    return url
+
+
+def _get_json():
+    this_url = _get_url()
+    response = requests.get(this_url)
+    return response
+
+
+def _load_json():
+    this_response = _get_json()
+    this_json = json.loads(this_response.text)
+    return this_json
+
+
+def _get_status():
+    this_json = _load_json()
+    this_status = this_json
+    # pprint(this_status)
+    status_return = None
+    if this_status:
+        status_return = this_status['status']
+    return status_return
+
+
+def _from_roman_to_integer(roman_number):
+    integer = 0
+    if roman_number == 'I':
+        integer = 1
+    elif roman_number == 'II':
+        integer = 2
+    elif roman_number == 'III':
+        integer = 3
+    elif roman_number == 'IV':
+        integer = 4
+    elif roman_number == 'V':
+        integer = 5
+    else:
+        print('Unknown Roman number: {}'.format(roman_number))
+    return integer
+
+
+def get_loot_level():
+    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()
+    # print(this_loot_level)
+    this_integer = _from_roman_to_integer(this_loot_level)
+    return this_loot_level,this_integer,this_status

+ 3 - 1
requirements.txt

@@ -1,2 +1,4 @@
+roman
 # dotenv
-python-dotenv
+python-dotenv
+requests

+ 1 - 0
status.json

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

+ 28 - 0
view.py

@@ -0,0 +1,28 @@
+import os
+import sys
+from pprint import pprint
+import json
+
+from dotenv import load_dotenv
+import roman
+import requests
+
+
+def send_message(status):
+    base_url = 'https://discordapp.com/api/webhooks/'
+    load_dotenv()
+    webhooks_json = os.environ["DISCORD_TOKENS"]
+    webhooks = json.loads(webhooks_json)
+
+    status_text = 'Duke Loot level changed to '+roman.toRoman(status)
+    this_json = {'content': status_text}
+    pprint(this_json)
+    for webhook in webhooks:
+        url = base_url+webhook
+        response = requests.post(url, json=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)