-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathsample.py
More file actions
170 lines (139 loc) · 5.84 KB
/
Copy pathsample.py
File metadata and controls
170 lines (139 loc) · 5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!python3
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
# Top level folder is represented by zero GUID.
# However, it is not the real folder and some API beahves differently than actual folder.
GUID_TOPLEVEL = '00000000-0000-0000-0000-000000000000'
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)')
return parser.parse_args()
def main():
args = parse_argument()
if args.skip_verify:
# This line is needed to suppress annoying warning message.
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# Use requests module's Session object in this example.
# ref. https://2.python-requests.org/en/master/user/advanced/#session-objects
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)
current_folder_id = GUID_TOPLEVEL
while True:
print('----------------------------')
current_folder = get_and_display_folder(folders, current_folder_id)
sub_folders = get_and_display_sub_folders(folders, current_folder_id)
current_folder_id = process_selection(folders, current_folder, sub_folders)
def get_and_display_folder(folders, folder_id):
'''
Returning folder object that is returned by API.
None if it is top level folder.
'''
print()
print('Folder:')
if folder_id == GUID_TOPLEVEL:
print(' Top level folder (no detail informaiton is available)')
return None
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')
else:
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']))
return folder
def get_and_display_sub_folders(folders, current_folder_id):
print()
print('Sub Folders:')
children = folders.get_children(current_folder_id)
# returning object is the dictionary, key (integer) and folder's ID (UUID)
result = {}
key = 0
for entry in children:
result[key] = entry['Id']
print(' [{0}]: {1}'.format(key, entry['Name']))
key += 1
return result
def process_selection(folders, current_folder, sub_folders):
if current_folder is None:
new_folder_id = GUID_TOPLEVEL
parent_folder_id = GUID_TOPLEVEL
else:
new_folder_id = current_folder['Id']
if current_folder['ParentFolder'] is None:
parent_folder_id = GUID_TOPLEVEL
else:
parent_folder_id = current_folder['ParentFolder']['Id']
print()
print('[P] Go to parent')
print('[R] Rename this folder')
print('[D] Delete this folder')
print('[S] Search folders')
print('[L] List sessions in the folder')
print()
selection = input('Enter the command (select number to move folder): ')
try:
key = int(selection)
if sub_folders[key]:
return sub_folders[key]
except:
pass # selection is not a number, fall through
if selection.lower() == 'p':
new_folder_id = parent_folder_id
elif selection.lower() == 'r' and current_folder is not None:
rename_folder(folders, current_folder)
elif selection.lower() == 'd' and current_folder is not None:
if delete_folder(folders, current_folder):
new_folder_id = parent_folder_id
elif selection.lower() == 's':
result = search_folder(folders)
if result is not None:
new_folder_id = result
elif selection.lower() == 'l' and current_folder is not None:
list_sessions(folders, current_folder)
else:
print('Invalid command.')
return new_folder_id
def rename_folder(folders, folder):
new_name = input('Enter new name: ')
return folders.update_folder_name(folder['Id'], new_name)
def delete_folder(folders, folder):
return folders.delete_folder(folder['Id'])
def search_folder(folders):
query = input('Enter search keyword: ')
entries = folders.search_folders(query)
if len(entries) == 0:
print(' No hit.')
return None
for index in range(len(entries)):
print(' [{0}]: {1}'.format(index, entries[index]['Name']))
selection = input('Enter the number (or just enter to stay current): ')
new_folder_id = None
try:
index = int(selection)
if 0 <= index < len(entries):
new_folder_id = entries[index]['Id']
except:
pass
return new_folder_id
def list_sessions(folders, folder):
print('Sessions in the folder:')
for entry in folders.get_sessions(folder['Id']):
print(' {0}: {1}'.format(entry['Id'], entry['Name']))
if __name__ == '__main__':
main()