-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyPhotos.py
More file actions
221 lines (186 loc) · 6.96 KB
/
Copy pathpyPhotos.py
File metadata and controls
221 lines (186 loc) · 6.96 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
from gphotospy import authorize
from gphotospy.album import Album
from gphotospy.media import Media,MediaItem
import os
import numpy as np
import pandas as pd
import requests
import json
import time
from cv2 import cv2
from PIL import Image
import email, smtplib, ssl
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
conf = {}
def get_conf():
for i, row in pd.read_csv("conf",header=None,names=["key","value"]).iterrows():
conf[row["key"]] = row["value"]
#https://dev.to/davidedelpapa/manage-your-google-photo-account-with-python-p-1-9m2
def create_message(conf,
subject,
body):
# Create a multipart message and set headers
message = MIMEMultipart()
message["From"] = conf["sender"]
message["To"] = conf["receiver"]
message["Subject"] = subject
message["Bcc"] = conf["receiver"] # Recommended for mass emails
# Add body to email
message.attach(MIMEText(body, "plain"))
return message
def get_attachment(filepng,filename):
# Open PDF file in binary mode
with open(filepng, "rb") as attachment:
# Add file as application/octet-stream
# Email client can usually download this automatically as attachment
part = MIMEBase("application", "octet-stream")
part.set_payload(attachment.read())
# Encode file in ASCII characters to send by email
encoders.encode_base64(part)
# Add header as key/value pair to attachment part
part.add_header(
"Content-Disposition",
f"attachment; filename={filename}",
)
return part
def send_mail(conf, text):
# Log in to server using secure context and send email
context = ssl.create_default_context()
with smtplib.SMTP("smtp.bo.infn.it", 587) as server:
server.ehlo() # Can be omitted
server.starttls(context=context)
server.ehlo() # Can be omitted
server.login(conf["user"], conf["password"])
server.sendmail(conf["sender"], conf["receiver"], text)
def checkImage(filename):
if not os.path.isfile(filename):
return False
else:
try:
img = Image.open(filename) # open the image file
img.verify() # verify that it is, in fact an image
return True
except:
return False
def checkVideo(filename):
if not os.path.isfile(filename):
return False
else:
try:
vid = cv2.VideoCapture(filename)
if not vid.isOpened():
return False
else:
return True
except:
return False
CLIENT_SECRET_FILE = "gphoto_oauth.json"
directory = "./Google Photos"
fname = time.strftime("%Y%m%d-%H%M%S") + ".log"
flog = "./log/" + fname
try:
service = authorize.init(CLIENT_SECRET_FILE)
except:
get_conf()
message = create_message(conf,
"pyPhotos: Token has been expired or revoked",
"pyPhotos: Token has been expired or revoked\n" \
"1- Remove 'photoslibrary_v1.token'\n"\
"2- Run 'pyPhotos.py'\n")
send_mail(conf, message.as_string())
exit(1)
album_manager = Album(service)
media_manager = Media(service)
album_iterator = album_manager.list()
media_iterator = media_manager.list()
output_dir_root = "./Google Photos/"
f = open(flog,"w")
all_media = {}
try:
for m in media_iterator:
if m["id"] not in all_media:
all_media[m["id"]] = {}
all_media[m["id"]][m["mediaMetadata"]["creationTime"]] = m["filename"]
media = MediaItem(m)
year = m["mediaMetadata"]["creationTime"][:4]
output_dir = output_dir_root + "Photos from " + year + "/"
if not os.path.isdir(output_dir):
os.mkdir(output_dir)
output_path = output_dir + m["filename"]
f.write(f"'{output_path}',{os.path.isfile(output_path)}")
if not os.path.isfile(output_path):
with open(output_path, 'wb') as output:
output.write(media.raw_download())
ftype=m["mimeType"].split('/')[0]
if ftype == "image":
status=checkImage(output_path)
f.write(f",{status}\n")
elif ftype == "video":
status=checkVideo(output_path)
f.write(f",{status}\n")
# metadata
output_json_path = output_path + ".json"
f.write(f"'{output_json_path}',{os.path.isfile(output_json_path)}")
if not os.path.isfile(output_json_path):
with open(output_json_path, 'w') as output:
json.dump(media.metadata(),output,indent=4)
f.write(f",{os.path.isfile(output_json_path)}\n")
except:
pass
try:
for a in album_iterator:
album_id = a.get("id")
items = media_manager.search_album(album_id)
try:
for m in items:
media = MediaItem(m)
output_dir = output_dir_root + a["title"] + "/"
if not os.path.isdir(output_dir):
os.mkdir(output_dir)
if m["id"] not in all_media:
continue
if m["mediaMetadata"]["creationTime"] not in all_media[m["id"]]:
continue
filename = all_media[m["id"]][m["mediaMetadata"]["creationTime"]]
output_path = output_dir + filename
f.write(f"'{output_path}',{os.path.isfile(output_path)}")
if not os.path.isfile(output_path):
f.write(f"saving {output_path}\n")
with open(output_path, 'wb') as output:
output.write(media.raw_download())
ftype=m["mimeType"].split('/')[0]
if ftype == "image":
status=checkImage(output_path)
f.write(f",{status}\n")
elif ftype == "video":
status=checkVideo(output_path)
f.write(f",{status}\n")
# metadata
output_json_path = output_path + ".json"
f.write(f"'{output_json_path}',{os.path.isfile(output_json_path)}")
if not os.path.isfile(output_json_path):
f.write(f"saving {output_json_path}\n")
with open(output_json_path, 'w') as output:
json.dump(media.metadata(),output,indent=4)
f.write(f",{os.path.isfile(output_json_path)}\n")
except:
pass
except:
pass
f.close()
get_conf()
message = create_message(conf,
"pyPhotos: Report",
"Attached the report of the last sync.")
message.attach(get_attachment(flog,fname))
send_mail(conf, message.as_string())
df = pd.read_csv(flog,header=None)
df.columns = ["file","presence_before","integrity"]
for index, row in df[df["integrity"] == False].iterrows():
try:
os.remove(row['file'])
except:
pass