Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
use Panopto API to delete all empty subfolders of given folder
import sys
import argparse
import requests
import urllib3
from panopto_folders import PanoptoFolders
from os.path import dirname, join, abspath
sys.path.insert(0, abspath(join(dirname(__file__), '..', 'common')))
from panopto_oauth2 import PanoptoOAuth2
def parse_argument():
parser = argparse.ArgumentParser(description='Sample of Folders API')
parser.add_argument('--server', dest='server', required=True, help='Server name as FQDN')
parser.add_argument('--client-id', dest='client_id', required=True, help='Client ID of OAuth2 client')
parser.add_argument('--client-secret', dest='client_secret', required=True, help='Client Secret of OAuth2 client')
parser.add_argument('--skip-verify', dest='skip_verify', action='store_true', required=False, help='Skip SSL certificate verification. (Never apply to the production code)')
parser.add_argument('--parent', dest='parent', required=True, help='UUID of the parent folder')
return parser.parse_args()
def main():
args = parse_argument()
if args.skip_verify:
# This line is needed to suppress annoying warning message.
# Use requests module's Session object in this example.
# ref.
requests_session = requests.Session()
requests_session.verify = not args.skip_verify
# Load OAuth2 logic
oauth2 = PanoptoOAuth2(args.server, args.client_id, args.client_secret, not args.skip_verify)
# Load Folders API logic
folders = PanoptoFolders(args.server, not args.skip_verify, oauth2)
get_and_display_folder(folders, args.parent)
# Archives subfolders
children = folders.get_children(args.parent)
for child in children:
delete_folder(folders, child)
def get_and_display_folder(folders, folder_id):
Returning folder object that is returned by API.
None if it is top level folder.
folder = folders.get_folder(folder_id)
print(' Name: {0}'. format(folder['Name']))
print(' Id: {0}'. format(folder['Id']))
if folder['ParentFolder'] is None:
print(' Parent Folder: Top level folder')
print(' Parent Folder: {0}'. format(folder['ParentFolder']['Name']))
print(' Folder URL: {0}'. format(folder['Urls']['FolderUrl']))
print(' Embed URL: {0}'. format(folder['Urls']['EmbedUrl']))
print(' Share settings URL: {0}'. format(folder['Urls']['ShareSettingsUrl']))
def delete_folder(folders, folder):
sessions = folders.get_sessions(folder['Id'])
if len(sessions) > 0:
print('There are {0} sessions in folder "{1}", skipping deletion.'.format(len(sessions), folder['Name']))
return False
print('Deleting folder {0}'.format(folder['Name']))
return folders.delete_folder(folder['Id'])
if __name__ == '__main__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment