|
| 1 | +from __future__ import print_function |
| 2 | +import datetime |
| 3 | +from googleapiclient.discovery import build |
| 4 | +from httplib2 import Http |
| 5 | +from oauth2client import file, client, tools |
| 6 | + |
| 7 | +# If modifying these scopes, delete the file token.json. |
| 8 | +SCOPES = 'https://www.googleapis.com/auth/drive.activity.readonly' |
| 9 | + |
| 10 | + |
| 11 | +def main(): |
| 12 | + """Shows basic usage of the Drive Activity API. |
| 13 | + Prints information about the last 10 events that occured the user's Drive. |
| 14 | + """ |
| 15 | + # The file token.json stores the user's access and refresh tokens, and is |
| 16 | + # created automatically when the authorization flow completes for the first |
| 17 | + # time. |
| 18 | + store = file.Storage('token.json') |
| 19 | + creds = store.get() |
| 20 | + if not creds or creds.invalid: |
| 21 | + flow = client.flow_from_clientsecrets('credentials.json', SCOPES) |
| 22 | + creds = tools.run_flow(flow, store) |
| 23 | + service = build('driveactivity', 'v2', http=creds.authorize(Http())) |
| 24 | + |
| 25 | + # Call the Drive Activity API |
| 26 | + results = service.activity().query(body={ |
| 27 | + 'pageSize': 10, |
| 28 | + 'consolidationStrategy': { |
| 29 | + 'legacy': {} |
| 30 | + } |
| 31 | + }).execute() |
| 32 | + activities = results.get('activities', []) |
| 33 | + |
| 34 | + if not activities: |
| 35 | + print('No activity.') |
| 36 | + else: |
| 37 | + print('Recent activity:') |
| 38 | + for activity in activities: |
| 39 | + time = getTimeInfo(activity) |
| 40 | + action = getActionInfo(activity['primaryActionDetail']) |
| 41 | + actors = map(getActorInfo, activity['actors']) |
| 42 | + targets = map(getTargetInfo, activity['targets']) |
| 43 | + print(u'{0}: {1}, {2}, {3}'.format(time, truncated(actors), action, |
| 44 | + truncated(targets))) |
| 45 | + |
| 46 | + |
| 47 | +def truncated(array, limit=2): |
| 48 | + contents = ', '.join(array[:limit]) |
| 49 | + more = '' if len(array) <= limit else ', ...' |
| 50 | + return '[' + contents + more + ']' |
| 51 | + |
| 52 | + |
| 53 | +def getTimeInfo(activity): |
| 54 | + if 'timestamp' in activity: |
| 55 | + return activity['timestamp'] |
| 56 | + if 'timeRange' in activity: |
| 57 | + return activity['timeRange']['endTime'] |
| 58 | + return 'unknown' |
| 59 | + |
| 60 | + |
| 61 | +def getActionInfo(actionDetail): |
| 62 | + return next(iter(actionDetail)) |
| 63 | + |
| 64 | + |
| 65 | +def getUserInfo(user): |
| 66 | + if 'knownUser' in user: |
| 67 | + knownUser = user['knownUser'] |
| 68 | + isMe = knownUser.get('isCurrentUser', False) |
| 69 | + return u'people/me' if isMe else knownUser['personName'] |
| 70 | + return next(iter(user)) |
| 71 | + |
| 72 | + |
| 73 | +def getActorInfo(actor): |
| 74 | + if 'user' in actor: |
| 75 | + return getUserInfo(actor['user']) |
| 76 | + return next(iter(actor)) |
| 77 | + |
| 78 | + |
| 79 | +def getTargetInfo(target): |
| 80 | + if 'driveItem' in target: |
| 81 | + return 'driveItem:"' + target['driveItem'].get('title', 'unknown') + '"' |
| 82 | + if 'teamDrive' in target: |
| 83 | + return 'teamDrive:"' + target['teamDrive'].get('title', 'unknown') + '"' |
| 84 | + if 'fileComment' in target: |
| 85 | + parent = target['fileComment'].get('parent', {}) |
| 86 | + return 'fileComment:"' + parent.get('title', 'unknown') + '"' |
| 87 | + return next(iter(target)) |
| 88 | + |
| 89 | + |
| 90 | +if __name__ == '__main__': |
| 91 | + main() |
0 commit comments