feat: add day as a folder

This commit is contained in:
bigfish 2023-04-01 00:22:49 +08:00
parent d9eb67503c
commit 6276c1fafc

View File

@ -6,6 +6,7 @@
''' '''
import os import os
import sys
from posixpath import abspath from posixpath import abspath
import exifread import exifread
import time import time
@ -37,7 +38,7 @@ class Classifier():
pass pass
def connect_database(self): def connect_database(self):
self.db = pymysql.connect(host='bt.biggerfish.tech', user='admin', password='zhiyong214', database='photo_classifier') self.db = pymysql.connect(host='138.2.66.248', user='admin', password='zhiyong214', database='photo_classifier')
def close_database(self): def close_database(self):
self.db.close() self.db.close()
@ -116,8 +117,8 @@ class Classifier():
md5 = self.get_md5(file_path) md5 = self.get_md5(file_path)
try: try:
self.validate(file_path, md5) self.validate(file_path, md5)
year, month = self.read_date(file_path) year, month, day = self.read_date(file_path)
new_name = self.rename_move(file_path, year, month) new_name = self.rename_move(file_path, year, month, day)
self.add_record(md5) self.add_record(md5)
self.processed_count += 1 self.processed_count += 1
print('已处理 {}: {} --> {}'.format(self.processed_count, file, new_name)) print('已处理 {}: {} --> {}'.format(self.processed_count, file, new_name))
@ -159,10 +160,11 @@ class Classifier():
key = keys[0] key = keys[0]
origin_date = tags[key] origin_date = tags[key]
time_str = str(origin_date) time_str = str(origin_date)
_date = time_str[:7].split(':') _date = time_str[:10].split(':')
year = _date[0] year = _date[0]
month = _date[1] month = _date[1]
return (year, month) day = _date[2]
return (year, month, day)
return None return None
def get_video_create_date(self, file): def get_video_create_date(self, file):
@ -170,10 +172,11 @@ class Classifier():
properties = propsys.SHGetPropertyStoreFromParsingName(file) properties = propsys.SHGetPropertyStoreFromParsingName(file)
dt = properties.GetValue(pscon.PKEY_Media_DateEncoded).GetValue() dt = properties.GetValue(pscon.PKEY_Media_DateEncoded).GetValue()
time_str = str(dt.astimezone(pytz.timezone('Asia/Shanghai'))) time_str = str(dt.astimezone(pytz.timezone('Asia/Shanghai')))
_date = time_str[:7].split('-') _date = time_str[:10].split('-')
year = _date[0] year = _date[0]
month = _date[1] month = _date[1]
return (year, month) day = _date[2]
return (year, month, day)
except: except:
return None return None
@ -188,13 +191,14 @@ class Classifier():
if not date: # 获取文件上次修改日期 if not date: # 获取文件上次修改日期
time_str = os.path.getmtime(file) time_str = os.path.getmtime(file)
time_str = str(datetime.datetime.fromtimestamp(time_str)) time_str = str(datetime.datetime.fromtimestamp(time_str))
_date = time_str[:7].split('-') _date = time_str[:10].split('-')
year = _date[0] year = _date[0]
month = _date[1] month = _date[1]
date = (year, month) day = _date[2]
date = (year, month, day)
return date return date
def rename_move(self, file_path, year, month): def rename_move(self, file_path, year, month, day):
if self.is_image(file_path): if self.is_image(file_path):
if self.is_photo(file_path): if self.is_photo(file_path):
output = self.photo_output output = self.photo_output
@ -205,19 +209,20 @@ class Classifier():
else: else:
raise Exception('移动文件失败, 非图片或视频: {}'.format(file_path)) raise Exception('移动文件失败, 非图片或视频: {}'.format(file_path))
new_path = os.path.join(output, year, month) new_path = os.path.join(output, year, month, day)
if not os.path.exists(new_path): if not os.path.exists(new_path):
os.makedirs(new_path) os.makedirs(new_path)
file_name, file_ext = os.path.splitext(file_path) file_name, file_ext = os.path.splitext(file_path)
new_name = year + '-' + month + '-' + str(time.time()) + file_ext new_name = year + '-' + month + '-' + day + '-' + str(time.time()) + file_ext
shutil.move(file_path, os.path.join(new_path, new_name)) shutil.move(file_path, os.path.join(new_path, new_name))
return new_name return new_name
cf = Classifier(input_folder='D:/temp/相册', cf = Classifier(
photo_output='D:/总仓库-照片视频/总照片备份', input_folder='D:/temp/相册',
video_output='D:/总仓库-照片视频/总视频备份', # input_folder='z:/待分类照片视频/Picture',
image_output='D:/总仓库-照片视频/总图片备份') photo_output='D:/总仓库-照片视频/总照片备份',
video_output='D:/总仓库-照片视频/总视频备份',
image_output='D:/总仓库-照片视频/总图片备份')
# cf.create_table()
cf.start() cf.start()