-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcleaner.py
122 lines (106 loc) · 4.47 KB
/
cleaner.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
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
#!/usr/bin/python
import httplib2
from pprint import pprint
import apiclient.discovery
from apiclient import errors
from oauth2client.client import OAuth2WebServerFlow
from config import *
class DriveCleaner():
def __init__(self, drive_service):
self.drive = drive_service
self.requests = 0
self.total = 0
self.moved = 0
self.trashed = 0
self.errors = 0
self.files = []
def __str__(self):
return 'This session, ' + str(self.moved) + ' moved, ' + str(self.trashed) + ' trashed, ' + str(
self.errors) + ' errors, with ' + str(self.total) + ' files currently listed'
def updateFiles(self):
keep_going = True
next_token = None
self.files = []
self.requests = 0
self.total = 0
while keep_going:
self.requests += 1
print 'Making request ' + str(self.requests)
f = self.drive.files().list(maxResults=1000, pageToken=next_token).execute()
try:
next_token = f['nextPageToken']
except KeyError:
next_token = None
keep_going = False
self.total += len(f['items'])
self.files.extend(f['items'])
print str(self)
def noParents(self):
return [x for x in self.files if len(x['parents']) == 0];
def noParentsMine(self):
return [x for x in self.noParents() if
len(x['owners']) == 1 and x['owners'][0]['isAuthenticatedUser']]
def trashItems(self, reload_files=False):
if len(self.files) == 0 or reload_files:
self.updateFiles()
# find items with no parents owned by me
for item in self.noParents:
# ... check to see if already trashed
if item['labels'] and item['labels']['trashed']:
# print '\tAlready in trash: ' + item['title'] + ' - ' + item['id']
pass
else:
print '\tTrashing: ' + item['title'] + ' - ' + item['id']
try:
self.drive.files().trash(fileId=item['id']).execute()
self.trashed += 1
except:
print '\t\t>>> ERROR TRASHING THIS FILE <<<'
self.errors += 1
self.updateFiles()
print str(self)
def moveItems(self, folder, reload_files=False):
"""
MoveItems will move all orphaned files into a particular folder. Unlike trashItems,
this function does not care whether the files are shared so long as they are owned
by the current user. Restricting the search to the current user avoids moving files
owned by others that are "orphaned" because they are still in the "Shared with me"
or "Incoming" folder).
Arguments:
folder (string): the id of the target folder
reload_files (bool): optional, updateFiles before running the move
"""
if len(self.files) == 0 or reload_files:
self.updateFiles()
# find items with no parents owned by me
for item in self.noParentsMine():
# ... check to see if already trashed
if item['labels'] and item['labels']['trashed']:
# print '\tAlready in trash: ' + item['title'] + ' - ' + item['id']
pass
else:
print '\tMoving: ' + item['title'] + ' - ' + item['id']
try:
self.drive.parents().insert(fileId=item['id'], body={'id': folder}).execute()
self.moved += 1
except errors.HttpError, error:
print '\t\t>>> ERROR MOVING THIS FILE <<<'
print '%s' % error
self.errors += 1
self.updateFiles()
print str(self)
def build_connection():
# Run through the OAuth flow and retrieve credentials
flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE, REDIRECT_URI)
authorize_url = flow.step1_get_authorize_url()
print 'Go to the following link in your browser: ' + authorize_url
code = raw_input('Enter verification code: ').strip()
credentials = flow.step2_exchange(code)
# Create an httplib2.Http object and authorize it with our credentials
http = httplib2.Http()
http = credentials.authorize(http)
drive_service = apiclient.discovery.build('drive', 'v2', http=http)
# Create drivecleaner and run it
dc = DriveCleaner(drive_service)
dc.updateFiles()
return dc