import os
import sys
import paramiko

password = os.environ.get('IX_PASSWORD')
if not password:
    print('IX_PASSWORD env var not set', file=sys.stderr)
    sys.exit(1)

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('172.16.3.10', username='root', password=password,
               look_for_keys=False, allow_agent=False, timeout=30)
sftp = client.open_sftp()

os.makedirs('test-data/episodes', exist_ok=True)

downloads = [
    ('/home/gurushow/public_html/archive/2010/COMPUTER GURU 5-8-10 hour 1.mp3', 'test-data/episodes/2010-05-08-hr1.mp3'),
    ('/home/gurushow/public_html/archive/2011/3-12-11 HR 1.mp3',                'test-data/episodes/2011-03-12-hr1.mp3'),
    ('/home/gurushow/public_html/archive/2012/3 - March/3-10-12HR1.mp3',        'test-data/episodes/2012-03-10-hr1.mp3'),
    ('/home/gurushow/public_html/archive/2012/6 - June/6-9-12-HR1.mp3',         'test-data/episodes/2012-06-09-hr1.mp3'),
    ('/home/gurushow/public_html/archive/2014/06/s6e19.mp3',                    'test-data/episodes/2014-s6e19.mp3'),
    ('/home/gurushow/public_html/archive/2015/01/s7e19.mp3',                    'test-data/episodes/2015-s7e19.mp3'),
    ('/home/gurushow/public_html/archive/2016/06/s8e43.mp3',                    'test-data/episodes/2016-s8e43.mp3'),
    ('/home/gurushow/public_html/archive/2017/04/s9e30.mp3',                    'test-data/episodes/2017-s9e30.mp3'),
    ('/home/gurushow/public_html/archive/2018/01/s10e18.mp3',                   'test-data/episodes/2018-s10e18.mp3'),
]

for remote, local in downloads:
    if os.path.exists(local):
        print(f'[skip] {local} already exists')
        continue
    size_mb = sftp.stat(remote).st_size / 1024 / 1024
    print(f'Downloading {local} ({size_mb:.1f} MB)...', flush=True)
    sftp.get(remote, local)
    print('  done', flush=True)

sftp.close()
client.close()
print('All downloads complete.')
