access google photo api with python using google api python client

Solutions on MaxInterview for access google photo api with python using google api python client by the best coders in the world

showing results for - "access google photo api with python using google api python client"
Carla
24 Apr 2018
1from os.path import join, dirname
2from googleapiclient.discovery import build
3from httplib2 import Http
4from oauth2client import file, client, tools
5SCOPES = 'https://www.googleapis.com/auth/photoslibrary.readonly'
6
7store = file.Storage(join(dirname(__file__), 'token-for-google.json'))
8creds = store.get()
9if not creds or creds.invalid:
10    flow = client.flow_from_clientsecrets(join(dirname(__file__), 'client_id.json', SCOPES))
11    creds = tools.run_flow(flow, store)
12google_photos = build('photoslibrary', 'v1', http=creds.authorize(Http()))
13
14day, month, year = ('0', '6', '2019')  # Day or month may be 0 => full month resp. year
15date_filter = [{"day": day, "month": month, "year": year}]  # No leading zeroes for day an month!
16nextpagetoken = 'Dummy'
17while nextpagetoken != '':
18    nextpagetoken = '' if nextpagetoken == 'Dummy' else nextpagetoken
19    results = google_photos.mediaItems().search(
20            body={"filters":  {"dateFilter": {"dates": [{"day": day, "month": month, "year": year}]}},
21                  "pageSize": 10, "pageToken": nextpagetoken}).execute()
22    # The default number of media items to return at a time is 25. The maximum pageSize is 100.
23    items = results.get('mediaItems', [])
24    nextpagetoken = results.get('nextPageToken', '')
25    for item in items:
26            print(f"{item['filename']} {item['mimeType']} '{item.get('description', '- -')}'"
27                      f" {item['mediaMetadata']['creationTime']}\nURL: {item['productUrl']}")
28