Skip to content
This repository has been archived by the owner on Sep 9, 2021. It is now read-only.

Commit

Permalink
Add proper handling of Google Drive shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
eXhumer committed May 18, 2020
1 parent 24af29a commit 36c69d8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
3 changes: 2 additions & 1 deletion TinGen.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
parser.add_argument("--add-nsw-files-without-title-id", action="store_true", help="Adds files without title ID")
parser.add_argument("--add-non-nsw-files", action="store_true", help="Adds files without valid NSW ROM extension(NSP/NSZ/XCI/XCZ) to index")
parser.add_argument("--success", metavar="SUCCESS_MESSAGE", help="Adds a success message to index file to show if index is successfully read by tinfoil")
parser.add_argument("--ignore-gdrive-shortcuts", action="store_true", help="Skip including any Google Drive shortcuts")

parser.add_argument("--encrypt", action="store_true", help="Encrypts the resulting index file")
parser.add_argument("--public-key", metavar="PUBLIC_KEY_FILE_PATH", default="public.pem", help="File Path for Public Key to encrypt with")
Expand All @@ -38,7 +39,7 @@
generator = TinGen(args.token, args.credentials, args.headless)

print(f"Generating index")
generator.index_generator(args.folder_ids, args.recursion, args.add_nsw_files_without_title_id, args.add_non_nsw_files)
generator.index_generator(args.folder_ids, args.recursion, args.add_nsw_files_without_title_id, args.add_non_nsw_files, args.ignore_gdrive_shortcuts)

if args.success:
print(f"Adding success message to index")
Expand Down
38 changes: 27 additions & 11 deletions TinGen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,14 @@ def _apicall(self, request, maximum_backoff=32):
raise Exception("Unretryable Error")
return response

def _ls(self, folder_id, fields="files(id,name,size,permissionIds),nextPageToken", searchTerms=""):
def get_file_meta(self, file_id, fields="id,name,size,permissionIds"):
return self._apicall(self.drive_service.files().get(
file_id,
fields=fields,
supportsAllDrives=True
))

def _ls(self, folder_id, fields="files(id,name,size,permissionIds,shortcutDetails(targetId)),nextPageToken", searchTerms=""):
files = []
resp = {"nextPageToken": None}
while "nextPageToken" in resp:
Expand All @@ -117,27 +124,27 @@ def _ls(self, folder_id, fields="files(id,name,size,permissionIds),nextPageToken
def _lsd(self, folder_id):
return self._ls(
folder_id,
searchTerms="mimeType contains \"application/vnd.google-apps.folder\""
searchTerms="mimeType = \"application/vnd.google-apps.folder\" or shortcutDetails.targetMimeType = \"application/vnd.google-apps.folder\""
)

def _lsf(self, folder_id, fields="files(id,name,size,permissionIds),nextPageToken"):
def _lsf(self, folder_id, fields="files(id,name,size,permissionIds,shortcutDetails(targetId)),nextPageToken"):
return self._ls(
folder_id,
fields=fields,
searchTerms="not mimeType contains \"application/vnd.google-apps.folder\""
searchTerms="mimeType != \"application/vnd.google-apps.folder\" or shortcutDetails.targetMimeType != \"application/vnd.google-apps.folder\""
)

def _lsd_my_drive(self):
return self._ls(
"root",
searchTerms="mimeType contains \"application/vnd.google-apps.folder\""
searchTerms="mimeType = \"application/vnd.google-apps.folder\" or shortcutDetails.targetMimeType = \"application/vnd.google-apps.folder\""
)

def _lsf_my_drive(self, fields="files(id,name,size,permissionIds),nextPageToken"):
def _lsf_my_drive(self, fields="files(id,name,size,permissionIds,shortcutDetails(targetId)),nextPageToken"):
return self._ls(
"root",
fields=fields,
searchTerms="not mimeType contains \"application/vnd.google-apps.folder\""
searchTerms="mimeType != \"application/vnd.google-apps.folder\" or shortcutDetails.targetMimeType != \"application/vnd.google-apps.folder\""
)

def check_file_shared(self, file_to_check):
Expand All @@ -153,17 +160,26 @@ def check_file_shared(self, file_to_check):
def delete_file_permission(self, file_id, permission_id):
self._apicall(self.drive_service.permissions().delete(fileId=file_id, permissionId=permission_id, supportsAllDrives=True))

def get_all_files_in_folder(self, folder_id: str, recursion: bool, progress_bar: tqdm) -> dict:
def get_all_files_in_folder(self, folder_id: str, recursion: bool, progress_bar: tqdm, ignore_gdrive_shortcuts: bool) -> dict:
files = {}

for _file in self._lsf(folder_id):
follow_shortcut = not ignore_gdrive_shortcuts and True
if "size" in _file:
files.update({_file["id"]: {"size": _file["size"], "name": _file["name"], "shared": self.check_file_shared(_file)}})
progress_bar.update(1)
if "shortcutDetails" not in _file:
files.update({_file["id"]: {"size": _file["size"], "name": _file["name"], "shared": self.check_file_shared(_file)}})
progress_bar.update(1)
elif "shortcutDetails" in _file and not ignore_gdrive_shortcuts:
file_meta = self.get_file_meta(_file["shortcutDetails"]["targetId"])
files.update({file_meta["id"]: {"size": file_meta["size"], "name": file_meta["name"], "shared": self.check_file_shared(file_meta)}})
progress_bar.update(1)

if recursion:
for _folder in self._lsd(folder_id):
files.update(self.get_all_files_in_folder(_folder["id"], recursion, progress_bar))
if "shortcutDetails" not in _folder:
files.update(self.get_all_files_in_folder(_folder["id"], recursion, progress_bar, ignore_gdrive_shortcuts))
elif "shortcutDetails" in _file and not ignore_gdrive_shortcuts:
files.update(self.get_all_files_in_folder(_folder["shortcutDetails"]["targetId"], recursion, progress_bar, ignore_gdrive_shortcuts))

return files

Expand Down

1 comment on commit 36c69d8

@eXhumer
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes #11.

Please sign in to comment.