2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

RasspberryPiでNASを作りたい

Last updated at Posted at 2024-03-10

Raspberrypi 3B+を購入したのでiPhoneからショートカットを使ってアップロードしラズパイ経由でHDDに保存するNASを作成した

Python
import os
import time
import shutil
import pysns_tool
import pykit_tool
import logging
import subprocess

def get_exif_info(fle):
    try:
        exif_output = subprocess.check_output(["exiftool", fle]).decode("utf-8")
        return exif_output
    except subprocess.CalledProcessError as e:
        exif_output = f"Exif情報の取得中にエラーが発生しました: {e}"
        
        return exif_output

    

def watch_folder(source_folder, destination_folder):
    while True:
        i = 2
        if os.path.exists(destination_folder):
            files = os.listdir(source_folder)
            for file in files:
                file_path = os.path.join(source_folder, file)
                if os.path.isfile(file_path) and time.time() - os.path.getctime(file_path) > 1:
                    try:
                        shutil.move(file_path, destination_folder)
                        txt = f"SuccessMove[{file}]{destination_folder}"
                        print(txt)
                        logging.info(logging.INFO, txt)
                        return file
                    except shutil.Error as e:
                        errfile = "media/alredy"+str(i)+file
                        print()
                        os.rename(file_path,errfile)
                        txt =f"already[{file}]is[{errfile}]{e}"
                        logging.error(txt)
                        print(txt)
                        i += 1
                        
                    except Exception as e:
                        txt =f"Error!!:[{file}]{e}"
                        logging.error(txt)
                        print(txt)
                        input("")
                        
        else:
            print("Destination folder does not exist.")
        time.sleep(5)

# 監視するフォルダと移動先のフォルダを指定
source_folder = "media/iPhonefld"
destination_folder = "media/savefld"
pykit_tool.setup_logging("backup.log")
while True:
        
    # フォルダを監視し、ファイルが追加されたら移動する
    fle = watch_folder(source_folder, destination_folder)
    print("追加されたファイル:", fle)


    
    last_dot_index = fle.rfind(".")
    extension = "" if last_dot_index == -1 else fle[last_dot_index + 1:]
    print(extension)
    if extension != "heic":        
        getjson = pysns_tool.get_json(key="discord")
        pysns_tool.send_discord(getjson,"",destination_folder + "/" + fle)
    print("fle情報書き込み")
    txt = f"{destination_folder}:{fle}"
    logging.info( txt)

    time.sleep(10)

んーできたけどコードが汚い

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?