Getting Started With Google Photos API In Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey how's going guys all right so in this video we're going to learn how to use Google photos API in Python if you're a photographer and uses Google photos to back out your media files like photos and videos then you will definitely find many use cases using Google photos API now some of the use cases using Google photos API such as you can automate your photos back up from various sources or you can perform Mass update like renaming the files renaming the albums and so and for me personally I tend to use Google photos API to upload my photos from ative sources like one drive Google Drive and Etc and one of the recent project that I have done for one of my clients was that we use facial recognition API to tag different individuals in around 10,000 photos now if we look at the agenda here I want to go over the quod liit so already cover the use cases with the Google photos API you get 10,000 requests per project per day that means if you have five projects then you can make 50,000 requests uh in your day and that's going to be for the library API now if I uploading photos or videos then that's going to be another quarter in that case that's going to be 75,000 request is per project per day now let's diving into Google Cloud conso so we're going to enable the photos API and I should move this to here so navigate to console. cloud.google.com and I use a create account as free then you want to create a new project so you can click on the drop down and click on new project and follow the instructions to create a new project to enable the photo API click on navigation menu then want to go to apis and services then click on Library here we're going to search for Google photos and should be the first one AOS Library API now clicking into the API and make sure that the API is enabled for your project now we need to go back to the conso now we need to create all of two account so go back to apis and services they want to click on credentials on the top click on create credentials and choose all of client ID all right so here we need to choose the application type now for local development we can use desktop app and I'm name the client as pho photos API Cent and create now we need to download the all of two client file so click on download Json and now name the file let's do service account now just in case if you lost the file somehow simply go back to the account so simply click into the account and click on ADD Secrets oh I actually don't need to do that so here you can redownload the Json file now at this point we are done with downloading the client file as well as enable the API now we need to install the Google python packages all right so go in the luno terminal and there are two libraries that we need to install one is the Google off all off library and the other one is going to be Google API python client and enter and once install the packages we can move on to the script development now for this lesson I'm going to show you how to do three things I'll show you how to list all the albums in your account how to upload photos to an album and let me add one more how to delete photos in an album and I think these four examples are going to cover 80 to 90% of the use cases all right so go and uh create a new python script and let's name the file demo one Pi I created a module called Google apis and let's look at the module first inside the module I have two functions one function is this a create service function so this is the function that I use to create different Google apis client instances like Google Sheets slide YouTube Google Drive and so on and the other function is used to convert RF see uh time stem to daytime object now if you want to connect to multiple accounts you can assign a prefix to the client instance so when you create a client instance or service instance you can assign prefix to identify the account now let's dive it into example one we're going to list all the albums in account all right so we're going to import the create service function then we need to define the location and let's code this variable client secret file and it's going to be the file path to the service account file and actually let me name this to client account client account I think that'll be more appropriate because service account is for uh something else then we need to define the API name API version and the Scopes that I want to connect to the API in this case we're going to connect to the photos Library API followed by the version and once we Define the API name API version and the Scopes we can create the photos Library service instance now let me go ahead to run this code block to create the service op now because this is my first time authenticating my account I'm going to choose my account on this page you want to click on events then click on this link to proceed now here want to go permissions to the app and the permission sets here uh deter by the Scopes that you specify in the script then want to click on continue and once the app is authenticated it's going to create a folder to store the token file under this uh token files folder and the reason why we have this token file is because next time when you run the app you don't need to reauthenticate uh your account again the app is going to use this file to load the assess token now if you need to change to a different account simply delete this file and it's going to pump you to reauthenticate in that case you can choose a different account or you can provide a prefix when you create the service object to list all the albums in your account under the photo library object instance and let's go to the documentation first on the reference want to go to albums and I'll link the link in the description below and let's go into list method so under the list method we have three parameters page size and we can set how many items we can fetch per API request now if fact there are additional photos or not photos albums that we need to retrieve then we need to provide page tokens from the request response then we have this optional print to exclude Now app created data all right so I'm actually going to yeah that's fine so I'm going to set the page size to 10 so that'll f 10 items at a time I'll do the same thing here as well the reason why I want to do that is because I want to show you how to erray all the batches to get all the albums so if I go in the run this code block actually let me run the first code block now because I have more than 10 albums I think I might have more than 10 albums in that case from the response option there will be a next page token available then I can retrieve the page token oh in this case I'm I may have less than 10 albums but assuming that I have more than 10 albums I'll basically run the same method again except that I will provide the page token to the page token parameter then append the albums to the albums list and repeat the process until the next page token is not presented then I can go ahead print the album's metad data like the album ID album title and how many media items is in the album so if I go ahead and run this Loop and these are all my albums in my account now let's move on to example two now we're going to create a new album so here I'm going to take this code block right here when you're connecting to Google photos API the service app creation is always going to be the same to create album we need to define the album's title and let's go to the documentation on Create and if we look at the albums appj field and these are the properties that you can can Define now I think everything else uh here everything else is going to be re only except cover photo media Item ID so this property is used to set the cover photo for the album and for now we just going to give the album a name and to create album we're going to reference the album's resource that I create in from the body parameter we're going to provide this request body Edition object that secq now I can run the Cod block to create the album now if I go back to my account if I refresh the page and on the albums and right here is my albums album now if I click into the album if we look at the UR address so I just want to point out that this is not the album's ID the album's ID is going to be the ID created by the API so if I print the response out this is the album's ID so if we compare the album's ID from the URL it knows that these two IDs are completely different all right so so here I'm going to mainly assign the ID to a variable now want to upload couple photos to the album now let me copy over the media files all right so if I go into my resource folder I have photos and I also have a couple videos now want to write a procedure to upload all the media files in this folder here now uploading photos actually not photos uploading media files is a little bit tricky with Google photos API unfortunately we cannot use the photos client library to upload media files instead we need to U make request codes using R API basically all right so here I need to install the http X Library you can also use request this library to upload media files so first we need to define the folder where the media files are located and it's going to be in the resource folder on my PC they need to load the authentication token and the assess token is located inside the token file so if we open the file so this is the file that is created when we authenticate our account and our assess token is going to get generated and we need to pass this token to be able to upload media files and that's what I meant by it can be a little bit tricky to upload media files then we're going to get ready each file in the resource folder so if we look at this uh loop right here so first we need to specify the headers inside the headers option we need to provide the assess token then we need to specify the content type the X Google upload file name property is going to be used to define the file name then we need to set the upload protocol to ra then we need to load the image as binary then we can make a post request to the photos library. googleapis.com to this uploads endpoint then we provide the headers and the image is binary now this going to be the first step so when you make a post request to this endpoint here it's going to return a token and we need to pass that token to to another API code and we need to pass that token to the album's uh endpoint so here we need to define the media items uh property and this going to be the description uh sure let's go back so description is going to be so description is going to be the description in the info page so this will be your description and to upload the image we need to supply the image token to the simple media item key then we can create the request body and will'll provide the album's ID we want to upload the media file then we can specify the media item to the New Media items key so here we can actually insert multiple media items and if we go to the documentation and the max item is 50 items per batch but for now I'm going to uh just upload them individually then you can use the media items resource. bat create method then set the body where request body's dis option the asq now if I run the loop and oh let me go ahead and uh create my asset token and I need to import a Json Library okay now let me try again so I'm going to create these two items then I'm going to erray each media item in my resource for and I haven't defined my album's ID right let's try again now it's uploading my media files and once all the media files are uploaded and I should have uh used 50 items per batch that's okay now if I go back to my my album's album and I can refr the page and I should see all the photos and videos got uploaded to my my album's album now let's delete all the media files in the album and I'll grab this code block right here and now delete these lines now to delete media files first we need to retrieve all the media items ID and we can do that using media items. search method now there are other features that you can use to perform the search should should be on the media items if we go into filters op these are all the filter types that you can use you can search the media items by day range by content media type such as uh video files or image files you can even use the features uh filter that basically includes whether if you favor the photos or not so one of the other and don't forget that you can use a combinations of features to per for the search but for now I just want to retrieve all the media files in the album so go and uh run this Loop right here to retrieve all the media items ID if I print media items I'm going to get a list of dictionaries and found here I can uh here the files in batches this time I'm going to group the items in batches so 50 items per batch then can erate each batch then just need to supply the media Item ID and using albums that batch remove media item method we just need to specify the album's ID and the request body now I'm going to go and the delete all my photos and I'll go back refresh the page and my album is not empty so unfortunately you cannot delete album using the API if you want to delete albums so an album you need to mainly delete the albums one by one so this concludes this tutorial and hopefully you guys find this video useful and don't forget to like the video and subscribe and I'll see you guys in the next video
Info
Channel: Jie Jenn
Views: 919
Rating: undefined out of 5
Keywords: google photos api, python
Id: vbHp_FiJXqA
Channel Id: undefined
Length: 22min 2sec (1322 seconds)
Published: Sun Mar 31 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.