Files
auto_import/auto_import.py
2025-06-27 21:59:21 +02:00

138 lines
5.3 KiB
Python

import os
from posixpath import ismount
import sys
import time
import subprocess
import datetime
import shutil
import json
_DEST = "/media/nas/nas-photo/imported"
_DEV = ""
drives = ["6362-3938"]
myCmd = "ls /dev/disk/by-uuid"
status, output = subprocess.getstatusoutput(myCmd)
input(output)
for l in output.splitlines():
if l in drives:
print(l)
input("????")
_DEV_MOUNT = f"/mnt/{l}"
_DEV_DIR = f"{_DEV_MOUNT}/DCIM"
_IMP_FILE = f"{_DEV_MOUNT}/auto_import"
_DEV_DATA = f"{_DEV_MOUNT}/MISC/version.txt"
print("###############")
#sys.exit()
while True:
if os.path.ismount(_DEV_MOUNT):
if os.path.exists(_DEV_DATA):
file = open(_DEV_DATA, 'r')
content = file.read()
file.close()
strToReplace = ','
replacementStr = ''
strToReplaceReversed = strToReplace[::-1]
replacementStrReversed = replacementStr[::-1]
strValue = content[::-1].replace(strToReplaceReversed, replacementStrReversed, 1)[::-1]
c = content.replace("\n","")
print("--------------")
print(c[-2])
print("--------------")
print(strValue)
_DEV_CONT = json.loads(strValue)
print(_DEV_CONT)
print(_DEV_CONT["wifi mac"])
wifimac = _DEV_CONT["wifi mac"]
if wifimac == "2474f742c017":
_DEV = "gopro9_1"
print("gopro9_1 registered")
elif wifimac == "2474f7421111":
_DEV = "dji_mini_4"
print("dji_mini_4 registered")
if os.path.exists(_IMP_FILE) and _DEV == "":
print("path exist")
time.sleep(5)
if _DEV == "":
file = open(_IMP_FILE, 'r')
content = file.read()
_DEV = json.loads(content)["device"]
file.close()
x = datetime.datetime.now()
print(x.year)
_DAT = x.strftime("%Y%m%d")
if len(os.listdir(_DEV_DIR)) == 0:
print("Folder is empty!")
myCmd = "umount /share/external/DEV3304_1"
status, output = subprocess.getstatusoutput(myCmd)
time.sleep(5)
continue
# myCmd = "/sbin/hal_app --se_buzzer enc_id=0,mode=101;sleep 1;/sbin/hal_app --se_buzzer enc_id=0,mode=101;sleep 1;/sbin/hal_app --se_buzzer enc_id=0,mode=101"
# status, output = subprocess.getstatusoutput(myCmd)
if not _DEV == "":
if os.path.exists(_DEV_DIR):
for file_or_dir in os.listdir(_DEV_DIR):
FULL_PATH = os.path.join(_DEV_DIR, file_or_dir)
print(f"Full path : {FULL_PATH}")
if os.path.isdir(FULL_PATH):
print(f"Folder: {file_or_dir}")
for filename in os.listdir(FULL_PATH):
print(filename)
c_time = os.path.getctime("{}/{}".format(FULL_PATH,filename))
dt_c = datetime.datetime.fromtimestamp(c_time)
print(dt_c)
_CDAT = dt_c.strftime("%Y-%m-%d")
print(_CDAT)
_FDEST = "{}/{}/{}".format(_DEST,_DEV, _CDAT)
if not os.path.exists(_FDEST):
cmnd = "mkdir -p {}".format(_FDEST)
status, output = subprocess.getstatusoutput(cmnd)
print("Moving file {} into {}".format(filename, _FDEST))
try:
shutil.move("{}/{}".format(FULL_PATH,filename),_FDEST)
except:
try:
shutil.move("{}/{}".format(FULL_PATH,filename),"{}/{}".format(_FDEST,filename))
except:
print("failed to move file {}".format(filename))
print("Umounting")
myCmd = "umount /share/external/DEV3304_1"
status, output = subprocess.getstatusoutput(myCmd)
print(output)
print(status)
myCmd = "/sbin/hal_app --se_buzzer enc_id=0,mode=100"
subprocess.getstatusoutput(myCmd)
time.sleep(5)
else:
print(f"File: {file_or_dir}")
try:
shutil.move("{}/{}".format(_DEV_DIR,file_or_dir),_FDEST)
except:
try:
shutil.move("{}/{}".format(_DEV_DIR,file_or_dir),"{}/{}".format(_FDEST,file_or_dir))
except:
print("failed to move file {}".format(file_or_dir))
else:
print("Nothing to import")
time.sleep(5)
else:
print("Unknown device")
time.sleep(5)
else:
print("Usb does not connected")
time.sleep(5)