feat: use korea vps as DB
This commit is contained in:
parent
9e556f5e15
commit
c1b2c52a82
@ -1,9 +1,9 @@
|
|||||||
'''
|
"""
|
||||||
根据读取的照片信息分类照片
|
根据读取的照片信息分类照片
|
||||||
分类:
|
分类:
|
||||||
目录名:2020\01
|
目录名:2020\01
|
||||||
文件名:2020-01-时间戳
|
文件名:2020-01-时间戳
|
||||||
'''
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
@ -18,16 +18,16 @@ import pytz
|
|||||||
from win32com.propsys import propsys, pscon
|
from win32com.propsys import propsys, pscon
|
||||||
|
|
||||||
|
|
||||||
class Classifier1():
|
class Classifier1:
|
||||||
mode = 'prod' # 开发模式(dev)还是产品模式(prod)
|
mode = "prod" # 开发模式(dev)还是产品模式(prod)
|
||||||
IMAGE_EXTENTIONS = ['jpg', 'jpeg', 'bmp', 'png', 'tif', 'gif', 'heic']
|
IMAGE_EXTENTIONS = ["jpg", "jpeg", "bmp", "png", "tif", "gif", "heic"]
|
||||||
VIDEO_EXTENTIONS = ['mp4', 'avi', 'rmvb', 'mkv', 'mov', 'amr', 'mpg']
|
VIDEO_EXTENTIONS = ["mp4", "avi", "rmvb", "mkv", "mov", "amr", "mpg"]
|
||||||
TEST_TABLE = 'TEST_PHOTO'
|
TEST_TABLE = "TEST_PHOTO"
|
||||||
TABLE = 'PHOTO'
|
TABLE = "PHOTO"
|
||||||
PHOTO_NO_DATE_KEYS = ['EXIF ExifVersion']
|
PHOTO_NO_DATE_KEYS = ["EXIF ExifVersion"]
|
||||||
PHOTO_DATE_KEYS = ['Image DateTime', 'EXIF DateTimeOriginal']
|
PHOTO_DATE_KEYS = ["Image DateTime", "EXIF DateTimeOriginal"]
|
||||||
PHOTO_EXIF_KEYS = PHOTO_NO_DATE_KEYS + PHOTO_DATE_KEYS
|
PHOTO_EXIF_KEYS = PHOTO_NO_DATE_KEYS + PHOTO_DATE_KEYS
|
||||||
SKIP_FOLDERS = ['System Volume Information', '$RECYCLE.BIN', '.stfolder']
|
SKIP_FOLDERS = ["System Volume Information", "$RECYCLE.BIN", ".stfolder"]
|
||||||
|
|
||||||
def __init__(self, input_folder, photo_output, video_output, image_output):
|
def __init__(self, input_folder, photo_output, video_output, image_output):
|
||||||
self.input = input_folder
|
self.input = input_folder
|
||||||
@ -35,11 +35,17 @@ class Classifier1():
|
|||||||
self.video_output = video_output
|
self.video_output = video_output
|
||||||
self.image_output = image_output
|
self.image_output = image_output
|
||||||
self.processed_count = 0
|
self.processed_count = 0
|
||||||
self.table = self.TEST_TABLE if self.mode == 'dev' else self.TABLE
|
self.table = self.TEST_TABLE if self.mode == "dev" else self.TABLE
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def connect_database(self):
|
def connect_database(self):
|
||||||
self.db = pymysql.connect(host='northflyfish.myqnapcloud.cn', user='admin', password='zhiyong@214', database='photo_classifier', port=3307)
|
self.db = pymysql.connect(
|
||||||
|
host="northflyfish.myqnapcloud.cn",
|
||||||
|
user="admin",
|
||||||
|
password="zhiyong@214",
|
||||||
|
database="photo_classifier",
|
||||||
|
port=3307,
|
||||||
|
)
|
||||||
|
|
||||||
def close_database(self):
|
def close_database(self):
|
||||||
self.db.close()
|
self.db.close()
|
||||||
@ -48,17 +54,19 @@ class Classifier1():
|
|||||||
self.connect_database()
|
self.connect_database()
|
||||||
cursor = self.db.cursor()
|
cursor = self.db.cursor()
|
||||||
|
|
||||||
sql = 'DROP TABLE IF EXISTS {}'.format(self.table)
|
sql = "DROP TABLE IF EXISTS {}".format(self.table)
|
||||||
cursor.execute(sql)
|
cursor.execute(sql)
|
||||||
print('删除表 {}'.format(self.table))
|
print("删除表 {}".format(self.table))
|
||||||
|
|
||||||
sql = '''CREATE TABLE {} (
|
sql = """CREATE TABLE {} (
|
||||||
ID INT NOT NULL AUTO_INCREMENT ,
|
ID INT NOT NULL AUTO_INCREMENT ,
|
||||||
MD5 VARCHAR(255) NOT NULL ,
|
MD5 VARCHAR(255) NOT NULL ,
|
||||||
PRIMARY KEY (ID), UNIQUE (MD5))
|
PRIMARY KEY (ID), UNIQUE (MD5))
|
||||||
ENGINE = InnoDB;'''.format(self.table)
|
ENGINE = InnoDB;""".format(
|
||||||
|
self.table
|
||||||
|
)
|
||||||
cursor.execute(sql)
|
cursor.execute(sql)
|
||||||
print('创建表 {}'.format(self.table))
|
print("创建表 {}".format(self.table))
|
||||||
|
|
||||||
self.close_database()
|
self.close_database()
|
||||||
|
|
||||||
@ -70,12 +78,12 @@ class Classifier1():
|
|||||||
|
|
||||||
def get_file_count(self, folder):
|
def get_file_count(self, folder):
|
||||||
count = 0
|
count = 0
|
||||||
for (_, _, _files) in os.walk(folder):
|
for _, _, _files in os.walk(folder):
|
||||||
count += len(_files)
|
count += len(_files)
|
||||||
return count
|
return count
|
||||||
|
|
||||||
def delete_folders(self, folder):
|
def delete_folders(self, folder):
|
||||||
for (root, dirs, files) in os.walk(folder):
|
for root, dirs, files in os.walk(folder):
|
||||||
for dir in dirs:
|
for dir in dirs:
|
||||||
if dir in self.SKIP_FOLDERS:
|
if dir in self.SKIP_FOLDERS:
|
||||||
continue
|
continue
|
||||||
@ -83,7 +91,7 @@ class Classifier1():
|
|||||||
if os.path.isdir(abs_path):
|
if os.path.isdir(abs_path):
|
||||||
if self.get_file_count(abs_path) == 0:
|
if self.get_file_count(abs_path) == 0:
|
||||||
shutil.rmtree(abs_path)
|
shutil.rmtree(abs_path)
|
||||||
print('删除目录: {}'.format(abs_path))
|
print("删除目录: {}".format(abs_path))
|
||||||
|
|
||||||
def is_photo(self, file_name):
|
def is_photo(self, file_name):
|
||||||
return self.is_image(file_name) and self.contains_exif(file_name)
|
return self.is_image(file_name) and self.contains_exif(file_name)
|
||||||
@ -101,18 +109,18 @@ class Classifier1():
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
def contains_exif(self, file_name):
|
def contains_exif(self, file_name):
|
||||||
with open(file_name, 'rb') as reader:
|
with open(file_name, "rb") as reader:
|
||||||
tags = exifread.process_file(reader)
|
tags = exifread.process_file(reader)
|
||||||
keys = [key for key in self.PHOTO_EXIF_KEYS if key in tags]
|
keys = [key for key in self.PHOTO_EXIF_KEYS if key in tags]
|
||||||
return len(keys) > 0
|
return len(keys) > 0
|
||||||
|
|
||||||
def process_folder(self, folder):
|
def process_folder(self, folder):
|
||||||
for (root, dirs, files) in os.walk(folder):
|
for root, dirs, files in os.walk(folder):
|
||||||
for file in files:
|
for file in files:
|
||||||
self.process_file(root, file)
|
self.process_file(root, file)
|
||||||
|
|
||||||
def get_md5(self, file):
|
def get_md5(self, file):
|
||||||
with open(file, 'rb') as reader:
|
with open(file, "rb") as reader:
|
||||||
return hashlib.md5(reader.read()).hexdigest()
|
return hashlib.md5(reader.read()).hexdigest()
|
||||||
|
|
||||||
def process_file(self, root, file):
|
def process_file(self, root, file):
|
||||||
@ -125,11 +133,13 @@ class Classifier1():
|
|||||||
new_name = self.rename_move(file_path, year, month, day, md5)
|
new_name = self.rename_move(file_path, year, month, day, md5)
|
||||||
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)
|
||||||
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(str(e))
|
print(str(e))
|
||||||
else:
|
else:
|
||||||
print('非图片或视频, 忽略文件: {}'.format(file_path))
|
print("非图片或视频, 忽略文件: {}".format(file_path))
|
||||||
|
|
||||||
def add_record(self, md5):
|
def add_record(self, md5):
|
||||||
try:
|
try:
|
||||||
@ -138,7 +148,7 @@ class Classifier1():
|
|||||||
cursor.execute(sql)
|
cursor.execute(sql)
|
||||||
self.db.commit()
|
self.db.commit()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print('插入记录 {} 到数据库photo_classifier失败: {}'.format(md5, str(e)))
|
print("插入记录 {} 到数据库photo_classifier失败: {}".format(md5, str(e)))
|
||||||
self.db.rollback()
|
self.db.rollback()
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
@ -149,24 +159,24 @@ class Classifier1():
|
|||||||
sql = "SELECT MD5 FROM {} WHERE MD5='{}'".format(self.table, md5)
|
sql = "SELECT MD5 FROM {} WHERE MD5='{}'".format(self.table, md5)
|
||||||
cursor.execute(sql)
|
cursor.execute(sql)
|
||||||
record = cursor.fetchone()
|
record = cursor.fetchone()
|
||||||
if str(record) != 'None':
|
if str(record) != "None":
|
||||||
os.remove(file_path)
|
os.remove(file_path)
|
||||||
raise Exception('重复文件 {} --> 删除'.format(file_path))
|
raise Exception("重复文件 {} --> 删除".format(file_path))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
if (not self.is_image(file_path)) and (not self.is_video(file_path)):
|
if (not self.is_image(file_path)) and (not self.is_video(file_path)):
|
||||||
raise Exception('非图片或视频: {} --> 跳过'.format(file_path))
|
raise Exception("非图片或视频: {} --> 跳过".format(file_path))
|
||||||
|
|
||||||
def get_photo_create_date(self, file):
|
def get_photo_create_date(self, file):
|
||||||
with open(file, 'rb') as reader:
|
with open(file, "rb") as reader:
|
||||||
tags = exifread.process_file(reader)
|
tags = exifread.process_file(reader)
|
||||||
keys = [key for key in self.PHOTO_DATE_KEYS if key in tags]
|
keys = [key for key in self.PHOTO_DATE_KEYS if key in tags]
|
||||||
if len(keys) > 0:
|
if len(keys) > 0:
|
||||||
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[:10].split(':')
|
_date = time_str[:10].split(":")
|
||||||
year = _date[0]
|
year = _date[0]
|
||||||
month = _date[1]
|
month = _date[1]
|
||||||
day = _date[2]
|
day = _date[2]
|
||||||
@ -177,8 +187,8 @@ class Classifier1():
|
|||||||
try:
|
try:
|
||||||
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[:10].split('-')
|
_date = time_str[:10].split("-")
|
||||||
year = _date[0]
|
year = _date[0]
|
||||||
month = _date[1]
|
month = _date[1]
|
||||||
day = _date[2]
|
day = _date[2]
|
||||||
@ -187,7 +197,7 @@ class Classifier1():
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
def read_date(self, file):
|
def read_date(self, file):
|
||||||
file = file.replace('/', '\\')
|
file = file.replace("/", "\\")
|
||||||
date = None
|
date = None
|
||||||
if self.is_photo(file):
|
if self.is_photo(file):
|
||||||
date = self.get_photo_create_date(file) # 照片可能没有EXIF日期
|
date = self.get_photo_create_date(file) # 照片可能没有EXIF日期
|
||||||
@ -197,7 +207,7 @@ class Classifier1():
|
|||||||
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[:10].split('-')
|
_date = time_str[:10].split("-")
|
||||||
year = _date[0]
|
year = _date[0]
|
||||||
month = _date[1]
|
month = _date[1]
|
||||||
day = _date[2]
|
day = _date[2]
|
||||||
@ -213,23 +223,24 @@ class Classifier1():
|
|||||||
elif self.is_video(file_path):
|
elif self.is_video(file_path):
|
||||||
output = self.video_output
|
output = self.video_output
|
||||||
else:
|
else:
|
||||||
raise Exception('移动文件失败, 非图片或视频: {}'.format(file_path))
|
raise Exception("移动文件失败, 非图片或视频: {}".format(file_path))
|
||||||
|
|
||||||
new_path = os.path.join(output, year, month, day)
|
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 + '-' + day + '-' + md5 + file_ext
|
new_name = year + "-" + month + "-" + day + "-" + md5 + 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 = Classifier1(
|
cf = Classifier1(
|
||||||
input_folder='D:/待分类照片视频1',
|
input_folder="D:/待分类照片视频1",
|
||||||
# input_folder='z:/待分类照片视频/Picture',
|
# input_folder='z:/待分类照片视频/Picture',
|
||||||
photo_output='D:/总仓库-照片视频1/总照片备份',
|
photo_output="D:/总仓库-照片视频1/总照片备份",
|
||||||
video_output='D:/总仓库-照片视频1/总视频备份',
|
video_output="D:/总仓库-照片视频1/总视频备份",
|
||||||
image_output='D:/总仓库-照片视频1/总图片备份')
|
image_output="D:/总仓库-照片视频1/总图片备份",
|
||||||
|
)
|
||||||
|
|
||||||
cf.start()
|
cf.start()
|
||||||
# cf.create_table()
|
# cf.create_table()
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
'''
|
"""
|
||||||
根据读取的照片信息分类照片
|
根据读取的照片信息分类照片
|
||||||
分类:
|
分类:
|
||||||
目录名:2020\01
|
目录名:2020\01
|
||||||
文件名:2020-01-时间戳
|
文件名:2020-01-时间戳
|
||||||
'''
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
@ -17,17 +17,19 @@ import datetime
|
|||||||
import pytz
|
import pytz
|
||||||
from win32com.propsys import propsys, pscon
|
from win32com.propsys import propsys, pscon
|
||||||
|
|
||||||
|
# 使用韩国oracle 数据库
|
||||||
|
|
||||||
class Classifier():
|
|
||||||
mode = 'prod' # 开发模式(dev)还是产品模式(prod)
|
class Classifier:
|
||||||
IMAGE_EXTENTIONS = ['jpg', 'jpeg', 'bmp', 'png', 'tif', 'gif', 'heic']
|
mode = "prod" # 开发模式(dev)还是产品模式(prod)
|
||||||
VIDEO_EXTENTIONS = ['mp4', 'avi', 'rmvb', 'mkv', 'mov', 'amr', 'mpg']
|
IMAGE_EXTENTIONS = ["jpg", "jpeg", "bmp", "png", "tif", "gif", "heic"]
|
||||||
TEST_TABLE = 'TEST_PHOTO'
|
VIDEO_EXTENTIONS = ["mp4", "avi", "rmvb", "mkv", "mov", "amr", "mpg"]
|
||||||
TABLE = 'PHOTO'
|
TEST_TABLE = "TEST_PHOTO"
|
||||||
PHOTO_NO_DATE_KEYS = ['EXIF ExifVersion']
|
TABLE = "PHOTO"
|
||||||
PHOTO_DATE_KEYS = ['Image DateTime', 'EXIF DateTimeOriginal']
|
PHOTO_NO_DATE_KEYS = ["EXIF ExifVersion"]
|
||||||
|
PHOTO_DATE_KEYS = ["Image DateTime", "EXIF DateTimeOriginal"]
|
||||||
PHOTO_EXIF_KEYS = PHOTO_NO_DATE_KEYS + PHOTO_DATE_KEYS
|
PHOTO_EXIF_KEYS = PHOTO_NO_DATE_KEYS + PHOTO_DATE_KEYS
|
||||||
SKIP_FOLDERS = ['System Volume Information', '$RECYCLE.BIN', '.stfolder']
|
SKIP_FOLDERS = ["System Volume Information", "$RECYCLE.BIN", ".stfolder"]
|
||||||
|
|
||||||
def __init__(self, input_folder, photo_output, video_output, image_output):
|
def __init__(self, input_folder, photo_output, video_output, image_output):
|
||||||
self.input = input_folder
|
self.input = input_folder
|
||||||
@ -35,11 +37,16 @@ class Classifier():
|
|||||||
self.video_output = video_output
|
self.video_output = video_output
|
||||||
self.image_output = image_output
|
self.image_output = image_output
|
||||||
self.processed_count = 0
|
self.processed_count = 0
|
||||||
self.table = self.TEST_TABLE if self.mode == 'dev' else self.TABLE
|
self.table = self.TEST_TABLE if self.mode == "dev" else self.TABLE
|
||||||
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="panel.zhiyong.tech",
|
||||||
|
user="yu_biggerfish",
|
||||||
|
password="jRHTbQrdkfNNTztH",
|
||||||
|
database="photo_classifier",
|
||||||
|
)
|
||||||
|
|
||||||
def close_database(self):
|
def close_database(self):
|
||||||
self.db.close()
|
self.db.close()
|
||||||
@ -48,17 +55,19 @@ class Classifier():
|
|||||||
self.connect_database()
|
self.connect_database()
|
||||||
cursor = self.db.cursor()
|
cursor = self.db.cursor()
|
||||||
|
|
||||||
sql = 'DROP TABLE IF EXISTS {}'.format(self.table)
|
sql = "DROP TABLE IF EXISTS {}".format(self.table)
|
||||||
cursor.execute(sql)
|
cursor.execute(sql)
|
||||||
print('删除表 {}'.format(self.table))
|
print("删除表 {}".format(self.table))
|
||||||
|
|
||||||
sql = '''CREATE TABLE {} (
|
sql = """CREATE TABLE {} (
|
||||||
ID INT NOT NULL AUTO_INCREMENT ,
|
ID INT NOT NULL AUTO_INCREMENT ,
|
||||||
MD5 VARCHAR(255) NOT NULL ,
|
MD5 VARCHAR(255) NOT NULL ,
|
||||||
PRIMARY KEY (ID), UNIQUE (MD5))
|
PRIMARY KEY (ID), UNIQUE (MD5))
|
||||||
ENGINE = InnoDB;'''.format(self.table)
|
ENGINE = InnoDB;""".format(
|
||||||
|
self.table
|
||||||
|
)
|
||||||
cursor.execute(sql)
|
cursor.execute(sql)
|
||||||
print('创建表 {}'.format(self.table))
|
print("创建表 {}".format(self.table))
|
||||||
|
|
||||||
self.close_database()
|
self.close_database()
|
||||||
|
|
||||||
@ -70,12 +79,12 @@ class Classifier():
|
|||||||
|
|
||||||
def get_file_count(self, folder):
|
def get_file_count(self, folder):
|
||||||
count = 0
|
count = 0
|
||||||
for (_, _, _files) in os.walk(folder):
|
for _, _, _files in os.walk(folder):
|
||||||
count += len(_files)
|
count += len(_files)
|
||||||
return count
|
return count
|
||||||
|
|
||||||
def delete_folders(self, folder):
|
def delete_folders(self, folder):
|
||||||
for (root, dirs, files) in os.walk(folder):
|
for root, dirs, files in os.walk(folder):
|
||||||
for dir in dirs:
|
for dir in dirs:
|
||||||
if dir in self.SKIP_FOLDERS:
|
if dir in self.SKIP_FOLDERS:
|
||||||
continue
|
continue
|
||||||
@ -83,7 +92,7 @@ class Classifier():
|
|||||||
if os.path.isdir(abs_path):
|
if os.path.isdir(abs_path):
|
||||||
if self.get_file_count(abs_path) == 0:
|
if self.get_file_count(abs_path) == 0:
|
||||||
shutil.rmtree(abs_path)
|
shutil.rmtree(abs_path)
|
||||||
print('删除目录: {}'.format(abs_path))
|
print("删除目录: {}".format(abs_path))
|
||||||
|
|
||||||
def is_photo(self, file_name):
|
def is_photo(self, file_name):
|
||||||
return self.is_image(file_name) and self.contains_exif(file_name)
|
return self.is_image(file_name) and self.contains_exif(file_name)
|
||||||
@ -101,18 +110,18 @@ class Classifier():
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
def contains_exif(self, file_name):
|
def contains_exif(self, file_name):
|
||||||
with open(file_name, 'rb') as reader:
|
with open(file_name, "rb") as reader:
|
||||||
tags = exifread.process_file(reader)
|
tags = exifread.process_file(reader)
|
||||||
keys = [key for key in self.PHOTO_EXIF_KEYS if key in tags]
|
keys = [key for key in self.PHOTO_EXIF_KEYS if key in tags]
|
||||||
return len(keys) > 0
|
return len(keys) > 0
|
||||||
|
|
||||||
def process_folder(self, folder):
|
def process_folder(self, folder):
|
||||||
for (root, dirs, files) in os.walk(folder):
|
for root, dirs, files in os.walk(folder):
|
||||||
for file in files:
|
for file in files:
|
||||||
self.process_file(root, file)
|
self.process_file(root, file)
|
||||||
|
|
||||||
def get_md5(self, file):
|
def get_md5(self, file):
|
||||||
with open(file, 'rb') as reader:
|
with open(file, "rb") as reader:
|
||||||
return hashlib.md5(reader.read()).hexdigest()
|
return hashlib.md5(reader.read()).hexdigest()
|
||||||
|
|
||||||
def process_file(self, root, file):
|
def process_file(self, root, file):
|
||||||
@ -125,11 +134,13 @@ class Classifier():
|
|||||||
new_name = self.rename_move(file_path, year, month, day, md5)
|
new_name = self.rename_move(file_path, year, month, day, md5)
|
||||||
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)
|
||||||
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(str(e))
|
print(str(e))
|
||||||
else:
|
else:
|
||||||
print('非图片或视频, 忽略文件: {}'.format(file_path))
|
print("非图片或视频, 忽略文件: {}".format(file_path))
|
||||||
|
|
||||||
def add_record(self, md5):
|
def add_record(self, md5):
|
||||||
try:
|
try:
|
||||||
@ -138,7 +149,7 @@ class Classifier():
|
|||||||
cursor.execute(sql)
|
cursor.execute(sql)
|
||||||
self.db.commit()
|
self.db.commit()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print('插入记录 {} 到数据库photo_classifier失败: {}'.format(md5, str(e)))
|
print("插入记录 {} 到数据库photo_classifier失败: {}".format(md5, str(e)))
|
||||||
self.db.rollback()
|
self.db.rollback()
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
@ -149,24 +160,24 @@ class Classifier():
|
|||||||
sql = "SELECT MD5 FROM {} WHERE MD5='{}'".format(self.table, md5)
|
sql = "SELECT MD5 FROM {} WHERE MD5='{}'".format(self.table, md5)
|
||||||
cursor.execute(sql)
|
cursor.execute(sql)
|
||||||
record = cursor.fetchone()
|
record = cursor.fetchone()
|
||||||
if str(record) != 'None':
|
if str(record) != "None":
|
||||||
os.remove(file_path)
|
os.remove(file_path)
|
||||||
raise Exception('重复文件 {} --> 删除'.format(file_path))
|
raise Exception("重复文件 {} --> 删除".format(file_path))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
if (not self.is_image(file_path)) and (not self.is_video(file_path)):
|
if (not self.is_image(file_path)) and (not self.is_video(file_path)):
|
||||||
raise Exception('非图片或视频: {} --> 跳过'.format(file_path))
|
raise Exception("非图片或视频: {} --> 跳过".format(file_path))
|
||||||
|
|
||||||
def get_photo_create_date(self, file):
|
def get_photo_create_date(self, file):
|
||||||
with open(file, 'rb') as reader:
|
with open(file, "rb") as reader:
|
||||||
tags = exifread.process_file(reader)
|
tags = exifread.process_file(reader)
|
||||||
keys = [key for key in self.PHOTO_DATE_KEYS if key in tags]
|
keys = [key for key in self.PHOTO_DATE_KEYS if key in tags]
|
||||||
if len(keys) > 0:
|
if len(keys) > 0:
|
||||||
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[:10].split(':')
|
_date = time_str[:10].split(":")
|
||||||
year = _date[0]
|
year = _date[0]
|
||||||
month = _date[1]
|
month = _date[1]
|
||||||
day = _date[2]
|
day = _date[2]
|
||||||
@ -177,8 +188,8 @@ class Classifier():
|
|||||||
try:
|
try:
|
||||||
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[:10].split('-')
|
_date = time_str[:10].split("-")
|
||||||
year = _date[0]
|
year = _date[0]
|
||||||
month = _date[1]
|
month = _date[1]
|
||||||
day = _date[2]
|
day = _date[2]
|
||||||
@ -187,7 +198,7 @@ class Classifier():
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
def read_date(self, file):
|
def read_date(self, file):
|
||||||
file = file.replace('/', '\\')
|
file = file.replace("/", "\\")
|
||||||
date = None
|
date = None
|
||||||
if self.is_photo(file):
|
if self.is_photo(file):
|
||||||
date = self.get_photo_create_date(file) # 照片可能没有EXIF日期
|
date = self.get_photo_create_date(file) # 照片可能没有EXIF日期
|
||||||
@ -197,7 +208,7 @@ 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[:10].split('-')
|
_date = time_str[:10].split("-")
|
||||||
year = _date[0]
|
year = _date[0]
|
||||||
month = _date[1]
|
month = _date[1]
|
||||||
day = _date[2]
|
day = _date[2]
|
||||||
@ -213,23 +224,24 @@ class Classifier():
|
|||||||
elif self.is_video(file_path):
|
elif self.is_video(file_path):
|
||||||
output = self.video_output
|
output = self.video_output
|
||||||
else:
|
else:
|
||||||
raise Exception('移动文件失败, 非图片或视频: {}'.format(file_path))
|
raise Exception("移动文件失败, 非图片或视频: {}".format(file_path))
|
||||||
|
|
||||||
new_path = os.path.join(output, year, month, day)
|
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 + '-' + day + '-' + md5 + file_ext
|
new_name = year + "-" + month + "-" + day + "-" + md5 + 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(
|
cf = Classifier(
|
||||||
input_folder='D:/待分类照片视频',
|
input_folder="D:/待分类照片视频",
|
||||||
# input_folder='z:/待分类照片视频/Picture',
|
# input_folder="D:/总仓库-照片视频-bak",
|
||||||
photo_output='D:/总仓库-照片视频/总照片备份',
|
photo_output="D:/总仓库-照片视频/总照片备份",
|
||||||
video_output='D:/总仓库-照片视频/总视频备份',
|
video_output="D:/总仓库-照片视频/总视频备份",
|
||||||
image_output='D:/总仓库-照片视频/总图片备份')
|
image_output="D:/总仓库-照片视频/总图片备份",
|
||||||
|
)
|
||||||
|
|
||||||
cf.start()
|
cf.start()
|
||||||
# cf.create_table()
|
# cf.create_table()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user