From 1646e7f80e741ceadcd305ff411fda48120add69 Mon Sep 17 00:00:00 2001
From: Rolf Niepraschk <Rolf.Niepraschk@ptb.de>
Date: Mon, 26 Jul 2021 15:03:07 +0200
Subject: [PATCH] read releases json structure

---
 config.json |  4 +++-
 server.py   | 23 ++++++++++++++++++++++-
 utils.py    | 14 ++++++++++++++
 3 files changed, 39 insertions(+), 2 deletions(-)

diff --git a/config.json b/config.json
index a8cec25..8990e66 100644
--- a/config.json
+++ b/config.json
@@ -5,6 +5,8 @@
   },
   "xsd":{
     "filename":"dcc.xsd",
-    "externalBaseURL":"https://ptb.de/dcc/"
+    "externalBaseURL":"https://ptb.de/dcc/",
+    "localPath":"./xsd-local/",
+    "releases":"https://www.ptb.de/dcc/releases.json"
   }
 }
diff --git a/server.py b/server.py
index 05471f8..5ccce74 100644
--- a/server.py
+++ b/server.py
@@ -7,7 +7,8 @@ import subprocess
 from pprint import pprint
 
 config = utils.get_config_dict()
-
+releases_dict = utils.get_releases_dict(config['xsd']['releases'])
+ 
 app = Flask(__name__)
 CORS(app)
 trans = Trans()
@@ -70,6 +71,24 @@ def validation():
     if l != 'de':
         l = 'en'
     x = "['2.4.0','2.3.0','2.2.0','2.1.1','2.1.0']"
+    xx = '['
+    comma = ''
+    for i in releases_dict['releases']:
+        xx = xx + comma + '"' + i['version'] + '"'
+        comma = ','
+    xx = xx + ']' 
+    arr = []
+    for i in releases_dict['releases']:
+        arr.append('"' + i['version'] + '"')
+    arr = reversed(arr)# !!!
+    yy = '[' + ','.join(arr) + ']'
+    print('********************************')
+    pprint(xx)
+    pprint(yy)
+    print('********************************')
+    #pprint(releases_dict)
+    print('########################')
+#    x = utils.get_xsd_versions() ##############
     return trans.show_html(version=utils.get_version(), language=l, 
       xsd_versions=x)
 
@@ -90,3 +109,5 @@ def logo_folder(fn):
 
 if __name__ == '__main__':
     app.run(host=config['server']['host'], port=config['server']['port'])
+
+
diff --git a/utils.py b/utils.py
index cffec5b..21e987a 100644
--- a/utils.py
+++ b/utils.py
@@ -21,6 +21,20 @@ def get_config_dict():
 
     return config
 
+def get_releases_dict(url):
+    try:
+        filename = urlparse(url).path.split('/')[-1]
+        r = requests.get(url, allow_redirects=True)
+        if r.url.endswith(filename): # no bad redirection
+            try:
+                return json.loads(r.text) # loads !!!
+            except:
+                return False         
+        else:
+            return False
+    except:
+        return False  
+  
 def get_version():
     try:
         with open('./VERSION', 'r') as f:
-- 
GitLab