I'm trying to figure out how to download the most recent file from a server using wget on my Linux system. The files are ssl certs in zip archives for some services i.e. prometheus.my.domain-09.28.2020, alertmanager.my.domain-09.28.2020, my.domain-07.28.2020, etc. The date of its addition to the server is nailed to the name of the file. Currently, i have python script that helps me to cope with the task, but due to the presence of crutches, I want to switch to bash.
import os
import zipfile
from datetime import datetime
import dateutil.parser as dparser
import requests
from bs4 import BeautifulSoup
url = 'https://myserver@domain/ssl/'
download_dir = '/home/my/Downloads' # Specify download folder!
###################################################################################
cert = {}
soup = BeautifulSoup(requests.get(url).content, 'html.parser')
for tag in soup.find_all('a'):
t = tag.get('href')
if t != '../':
date = int(dparser.parse(t, fuzzy=True).timestamp())
domain = t.split('-')[0]
if domain not in cert:
cert[domain] = [date, t]
if cert[domain][0] < date:
cert[domain][0] = date
cert[domain][1] = t
for key, value in cert.items():
address = url + value[1]
downloaded_obj = requests.get(address)
dir = os.path.join(download_dir, f'{key}')
if not os.path.exists(dir):
os.mkdir(dir)
temp_path = os.path.join(dir, value[1])
with open(f"{temp_path}", "wb") as file:
file.write(downloaded_obj.content)
time = str(datetime.fromtimestamp(value[0]).date())
unzip_path = os.path.join(dir, key + '-' + time)
zipfile.ZipFile(temp_path).extractall(unzip_path)
print(f"{value[1]} downloaded and extracted to {unzip_path} folder")
Aucun commentaire:
Enregistrer un commentaire