0

i am new to python api and i am trying to download files from google drive on incremental bases. I am able to download file but not on incremental basis. I have used the steps provided in youtube. Could anyone help me in automating file download from google drive. Thank in advance.

imicron3
  • 7
  • 3

1 Answers1

0

You want to automate your python script to download new/modified files?

If so, then you can utilize Window's Task Scheduler for Windows or cron for Linux to run your python script on a daily basis.

Next is we need to define new/modified files. How do we identify if a file is new or recently modified? Since we already have automated our script to run on a daily basis, we need to check if there are new or recently modified files in the drive. This is where parameter q comes in.

Use query to search for files which were created or modified 1 day before your daily triggered time.

Then integrate your query to search for specific files and download them.

List down files matching the query:

# Call the Drive v3 API
results = service.files().list(
    pageSize=10,
    fields="nextPageToken, files(id, name)",
    q="modifiedTime > '2021-07-27T12:00:00-08:00'"
    ).execute()
items = results.get('files', [])
if not items:
    print('No files found.')
else:
    print('Files:')
    for item in items:
        print('{0} ({1})'.format(item['name'], item['id']))

Note:

  • Sample code above needs to be modified for generating the time based on the daily triggered time and downloading the files. Above code is just a sample for showing how query looks.

References:

NightEye
  • 10,634
  • 2
  • 5
  • 24
  • Hi Thanks for the solution but it is only working for modified file but not the one which i upload recently(the file which i uploaded was created long back but i uploaded recently into the drive). could you please help in this issue? thanks in advance @NaziA – imicron3 Jul 29 '21 at 15:34
  • hi @imicron3, add `createdDate` in your query. Sample query will be: `q="modifiedTime > generatedDate or createdDate > generatedDate"`. This will include both created and modified files beyond `generatedDate`. Also, if we answered your question, please click the accept button on the left (check icon). By doing so, other people in the community, who may have the same concern as you, will know that theirs can be resolved. If the accept button is unavailable to you, feel free to tell me. [how to accept answer](https://stackoverflow.com/help/accepted-answer) – NightEye Jul 29 '21 at 15:42
  • 1
    It worked for createdTime. Thank you so much for the help @NaziA – imicron3 Jul 29 '21 at 16:46