Reputation: 99
Hello everyone I have multiple files in my google drive that I need to get the file id for to share download links to people. But I cant seem to get but 1 file id at a time. Is there a way to pull all the file ids and match them with a list?
In my list A,B C
each letter represents an order number I was trying to keep it simple but A would actually be an order number 3472834, B= 3293881, C =,3498249.
In google drive example: File1,File2,File3
The files that are in my google drive are pdf printouts of the orders. They are actually named the order numbers File 1= 3472834, File 2= 3293881, File 3 =3498249
So my goal is to iterate over the ListIDS each one and get the corresponding Google file id. But I cant seem to get it to do but the first google file id each time.
Code:
ListIDS= ['A','B','C']
for eachId in ListIDS:
service = create_service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES)
filename= f'{ListID}'
query = "name contains " + "'"+ filename +"' and trashed = false"
response = service.files().list(q=query, fields= 'files(id,name)').execute()
files = response.get('files')
if files:
print("File Found!")
google_file_name= files[0].get('name')
google_file_id = files[0].get('id')
print (google_file_id)
print(google_file_name)
else:
print("FileNot Found")
FileURL= "https://drive.google.com/file/d/"+ google_file_id + "/view?usp=sharing"
But again the problem is I only get the 2nd value B for all 3 lists id's printed out for example
output:
https://drive.google.com/file/d/FileB_ID_Path/view?usp=sharing
But my expected outcome would be:
https://drive.google.com/file/d/FileA_ID_Path/view?usp=sharing
https://drive.google.com/file/d/FileB_ID_Path/view?usp=sharing
https://drive.google.com/file/d/FileC_ID_Path/view?usp=sharing
Any help please.
Upvotes: 1
Views: 1832
Reputation: 201378
I believe your goal is as follows.
ListIDS= ['A','B','C']
. The files of those filenames are put in your Google Drive.In this case, how about the following modification?
In this pattern, using ListIDS= ['A','B','C']
, one search query is used.
ListIDS = ['A','B','C'] # filenames
service = create_service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES)
qFiles = ["name contains " + "'" + e + "'" for e in ListIDS]
query = "(" + " or ".join(qFiles) + ") and trashed = false"
response = service.files().list(q=query, fields='files(id,name)').execute()
files = response.get('files')
values = {}
for e in files:
for f in ListIDS:
if f in e.get('name'):
id = e.get('id')
v = "https://drive.google.com/file/d/" + id + "/view?usp=sharing"
if values.get(f):
values[f].append(v)
else:
values[f] = [v]
print(values)
When this script is run, the following JSON object is returned.
{
'A': ["https://drive.google.com/file/d/###/view?usp=sharing"],
'B': ["https://drive.google.com/file/d/###/view?usp=sharing"],
'C': ["https://drive.google.com/file/d/###/view?usp=sharing", "https://drive.google.com/file/d/###/view?usp=sharing"]
}
From your showing script, name contains '###'
is used as the search query. So, when there are multiple files are returned by one filename, those file IDs are put in the array.
If ListIDS
is large, the above pattern might not be able to be used. So, when pattern 1 cannot be used, please use this pattern 2. In this pattern, using ListIDS= ['A','B','C']
, the multiple search queries are used.
ListIDS = ['A','B','C'] # filenames
service = create_service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES)
files = []
for filename in ListIDS:
query = "name contains " + "'" + filename + "' and trashed = false"
response = service.files().list(q=query, fields='files(id,name)').execute()
files.extend(response.get('files'))
values = {}
for e in files:
for f in ListIDS:
if f in e.get('name'):
id = e.get('id')
v = "https://drive.google.com/file/d/" + id + "/view?usp=sharing"
if values.get(f):
values[f].append(v)
else:
values[f] = [v]
print(values)
Upvotes: 1