Skip to content

Instantly share code, notes, and snippets.

@DavidBuchanan314
Last active June 22, 2023 22:21
Show Gist options
  • Save DavidBuchanan314/26aa8bd765807798d917b983ac13213b to your computer and use it in GitHub Desktop.
Save DavidBuchanan314/26aa8bd765807798d917b983ac13213b to your computer and use it in GitHub Desktop.
Panopto video downloader
import requests
import json
import os
import youtube_dl
PANOPTO_BASE = "https://cardiff.cloud.panopto.eu"
"""
Place the value of your .ASPXAUTH token in the following variable
"""
TOKEN = "PUT_YOUR_AUTH_TOKEN_HERE"
s = requests.session() # cheeky global variable
s.cookies = requests.utils.cookiejar_from_dict({".ASPXAUTH": TOKEN})
# WHYYYY does panopto use at least 3 different types of API!?!?!?
def json_api(endpoint, params=dict(), post=False, paramtype="params"):
if post:
r = s.post(PANOPTO_BASE + endpoint, **{paramtype: params})
else:
r = s.get(PANOPTO_BASE + endpoint, **{paramtype: params})
if not r.ok:
print(r.text)
return json.loads(r.text)
def name_normalize(name):
return name.replace("/", "-")
def dl_session(session):
dest_dir = os.path.join(
"downloads",
name_normalize(session["FolderName"]),
name_normalize(session["SessionName"])
)
if not os.path.exists(dest_dir):
os.makedirs(dest_dir)
delivery_info = json_api("/Panopto/Pages/Viewer/DeliveryInfo.aspx", {
"deliveryId": session["DeliveryID"],
"responseType": "json"
}, True, "data")
streams = delivery_info["Delivery"]["Streams"]
for i in range(len(streams)):
filename = "{:02d}_{}.mp4".format(i, streams[i]["Tag"])
dest_filename = os.path.join(dest_dir, filename)
print("Downloading:", dest_filename)
ydl_opts = {
"outtmpl": dest_filename,
"quiet": True
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([streams[i]["StreamUrl"]])
def dl_folder(folder):
sessions = json_api("/Panopto/Services/Data.svc/GetSessions", {
"queryParameters": {
"folderID": folder["Id"],
}
}, True, "json")["d"]["Results"]
for session in sessions:
dl_session(session)
folders = json_api("/Panopto/Api/v1.0-beta/Folders", {
"parentId": "null",
"folderSet": 1
})
for folder in folders:
"""
Put an if statement here based on folder["Name"] if you just want a certain
module or year etc.
e.g.:
"""
if folder["Name"].startswith("1819"):
dl_folder(folder)
@eulerou
Copy link

eulerou commented Aug 25, 2021

Hi, could you tell me where I can find the downloaded file? Thank you!

@DavidBuchanan314
Copy link
Author

DavidBuchanan314 commented Aug 25, 2021

I haven't used this for a while, but it looks like it goes in a folder named downloads in the current working directory.

You'll also need to change the if statement at the end if you want it to download anything at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment