3Dshock/server/download.py

57 lines
1.5 KiB
Python
Raw Normal View History

2021-03-08 01:53:06 +00:00
import os
import settings
import threading
import paramiko
import time
paramiko.util.log_to_file('paramiko.log')
2021-03-11 05:24:18 +00:00
def fake_download(ip, dest):
2021-03-11 00:37:01 +00:00
import requests, random
2021-03-11 05:24:18 +00:00
num = ip.split('.')[3]
r = requests.get('https://picsum.photos/400/300?random=' + num)
2021-03-11 05:41:37 +00:00
time.sleep(random.uniform(2, 10))
2021-03-11 05:24:18 +00:00
with open(dest / (num + '_420.jpg'), 'wb') as f:
2021-03-11 00:37:01 +00:00
f.write(r.content)
2021-03-08 01:53:06 +00:00
def download(ip, dest):
2021-03-11 00:37:01 +00:00
if settings.DEBUG:
2021-03-11 05:24:18 +00:00
fake_download(ip, dest)
2021-03-11 00:37:01 +00:00
2021-03-08 01:53:06 +00:00
print('Downloading from', ip)
port = 22
transport = paramiko.Transport((ip, port))
transport.connect(None, settings.RASPBERRY_USER, settings.RASPBERRY_PASS)
sftp = paramiko.SFTPClient.from_transport(transport)
files = sftp.listdir('/3dscan/')
for f in files:
source_file = '/3dscan/' + f
2021-03-08 05:49:10 +00:00
dest_file = dest / (f + '.tmp')
2021-03-08 01:53:06 +00:00
print('Grabbing file', source_file)
sftp.get(source_file, dest_file)
2021-03-08 05:01:35 +00:00
sftp.remove(source_file)
2021-03-08 05:49:10 +00:00
done_file = dest / f
dest_file.rename(done_file)
2021-03-08 01:53:06 +00:00
if sftp: sftp.close()
if transport: transport.close()
print('Finished downloading from', ip)
def download_all_photos(dest):
2021-03-08 04:43:47 +00:00
if not dest.exists():
2021-03-08 01:53:06 +00:00
raise Exception('Destination does not exist')
print('Downloading all photos to', dest)
for ip in settings.RASPBERRY_IPS:
t = threading.Thread(target=download, args=(ip, dest))
t.start()
2021-03-08 04:43:47 +00:00
if __name__ == '__main__':
from pathlib import Path
download_all_photos(Path('test/'))