-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort-photos.py
executable file
·78 lines (69 loc) · 2.16 KB
/
sort-photos.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/python3
import datetime
import os
import shutil
import filecmp
import re
from sys import argv
import tempfile
import configparser
import requests
# Crontab config:
# 30 0 * * * /home/al/bin/sort-photos.py /nas/photo/sync/lani/camera /nas/photo/sync/lani/phone
def isVideo(f):
m = re.match(".*\.(mp4|avi|mkv)$", f.lower())
return m is not None
config = configparser.ConfigParser()
config.read(os.path.expanduser('~/.pushover'))
setup = config["setup"]
user = setup['user']
token = setup['cron-token']
def pushover(message):
requests.post('https://api.pushover.net/1/messages.json',
data={
'user': user,
'token': token,
'sound': 'none',
'message': message
})
for root in argv[1:]:
files = os.listdir(root)
count = 0
for f in files:
if f == "Thumbs.db" or f == "breezebrowser.dat":
continue
src = os.path.join(root, f)
if os.path.isdir(src):
continue
m = re.match("^(IMG_|VID_|PANO_)?(?P<year>\d\d\d\d)[-]?(?P<month>\d\d)[-]?(?P<day>\d\d).*", f)
year = None
if m:
year = m.group("year")
month = m.group("month")
day = m.group("day")
if year is None or int(year) < 2000 or int(year) > 2050:
created = int(os.stat(src).st_ctime)
year = datetime.datetime.fromtimestamp(created).strftime("%Y")
month = datetime.datetime.fromtimestamp(created).strftime("%m")
day = datetime.datetime.fromtimestamp(created).strftime("%d")
if isVideo(f):
dst = os.path.join(root, "video", year, month)
else:
dst = os.path.join(root, year, month, day)
dstFile = os.path.join(dst, f)
if os.path.exists(dstFile):
if filecmp.cmp(src, dstFile, shallow=0):
# Same file, delete file
os.remove(src)
continue
else:
# Different file with same name, copy under a new name
name, ext = os.path.splitext(f)
dstFile = tempfile.mktemp(ext, name, dst)
if not os.path.exists(dst):
os.makedirs(dst)
shutil.move(src, dstFile)
count = count + 1
message = "Sorted %d files in %s" % (count, root)
print(message)
pushover(message)