Last active
October 8, 2020 09:28
-
-
Save vjrj/f366238d9262acd9d49233fab9913c12 to your computer and use it in GitHub Desktop.
A script to get LA component versions via BuildInfo and meta
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import sys | |
import csv | |
import urllib2 | |
import re | |
import json | |
from bs4 import BeautifulSoup | |
csv.register_dialect('pipes', delimiter='|') | |
wikiPage = 'https://raw.githubusercontent.com/wiki/AtlasOfLivingAustralia' \ | |
'/documentation/LA-Nodes-and-Components.md' | |
response = urllib2.urlopen(wikiPage) | |
content = response.read() | |
fSplit = content.split('<!--tstart-->', 2)[-1] | |
csvWithBlankLines = fSplit.split('<!--tends-->', 2)[0] | |
csvWOBlank = "".join([s for s in csvWithBlankLines.strip().splitlines(True) | |
if s.strip("\r\n").strip()]) | |
csv_reader = csv.reader(csvWOBlank.splitlines(), dialect='pipes') | |
for row in csv_reader: | |
pos = 0 | |
for column in row: | |
pos += 1 | |
match = re.search(r'\[.{1,4}\]\((.*)\)', column) | |
if match: | |
serviceUrl = match.group(1) | |
serviceBuildInfoUrl = serviceUrl + '/buildInfo' | |
rread = "" | |
version = "" | |
try: | |
outputUrl = serviceBuildInfoUrl + '?format=json' | |
rread = urllib2.urlopen(outputUrl, timeout=5).read() | |
data = json.loads(rread) | |
version = data['runtimeEnvironment']['app.version'] | |
except: | |
# Maybe 'format=json' does not work, try html table parsing | |
outputUrl = serviceBuildInfoUrl | |
try: | |
soup = BeautifulSoup(rread, "lxml") | |
table_data = [[cell.text for cell in row("td")] | |
for row in soup("tr")] | |
parseBuild = json.loads(json.dumps(dict(table_data))) | |
version = parseBuild["App version"] | |
except: | |
try: | |
# Try to read meta | |
soup = BeautifulSoup(urllib2.urlopen(serviceUrl, | |
timeout=5).read(), | |
"lxml") | |
for tag in soup.find_all("meta"): | |
if tag.get("name") == "app.version": | |
version = tag.get("content") | |
if not version: | |
version = "?" | |
outputUrl = serviceUrl | |
except: | |
version = "?" | |
outputUrl = serviceUrl | |
print("| <sub> [{0}]({1})</sub>".format(version, outputUrl)), | |
sys.stdout.flush() | |
else: | |
if (pos > 1): | |
print("| <sub>{0}</sub> ".format(column.strip())), | |
sys.stdout.flush() | |
# end of row | |
print("") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment