API + timestamp
This commit is contained in:
parent
4b319e3f40
commit
0249299111
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
.vscode/
|
44
main.py
44
main.py
@ -1,6 +1,8 @@
|
|||||||
import requests
|
import requests
|
||||||
import json
|
import json
|
||||||
import pprint
|
import pprint
|
||||||
|
from datetime import datetime
|
||||||
|
from pytz import timezone
|
||||||
import psycopg
|
import psycopg
|
||||||
from psycopg.rows import dict_row
|
from psycopg.rows import dict_row
|
||||||
import semver
|
import semver
|
||||||
@ -21,8 +23,6 @@ class jru:
|
|||||||
|
|
||||||
def _db_connect(self,db_config):
|
def _db_connect(self,db_config):
|
||||||
try:
|
try:
|
||||||
#pprint.pprint(db_config)
|
|
||||||
#print(db_config['db']['server'])
|
|
||||||
conn = psycopg.connect(
|
conn = psycopg.connect(
|
||||||
host=db_config['db']['server'],
|
host=db_config['db']['server'],
|
||||||
dbname=db_config['db']['name'],
|
dbname=db_config['db']['name'],
|
||||||
@ -39,7 +39,6 @@ class jru:
|
|||||||
def _get_version_from_db(self,db_conn):
|
def _get_version_from_db(self,db_conn):
|
||||||
cur = db_conn.cursor(row_factory=dict_row)
|
cur = db_conn.cursor(row_factory=dict_row)
|
||||||
qres = cur.execute("select * from jru.java_versions").fetchall()
|
qres = cur.execute("select * from jru.java_versions").fetchall()
|
||||||
#print(qres)
|
|
||||||
if qres:
|
if qres:
|
||||||
return qres
|
return qres
|
||||||
else:
|
else:
|
||||||
@ -49,15 +48,25 @@ class jru:
|
|||||||
def _get_version_semver_from_db(self,java_version):
|
def _get_version_semver_from_db(self,java_version):
|
||||||
cur = self.db_conn.cursor(row_factory=dict_row)
|
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()
|
qres = cur.execute("select latest_release_packaged from jru.java_versions where java_version='{}'".format(java_version)).fetchall()
|
||||||
#print(qres)
|
|
||||||
if qres:
|
if qres:
|
||||||
return qres
|
return qres
|
||||||
else:
|
else:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
def _update_version_in_db(self,java_version,java_semver):
|
def _update_version_in_db(self,java_version,java_semver,update_timestampz):
|
||||||
self.db_conn.execute("update jru.java_versions set latest_release_packaged = '{}' where java_version = '{}'".format(java_semver,java_version))
|
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()
|
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):
|
def _get_version_from_json(self):
|
||||||
return self.db_config["java_settings"]["enabled_version"]
|
return self.db_config["java_settings"]["enabled_version"]
|
||||||
@ -68,7 +77,6 @@ class jru:
|
|||||||
try:
|
try:
|
||||||
print(" - requesting adoptium : {}".format(url))
|
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'})
|
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:
|
except Exception as err_json_request:
|
||||||
print(err_json_request)
|
print(err_json_request)
|
||||||
r = {}
|
r = {}
|
||||||
@ -78,30 +86,26 @@ class jru:
|
|||||||
print("Retrieve Java versions from DB : ")
|
print("Retrieve Java versions from DB : ")
|
||||||
java_db_version = self._get_version_from_db(self.db_conn)
|
java_db_version = self._get_version_from_db(self.db_conn)
|
||||||
java_db_version_list = [x["java_version"] for x in java_db_version]
|
java_db_version_list = [x["java_version"] for x in java_db_version]
|
||||||
# if java_db_version_list:
|
print("Retrieve Java versions from API : ")
|
||||||
# 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 : ")
|
|
||||||
|
|
||||||
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
|
# Inserting in database first time
|
||||||
for json_vers in java_config_version:
|
for json_vers in java_config_version:
|
||||||
print(' - {}'.format(json_vers))
|
|
||||||
if json_vers not in java_db_version_list:
|
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))
|
self.db_conn.execute("insert into jru.java_versions (java_version,latest_release_packaged) values ({},'{}.0.0')".format(json_vers,json_vers))
|
||||||
else:
|
else:
|
||||||
print('Java {} already found in database'.format(json_vers))
|
print('- Java {} already found in database'.format(json_vers))
|
||||||
self.db_conn.commit()
|
self.db_conn.commit()
|
||||||
|
|
||||||
#updating java_version from database
|
#updating java_version from database
|
||||||
java_db_version = self._get_version_from_db(self.db_conn)
|
java_db_version = self._get_version_from_db(self.db_conn)
|
||||||
java_db_version_list = [x["java_version"] for x in java_db_version]
|
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:
|
for db_version in java_db_version_list:
|
||||||
print("\nProcessing Java {}".format(db_version))
|
print("\nProcessing Java {}".format(db_version))
|
||||||
api_version_info = self._get_latest_release_info(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"]
|
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):
|
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))
|
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:
|
else:
|
||||||
print(' - No upgrade needed! API {} == DB {}'.format(api_version_info_semver,db_version_semver))
|
print(' - No upgrade needed! API {} == DB {}'.format(api_version_info_semver,db_version_semver))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
jru_main = jru()
|
jru_main = jru()
|
||||||
|
Loading…
Reference in New Issue
Block a user