]> git.treefish.org Git - photosort.git/blob - src/misc.py
4cbbf74e9f2787af3a40d09cd41593bfe8900080
[photosort.git] / src / misc.py
1 import mimetypes
2 import os
3 import shutil
4
5 def walk_media_files(dir_path):
6     for root, dirs, files in os.walk(dir_path):
7         for f in files:
8             file_path = os.path.join(root, f)
9             if _is_media_file(file_path):
10                 yield (f, file_path)
11
12 def extract_timestamp(file_path):
13     return os.path.getmtime(file_path)
14
15 def find_file(dir_path, file_name, file_size):
16     for root, dirs, files in os.walk(dir_path):
17         for f in files:
18             if f == file_name:
19                 full_path = os.path.join(root, f)
20                 if os.path.getsize(full_path) == file_size:
21                     return root
22     return None
23
24 def import_file(src_file_path, dst_file_path, move=False):
25     shutil.copyfile(src_file_path, dst_file_path)
26     shutil.copystat(src_file_path, dst_file_path)
27
28 def delete_dir_contents(dir_path):
29     for file_name in os.listdir(dir_path):
30         file_path = os.path.join(dir_path, file_name)
31         if os.path.isfile(file_path) or os.path.islink(file_path):
32             os.unlink(file_path)
33         elif os.path.isdir(file_path):
34             shutil.rmtree(file_path)
35
36 def _is_media_file(file_path):
37     if not os.path.isfile(file_path):
38         return False
39     mime_type = mimetypes.guess_type(file_path)[0]
40     if not mime_type:
41         return False
42     if not mime_type.split('/')[0] in ['image', 'video']:
43         return False
44     return True