1

is there a way to iterate over all items in some album, in google photos? I'm using the google photos provided API, and from what im aware of, I can only get access to 100 media items at a time, using the following:

def create_service(client_secret_file, api_name, api_version, scopes):
    print(client_secret_file, api_name, api_version, scopes, sep=', ')
    SCOPES = [scope for scope in scopes[0]]
    cred = None
    pickle_file = f'token_{api_name}_{api_version}.pickle'
    if os.path.exists(pickle_file):
        with open(pickle_file, 'rb') as token:
            cred = pickle.load(token)

    if not cred or not cred.valid:
        if cred and cred.expired and cred.refresh_token:
            cred.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                client_secret_file, SCOPES)
            cred = flow.run_local_server()

        with open(pickle_file, 'wb') as token:
            pickle.dump(cred, token)

    service = build(api_name, api_version, credentials=cred)
    print(api_name, 'service created successfully')
    return service




service = create_service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES)
media = service.mediaItems().list(pageSize=MEDIA_TO_SHOW).execute()

where all the upper case are some non relevant string and int constants, that indicate the api's name, the scopes accessible, etc...(for readability). specifically, MEDIA_TO_SHOW = 100

thanks for the help

Hadar
  • 658
  • 4
  • 17

1 Answers1

1

At the method of "mediaItems.list" in Google Photos API, the maximum value of pageSize is 100. When you want to retrieve more items, it is required to use pageToken. At the official document of pageToken, it says as follows.

pageToken: A continuation token to get the next page of the results. Adding this to the request returns the rows after the pageToken. The pageToken should be the value returned in the nextPageToken parameter in the response to the listMediaItems request.

When this is reflected to your script, it becomes as follows.

Modified script:

service = create_service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES)

# I modified below script.
MEDIA_TO_SHOW = 100
mediaItemList = []
pageToken = ""
while True:
    res = service.mediaItems().list(pageSize=MEDIA_TO_SHOW, pageToken=pageToken if pageToken != "" else "").execute()
    mediaItems = res.get('mediaItems', [])
    mediaItemList.extend(mediaItems)
    pageToken = res.get('nextPageToken')
    if not pageToken:
        break

print(len(mediaItemList))  # You can see the number of items here.
  • In this script, it supposes that your service can be used for the method of service.mediaItems().list().

Reference:

Added:

If you want to retrieve the item list from the specific album, please use the following script.

Modified script:

service = create_service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES)

# I modified below script.
album_id = "###"  # Please set the album ID.
MEDIA_TO_SHOW = 100
mediaItemList = []
pageToken = ""
while True:
    body = {
        "albumId": album_id,
        "pageToken": pageToken if pageToken != "" else "",
        "pageSize": MEDIA_TO_SHOW
    }
    res = service.mediaItems().search(body=body).execute()
    mediaItems = res.get('mediaItems', [])
    mediaItemList.extend(mediaItems)
    pageToken = res.get('nextPageToken')
    if not pageToken:
        break

print(len(mediaItemList))  # You can see the number of items here.

Reference:

-Method: mediaItems.search

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • thank you. this, as I understand it, iterates over all media items in all albums, correct? is there a way to do the exact thing, but over some specific album? – Hadar Sep 28 '20 at 13:51
  • 1
    @Hadar Sharvit Thank you for replying. Yes. In my 1st proposed modified script, the item list is retrieved from all album. If you want to retrieve the item list from the specific album, please check the added sample script in my answer. If that was not the direction you expect, I apologize. – Tanaike Sep 28 '20 at 22:33