-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_analyzer.py
More file actions
444 lines (390 loc) · 18.6 KB
/
log_analyzer.py
File metadata and controls
444 lines (390 loc) · 18.6 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
import httplib2
import os
import pymongo
import sys
import logging
import hashlib
import shutil
import re
import time
import collections
from apiclient import discovery
import oauth2client
from oauth2client import client
from oauth2client import tools
LOCAL_ARCHIVE = r"C:\Users\SJackson\Pictures\Test Archive"
GPHOTO_UPLOAD_QUEUE = r"C:\Users\SJackson\Pictures\GooglePhotosQueue"
IMAGE_FILE_EXTENSIONS = ['jpg']
LOG_FILE = os.path.join(r"C:\Users\SJackson\Documents\Personal\Programming", time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime()) + "photolog.txt")
#LOG_FILE = os.path.join(r"C:\Users\SJackson\Documents\Personal\Programming\photolog.txt")
LOG_FORMAT = "%(asctime)s - %(levelname)s - %(module)s - %(funcName)s - %(message)s"
logging.basicConfig(
filename=LOG_FILE,
format=LOG_FORMAT,
level=logging.DEBUG,
filemode='w'
)
def _safe_print(u, errors="replace"):
"""Safely print the given string.
If you want to see the code points for unprintable characters then you
can use `errors="xmlcharrefreplace"`.
"""
s = u.encode(sys.stdout.encoding or "utf-8", errors)
print(s)
#confirmed_db = pymongo.MongoClient()['gp']['confirmed']
def main():
wait_for_backup(1)
sys.exit(2)
logging.info("Start up")
gphotos = Gphotos('gp', 'gphotos')
# archive = Local_archive(LOCAL_ARCHIVE, 'gp', 'archive')
delete_queue(GPHOTO_UPLOAD_QUEUE)
print("Syncing database")
gphotos.sync()
# archive.sync()
cycle = 0
queue_count = 0
skipped_count = 0
for photo in gphotos.check_tree(r"E:\mnt\Photos\Candidates"):
if len(photo) > 1:
#move_to_local_archive(photo)
skipped_count += 1
if skipped_count%100 == 0:
print("{} skipped".format(skipped_count))
else:
if os.path.splitext(photo['filepath'])[1].lower() in ['.jpg']:
queue_count += 1
print('{}: copy {} to upload'.format(queue_count, photo['filepath']))
try:
shutil.copy2(photo['filepath'], GPHOTO_UPLOAD_QUEUE)
except shutil.SameFileError:
print("Same filename: {}".format(photo['filepath']))
if queue_count >= 500:
cycle += 1
wait_for_backup(cycle)
delete_queue(GPHOTO_UPLOAD_QUEUE)
print("Syncing database")
gphotos.sync()
queue_count = 0
else:
skipped_count += 1
#print('Rejected: {}'.format(photo['filepath']))
logging.info("Done")
print("***Done***")
def delete_queue(path):
print("Deleting all files in Queue")
for filename in os.listdir(path):
try:
os.remove(os.path.join(path, filename))
except os.error as e:
logging.error(e)
# This is here in case Google Photo Backup starts hanging and we need to restart - mostly to capture library name
# def restart_photo_backup():
# import psutil
#
# for proc in psutil.process_iter():
# try:
# pinfo = proc.as_dict(attrs=['pid', 'name'])
# except psutil.NoSuchProcess:
# pass
# else:
# print(pinfo)
def wait_for_backup(cycle): # TODO: Not sure how this works on an empty file or when it runs to top without finding token
TIMEOUT = 60 * 60 #One hour
GOOGLE_BACKUP_LOG = r"C:\Users\SJackson\AppData\Local\Google\Google Photos Backup\network.log"
f = os.open(GOOGLE_BACKUP_LOG, os.O_RDONLY)
os.lseek(f,-2000,os.SEEK_END)
tail = os.read(f, 2100)
# regex = re.compile(".*Upload Begin .(.*.jpg).*remainingMediaCount=([0-9]+).*$")
regex = re.compile('.*Upload Begin .(.*.jpg).*remainingMediaCount=([0-9]+).*')
records = regex.search(str(tail))
print(tail)
sys.exit(3)
print("End of file count: {}".format(end_count))
f.seek(end_count - 2000, os.SEEK_SET)
regex_count = re.compile('.*remainingMediaCount=([0-9]+).*')
regex_filename = re.compile(".*Upload begin '(.*)'")
time_start = time.time()
last_remaining_count = -1
while True:
with open(GOOGLE_BACKUP_LOG) as f:
tail = collections.deque(f)
while True:
target_count = tail.pop()
found = regex_count.search(target_count)
if found:
remaining_count = int(found.group(1))
while True:
target_name = tail.pop() #Pop from an empty queue
file_found = regex_filename.search(target_name)
if file_found:
file_path = file_found.group(1)
break
break
if remaining_count == 0 and last_remaining_count == -1: #Log file hasn't updated yet - wait for remaining_count > 0
print("Remaining count is 0; waiting for update")
else:
if remaining_count != last_remaining_count:
elapsed = time.time() - time_start
print("{} Elapsed: {:d}:{:02d} Cycle {}: {} remaining. Target: {}".format(time.asctime(), int(elapsed//60), int(elapsed%60), cycle, remaining_count, file_path))
last_remaining_count = remaining_count
if remaining_count == 0:
return
if time.time() - time_start > TIMEOUT:
logging.info("Timed out")
return
time.sleep(5)
class Local_archive(object):
def __init__(self, top, database, collection):
self.db = pymongo.MongoClient()[database][collection]
self.db.create_index('md5Checksum')
self.top = top
def sync(self):
for root, dirs, files in os.walk(self.top): #TODO: Add error trapping argument
logging.info('Computing MD5s for directory {}'.format(root))
for path in [os.path.join(root, x) for x in files]:
self.db.insert({'path': path, 'md5Checksum': file_md5sum(path)})
def check_member(self, md5):
return self.db.find({'md5Checksum': md5})
# def move_to_local_archive(archive, photo):
# if 'md5Checksum' in photo:
# if archive.check_member(photo['md5Checksum']):
# safe_move(photo['path'], TEMPORARY_HOLD_DIR)
# else:
# safe_move(photo['path'], place_figured_out_from_path_elements)
#if md5sum already in local archive:
# safe move to 'to be deleted'
# return
#build path from parent record
#safe_move to local archive
#add checksum to database
class Gphotos(object):
"""
Gphotos: A set of tools to aid management of local images and a Google Photos repository
"""
def __init__(self, database, collection):
self.service = None
self.db = pymongo.MongoClient()[database][collection]
self.db.create_index('id')
self.db.create_index('md5Checksum')
def check_member(self, md5):
"""
If md5 is in Google Photos returns associated Gphoto metadata, otherwise returns None
:param md5: MD5 sum of record possibly on Google Photos
:return: dict of matching Google Photo metadata, returns None if not in Google Photos
"""
meta = self.db.find_one({'md5Checksum': md5, 'trashed': False, 'explicitlyTrashed': False})
if meta is not None:
gphoto_path = os.path.join(*(self.db.find_one({'id': meta['parents'][0]})['path']))
meta.update({'gpath': gphoto_path})
return meta
def _walk_error(self, walk_err):
# TODO: Maybe some better error trapping here...
print("Error {}:{}".format(walk_err.errno, walk_err.strerror))
raise
def check_tree(self, top):
"""
Descends filesystem from top and returns a dict containing file path for each file. If a record appears in Google Photos
with the same MD5 sum then also populates dict with record metadata
:param top: Root of tree to descend in filesystem
:return: dict with path of each file, augmented with Google Photo record metadata if record with same MD5 sum appears in Google Photos
"""
logging.info("Traversing filesystem tree starting at {}...".format(top))
if os.path.isfile(top):
yield self.add_filepath_and_lookup(top)
else:
for dirpath, dirnames, filenames in os.walk(top, onerror=self._walk_error):
logging.info('Processing dir {}'.format(dirpath))
for filepath in [os.path.join(dirpath, filename) for filename in filenames]:
yield self.add_filepath_and_lookup(filepath)
logging.info("Done traversing filesystem tree.")
def add_filepath_and_lookup(self, filepath):
member = {'filepath': filepath}
metadata = self.check_member(file_md5sum(filepath))
if metadata is not None:
member.update(self.check_member(file_md5sum(filepath)))
return member
def sync(self):
"""
Synchronize database with google photos
"""
if self.service is None:
self.get_service()
# TODO: Make sure we don't 'find' files that are marked at trashed
database_changed = False
INIT_FIELDS = "files(id,imageMediaMetadata/time,md5Checksum,mimeType,name,originalFilename,ownedByMe,parents,size,spaces,explicitlyTrashed,trashed), nextPageToken"
change_token_cursor = self.db.find({'change_token': {'$exists': True}})
assert change_token_cursor.count() <= 1
if change_token_cursor.count() == 0: # If we have no change token, drop and resync the database
logging.info("No change token available - resyncing database")
self.db.drop()
database_changed = True
next_page_token = None
while True:
file_list = self.service.files().list(pageToken=next_page_token,
spaces='photos',
pageSize=1000,
fields=INIT_FIELDS).execute()
if 'files' in file_list:
file_count = len(file_list['files'])
else:
file_count = 0
logging.info("Google sent {} records".format(file_count))
db_status = self.db.insert_many(file_list.get('files'))
logging.info("Mongodb stored {} records".format(len(db_status.inserted_ids)))
assert file_count == len(
db_status.inserted_ids), "Records stored != records from gPhotos. Got {} gPhotos and {} ids".format(
file_count, len(db_status.inserted_ids))
if 'nextPageToken' in file_list:
next_page_token = file_list['nextPageToken']
else:
break
# Once db is updated with all changes, get initial change token
change_token = self.service.changes().getStartPageToken().execute()
self.db.insert({'change_token': change_token['startPageToken']})
logging.info("Total records: {}".format(self.db.count()))
else:
new_count = 0
delete_count = 0
logging.info('Have change token; updating database.')
change_token = change_token_cursor[0]['change_token']
UPDATE_FIELDS = 'changes(file(id,md5Checksum,mimeType,name,originalFilename,ownedByMe,parents,size,spaces,explicitlyTrashed,trashed),fileId,removed,time),kind,newStartPageToken,nextPageToken'
while True:
changes = self.service.changes().list(pageToken=change_token,
spaces='photos',
pageSize=1000,
includeRemoved=True,
fields=UPDATE_FIELDS).execute()
change_count = len(changes.get('changes', []))
logging.info("Google sent {} records".format(change_count))
if change_count: # TODO: If 'removed' is True then remove file from database: changes['changes'][0]['removed']
database_changed = True
for change in changes['changes']:
if change['removed'] is True:
db_status = self.db.delete_one({'id': change['fileId']})
assert db_status.deleted_count == 1, "Deleted files count should be 1, got {}".format(
db_status.deleted_count)
delete_count += 1
else:
db_status = self.db.replace_one({'id': change['file']['id']}, change['file'],
upsert=True) # TODO: Make sure the data that comes with change is complete for insertion
# assert db_status.modified_count == 1, "Modified files count should be 1, got {}".format(
# db_status.modified_count)
new_count += 1
if 'nextPageToken' in changes:
change_token = changes['nextPageToken']
else:
assert 'newStartPageToken' in changes, "newStartPageToken missing when nextPageToken is missing. Should never happen."
db_status = self.db.replace_one({'change_token': {'$exists': True}},
{'change_token': changes['newStartPageToken']})
assert db_status.modified_count == 1, "Database did not update correctly"
break # All changes have been received
logging.info("Sync update complete. New files: {} Deleted files: {}".format(new_count, delete_count))
logging.info('Done with database resync')
if database_changed:
self.__get_parents()
root_id = self.service.files().list(q='name="Google Photos"').execute()['files'][0]['id']
self.__set_paths(root_id, ['Google Photos'])
logging.info('Done set_paths')
def get_stats():
count = self.db.count()
# self.db.sales.aggregate([
# {
# '$group': {_id: {day: {$dayOfYear: "$date"}, year: { $year: "$date"}},
# totalAmount: { $sum: { $multiply: ["$price", "$quantity"]}},
# count: { $sum: 1}}}
# ])
def __get_parents(self):
"""
Populate database entries for parent folders
:return: None. Changes database
"""
# TODO: This delivers a datbase record with "My Drive" in it. That is too high in the tree.....
if self.service is None:
self.get_service()
parents_needed = set(self.db.distinct('parents')) # Seed not_in_db_set with all parents assuming none are present
ids_in_db = set(self.db.distinct('id'))
parents_needed.difference_update(ids_in_db)
while parents_needed:
parent_id = parents_needed.pop()
parent_meta = self.service.files().get(fileId=parent_id, fields='id,kind,md5Checksum,mimeType,name,ownedByMe,parents,size,trashed').execute()
self.db.insert(parent_meta) #TODO Check write was successful?
ids_in_db.add(parent_id)
for parent in parent_meta.get('parents') or []:
if parent not in ids_in_db:
parents_needed.add(parent)
logging.info('Done getting parents')
def __set_paths(self, id, path):
"""
Sets path ids for folders
:param id: Google Drive id of Google Photos folder
:param path: Google Drive path to file with Google Drive id
:return: None. Adds path to each folder in Google Photos
"""
children = self.db.find({'mimeType': 'application/vnd.google-apps.folder', 'parents': id})
self.db.update_one({'id': id}, {'$set': {'path': path}})
if children.count() != 0:
for child in children:
my_name = self.db.find_one({'id': child['id']})['name']
path.append(my_name)
self.__set_paths(child['id'], path)
path.pop()
def get_service(self):
credentials = self.get_credentials()
http = credentials.authorize(httplib2.Http())
self.service = discovery.build('drive', 'v3', http=http)
def get_credentials(self):
"""Gets valid user credentials from storage.
If nothing has been stored, or if the stored credentials are invalid,
the OAuth2 flow is completed to obtain the new credentials.
Returns:
Credentials, the obtained credential.
"""
SCOPES = 'https://www.googleapis.com/auth/drive.readonly https://www.googleapis.com/auth/drive.photos.readonly'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Other Client 1'
try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir,
'drive-batch.json')
store = oauth2client.file.Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Needed only for compatibility with Python 2.6
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials
#db.getCollection('gp_batch').aggregate({$group: {_id: "$md5Checksum", count:{$sum: 1}}},{$match: {count: {$gt: 1}}} ) #Example that find md5 duplicates
def file_md5sum(path):
BUF_SIZE = 65536 # lets read stuff in 64kb chunks!
md5 = hashlib.md5()
with open(path, 'rb') as f:
while True:
data = f.read(BUF_SIZE)
if not data:
break
md5.update(data)
return md5.hexdigest()
# TODO: Consider this error handling should files fail to open:
# try:
# file = open(...)
# except OpenErrors...:
# # handle open exceptions
# else:
# try:
# # do stuff with file
# finally:
# file.close()
if __name__ == '__main__':
main()