Browse Source

added request_active_invasions()

Fabian Peter Hammerle 8 years ago
parent
commit
cdf6bf4c05
2 changed files with 35 additions and 1 deletions
  1. 1 1
      setup.py
  2. 34 0
      tooncher/__init__.py

+ 1 - 1
setup.py

@@ -7,7 +7,7 @@ import glob
 setup(
     name = 'tooncher',
     packages = ['tooncher'],
-    version = '0.2.0',
+    version = '0.3.0',
     description = "automates toontown rewritten's login process",
     author = 'Fabian Peter Hammerle',
     author_email = 'fabian.hammerle@gmail.com',

+ 34 - 0
tooncher/__init__.py

@@ -1,3 +1,4 @@
+import datetime
 import json
 import os
 import ssl
@@ -127,3 +128,36 @@ def launch(engine_path, username, password, validate_ssl_certs=True):
         p.wait()
     else:
         raise Exception(repr(result))
+
+
+class InvasionProgress:
+
+    def __init__(self, district, date, cog_type,
+                 despawned_number, total_number):
+        self.district = district
+        self.date = date
+        self.cog_type = cog_type
+        self.despawned_number = despawned_number
+        self.total_number = total_number
+
+    @property
+    def remaining_number(self):
+        return self.total_number - self.despawned_number
+
+
+def request_active_invasions(validate_ssl_certs=True):
+    resp_data = api_request(INVASIONS_API_URL)
+    if resp_data['error'] is not None:
+        raise Exception(resp_data['error'])
+    else:
+        invs = {}
+        for district, inv_data in resp_data['invasions'].items():
+            despawned_number, total_number = inv_data['progress'].split('/')
+            invs[district] = InvasionProgress(
+                district=district,
+                date=datetime.datetime.utcfromtimestamp(inv_data['asOf']),
+                cog_type=inv_data['type'],
+                despawned_number=int(despawned_number),
+                total_number=int(total_number),
+            )
+        return invs