from urllib.request import urlopen
import os
import re
import wget
import tarfile

cwd = os.getcwd()

url = 'https://trac.chirp.danplanet.com/chirp_daily/LATEST/'

html_content = urlopen(url).read()

html_content = html_content.decode('utf-8')
matches = re.findall('chirp-daily-\d\d\d\d\d\d\d\d.tar.gz', html_content);
release = matches[0][12:20]
url = url + matches[0]

if os.path.isfile(f'{cwd}/dailychirp.tar.gz'):
    os.system('rm dailychirp.tar.gz')

if not os.path.isdir(f'{cwd}/chirp-daily-{release}'):
    wget.download(url, f'{cwd}/dailychirp.tar.gz')
    tf = tarfile.open('dailychirp.tar.gz')
    tf.extractall()
extractedfilename = 'chirp-daily-' + release
os.system(f'{cwd}/{extractedfilename}/chirpw')