上記のLuaスクリプトを用いた自動ファイルダウンロードサンプル.
FlashAirのどこに置かれたファイルでも,隠しファイルでも関係なくダウンロードします.
(日本語ファイルはダウンロードできません.失敗します)
# -*- coding: utf-8 -*-
import requests
import codecs
import time
import csv
import pprint
import os, sys
flashairUrl = "http://192.168.0.1"
checkdir = "/"
lastupdatetime = 0
oldflist = ""
def waitForUpdate():
global lastupdatetime
while True:
try:
res = requests.get(flashairUrl+"/command.cgi?op=121", timeout=3.0).text
except:
res = "-1"
#print("Last updated:"+res+"[sec]", end="\n")
if(int(res) > lastupdatetime): #updated
lastupdatetime = int(res)
break
time.sleep(1)
def getUpdatedFilePath():
global oldflist
waitForUpdate()
time.sleep(1) #Wait for FAT Finalize
try:
flist = requests.get(flashairUrl+"/filelist.lua", timeout=15.0).text
except:
return []
#print(flist)
src_set = set(flist.splitlines()) #リストに変換→集合に変換
tag_set = set(oldflist.splitlines()) #リストに変換→集合に変換
matched_list = list(src_set - tag_set) #以前含まれなかったものを抽出
#pprint.pprint(matched_list)
#First time
if(oldflist == ""):
oldflist = flist
return;
oldflist = flist
updatedFileList = []
for row in matched_list:
# updatetime = row.split(":")[0]
filepath = row.split(":")[1]
updatedFileList.append(filepath)
return updatedFileList
def listdownload(flist):
if(not os.path.exists("download")):
os.mkdir("download")
for url in flist:
print("Downloading... "+flashairUrl+url)
download(flashairUrl+url,"download/")
#download function: https://torina.top/detail/161/
def download(url,distdir):
file_name = url.split("/")[-1]
res = requests.get(url, stream=True)
if res.status_code == 200:
with open(distdir+file_name, 'wb') as file:
for chunk in res.iter_content(chunk_size=1024):
file.write(chunk)
def main():
print("FlashAir updated file download script with filelist.lua v0.1")
print("init...")
getUpdatedFilePath()
while True:
print("wait...")
flist = getUpdatedFilePath()
print("detected!")
print(flist)
listdownload(flist)
if __name__ == "__main__":
main()