API + timestamp

This commit is contained in:
k3nny 2023-03-07 22:58:42 +01:00
parent 4b319e3f40
commit 0249299111
2 changed files with 24 additions and 21 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.vscode/

44
main.py
View File

@ -1,6 +1,8 @@
import requests
import json
import pprint
from datetime import datetime
from pytz import timezone
import psycopg
from psycopg.rows import dict_row
import semver
@ -21,8 +23,6 @@ class jru:
def _db_connect(self,db_config):
try:
#pprint.pprint(db_config)
#print(db_config['db']['server'])
conn = psycopg.connect(
host=db_config['db']['server'],
dbname=db_config['db']['name'],
@ -39,7 +39,6 @@ class jru:
def _get_version_from_db(self,db_conn):
cur = db_conn.cursor(row_factory=dict_row)
qres = cur.execute("select * from jru.java_versions").fetchall()
#print(qres)
if qres:
return qres
else:
@ -49,15 +48,25 @@ class jru:
def _get_version_semver_from_db(self,java_version):
cur = self.db_conn.cursor(row_factory=dict_row)
qres = cur.execute("select latest_release_packaged from jru.java_versions where java_version='{}'".format(java_version)).fetchall()
#print(qres)
if qres:
return qres
else:
return []
def _update_version_in_db(self,java_version,java_semver):
self.db_conn.execute("update jru.java_versions set latest_release_packaged = '{}' where java_version = '{}'".format(java_semver,java_version))
def _update_version_in_db(self,java_version,java_semver,update_timestampz):
self.db_conn.execute("update jru.java_versions set latest_release_packaged = '{}',latest_release_packaged_timestamp = '{}' where java_version = '{}'".format(java_semver,update_timestampz,java_version))
self.db_conn.commit()
def _get_version_from_api(self):
url = "https://api.adoptium.net/v3/info/available_releases"
try:
r = requests.get(url,headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36','Accept': 'application/json'})
available_lts = r.json()["available_lts_releases"]
available_lts_str = [str(i) for i in available_lts]
except Exception as err_json_request:
print(err_json_request)
r = {}
return available_lts_str
def _get_version_from_json(self):
return self.db_config["java_settings"]["enabled_version"]
@ -68,7 +77,6 @@ class jru:
try:
print(" - requesting adoptium : {}".format(url))
r = requests.get(url,headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36','Accept': 'application/json'})
#pprint.pprint(r.json())
except Exception as err_json_request:
print(err_json_request)
r = {}
@ -78,30 +86,26 @@ class jru:
print("Retrieve Java versions from DB : ")
java_db_version = self._get_version_from_db(self.db_conn)
java_db_version_list = [x["java_version"] for x in java_db_version]
# if java_db_version_list:
# for db_vers in java_db_version_list:
# print(' - {}'.format(db_vers))
# else:
# print(" - no version in database yet!")
print("Retrieve Java versions from JSON : ")
print("Retrieve Java versions from API : ")
java_config_version = self._get_version_from_json()
#java_config_version = self._get_version_from_json()
java_config_version = self._get_version_from_api()
# Inserting in database first time
for json_vers in java_config_version:
print(' - {}'.format(json_vers))
if json_vers not in java_db_version_list:
print('Java {} is not in database yet!'.format(json_vers))
print('- Java {} is not in database yet!'.format(json_vers))
self.db_conn.execute("insert into jru.java_versions (java_version,latest_release_packaged) values ({},'{}.0.0')".format(json_vers,json_vers))
else:
print('Java {} already found in database'.format(json_vers))
print('- Java {} already found in database'.format(json_vers))
self.db_conn.commit()
#updating java_version from database
java_db_version = self._get_version_from_db(self.db_conn)
java_db_version_list = [x["java_version"] for x in java_db_version]
update_timestampz = datetime.now(timezone("Europe/Paris"))
for db_version in java_db_version_list:
print("\nProcessing Java {}".format(db_version))
api_version_info = self._get_latest_release_info(db_version)
@ -109,11 +113,9 @@ class jru:
db_version_semver = self._get_version_semver_from_db(db_version)[0]["latest_release_packaged"]
if semver.VersionInfo.parse(api_version_info_semver) > semver.VersionInfo.parse(db_version_semver):
print(" - New version available! - {} > {}".format(api_version_info_semver,db_version_semver))
self._update_version_in_db(db_version,api_version_info_semver)
self._update_version_in_db(db_version,api_version_info_semver,update_timestampz)
else:
print(' - No upgrade needed! API {} == DB {}'.format(api_version_info_semver,db_version_semver))
if __name__ == "__main__":
jru_main = jru()