-
Notifications
You must be signed in to change notification settings - Fork 4
/
sdput
executable file
·53 lines (46 loc) · 2.01 KB
/
sdput
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#! /usr/bin/env python3
# put files on Toshiba FlashAir SD card via wi-fi
# see https://www.flashair-developers.com/en/documents/api/
import os
import sys
import argparse
import datetime
import configparser
import flashair
import httpx
from tqdm import tqdm
from tqdm.utils import CallbackIOWrapper
card=flashair.card()
card.parser.add_argument('-d', '--directory', help='directory', default=card.def_directory)
card.parser.add_argument('-p', '--progress', help='hide progress bar', default=True, action='store_false')
card.parser.add_argument('files', nargs='+', help='files to upload')
card.setup()
# keep the host from writing during upload
httpx.get('http://'+card.address+'/upload.cgi?WRITEPROTECT=ON')
# set upload directory
httpx.get('http://'+card.address+'/upload.cgi?UPDIR='+card.dir)
for file in card.args.files:
# get modification time of file as unix timestamp in milliseconds
mtime=os.stat(file).st_mtime
# convert from unix timestamp to date
ts=datetime.datetime.fromtimestamp(mtime)
# convert from date to msdos date & time
d, t=card.date2ftime(ts)
# set timestamp of file
bytes=hex((d << 16)+t)
httpx.get('http://'+card.address+'/upload.cgi?FTIME='+bytes)
if card.args.progress:
# upload file with progress bar
with open(file, "rb") as data:
with tqdm(desc=file, total=os.stat(file).st_size, unit="B", unit_scale=True, unit_divisor=1024) as progress:
wrapped_file=CallbackIOWrapper(progress.update, data, "read")
r=httpx.post('http://'+card.address+'/upload.cgi',
files={'file': (os.path.basename(file), wrapped_file, 'application/octet-stream')})
else:
# upload file
r=httpx.post('http://'+card.address+'/upload.cgi',
files={'file': (os.path.basename(file), open(file, 'rb'), 'application/octet-stream')})
if not '<h1>Success</h1>' in r.text:
print(file, 'failed')
# re-enable disk
httpx.get('http://'+card.address+'/upload.cgi?WRITEPROTECT=OFF')