Skip to content

Instantly share code, notes, and snippets.

@nadavkav
Created September 13, 2020 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nadavkav/fc531ea618211ffe4da40ac450b684f4 to your computer and use it in GitHub Desktop.
Save nadavkav/fc531ea618211ffe4da40ac450b684f4 to your computer and use it in GitHub Desktop.
Download UNICKO videos (php script via web API)
<?php
//require_once('config.php');
//require_once($CFG->libdir.'/filelib.php');
//use curl;
@ini_set('display_errors', '1');
@ini_set('html_errors', '1');
//$CFG->debug = (E_ALL | E_STRICT); // === DEBUG_DEVELOPER - NOT FOR PRODUCTION SERVERS!
//$CFG->debugdisplay = true;
function get_recordings($skip, $limit, $apiUrl, $consumerKey, $consumerSecret) {
$payload = [];
$payload['request_type'] = 'recordings';
$payload['limit'] = $limit;
$payload['skip'] = $skip;
$payload['state'] = 'ready';
$expire = 60 * 60;
$token = create_token($consumerKey, $consumerSecret, $payload, $expire);
$data = send_request($apiUrl, $token);
//$data = send_request_moodle($apiUrl, $token);
return json_decode($data);
}
function delete_recording($recording_id, $apiUrl, $consumerKey, $consumerSecret) {
$payload = [];
$payload['request_type'] = 'recording_delete';
$payload['id'] = $recording_id;
$expire = 60 * 60;
$token = create_token($consumerKey, $consumerSecret, $payload, $expire);
$data = send_request($apiUrl, $token);
//$data = send_request_moodle($apiUrl, $token);
return json_decode($data);
}
function get_user_sessions($skip, $limit, $apiUrl, $consumerKey, $consumerSecret) {
$payload = [];
$payload['request_type'] = 'user_sessions';
$payload['limit'] = $limit;
$payload['skip'] = $skip;
$expire = 60 * 60;
$token = create_token($consumerKey, $consumerSecret, $payload, $expire);
$data = send_request($apiUrl, $token);
//$data = send_request_moodle($apiUrl, $token);
return json_decode($data);
}
function send_request($url, $token) {
$headers = [];
$headers[] = "Accept: application/json";
//echo $fullurl = $url."?signed_request=".urlencode($token);
//$ch = curl_init($fullurl);
//$ch = curl_init($url."?signed_request=".urlencode($token));
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, "signed_request=".urlencode($token));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
//curl_setopt($ch, CURLOPT_PROXY, 'proxy:port');
//curl_setopt($ch, CURLOPT_POSTFIELDS, $contents);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_VERBOSE, 1);
$res=curl_exec($ch);
//echo "<hr>";
//print_r($res);
//echo "<hr>";
curl_close($ch);
return $res;
}
function send_request_moodle($url, $token) {
$headers = [];
$headers[] = "Accept: application/json";
//$ch = curl_init($url."?signed_request=".urlencode($token));
$curl = new \curl();
$resp = $curl->get($url."?signed_request=".urlencode($token));
//curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//curl_setopt($ch, CURLOPT_POST, true);
//curl_setopt($ch, CURLOPT_PROXY, 'proxy:port');
#curl_setopt($ch, CURLOPT_POSTFIELDS, $contents);
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//$res=curl_exec($ch);
//curl_close($ch);
return $resp;
}
function create_token($consumerKey, $consumerSecret, $payload, $expire) {
$version = 3;
$algorithm = "HMAC-SHA256";
$nonce = generate_nonce();
$issuedAt = time();
$expires = $issuedAt + $expire;
$payload["version"] = $version;
$payload["consumer_key"] = $consumerKey;
$payload["algorithm"] = $algorithm;
$payload["nonce"] = $nonce;
$payload["issued_at"] = $issuedAt;
$payload["expires"] = $expires;
return sign_request($payload, $consumerSecret);
}
function sign_request($payload, $secret) {
$data = base64_url_encode(json_encode($payload));
$signature = base64_url_encode(hash_hmac('sha256', $data, $secret, $raw = true));
return $signature . "." . $data;
}
function base64_url_encode($input) {
return strtr(rtrim(base64_encode($input), '='), '+/', '-_');
}
function generate_nonce() {
$random_string = openssl_random_pseudo_bytes(10);
return base64_encode(md5(time() . $random_string, true));
}
// API URL, Consumer Key, Consumer Secret
// https://www.unicko.com/docs/api
$apiUrl = "https://davidson.unicko.com/api";
$consumerKey = "use-your-domain";
$consumerSecret = "Use your UNICKO secret";
echo "Checking unicko WS...\n";
$recordings = get_recordings(0, 50, $apiUrl, $consumerKey, $consumerSecret);
echo "Count=".count($recordings->recordings)."<br>\n";
//echo "recordings\n";
//var_dump($recordings);
//echo json_encode($recordings->recordings, true);
echo "<hr>\n";
foreach ($recordings->recordings as $recording) {
$epoch = $recording->started_at;
$dt = new DateTime("@$epoch"); // convert UNIX timestamp to PHP DateTime
//echo $dt->format('Y-m-d'); // output = 2012-08-15 00:00:00
echo "\nstate={$recording->state} unickoid={$recording->recording_id} ";
if ($recording->state == 'deleted') {
echo "\n";
}
if ($recording->state == 'ready') {
// If not in wp-vod db, we need to download it...
if (!in_array($recording->recording_id, $processed_videos)) {
echo " - [Needs Downloading] \n";
echo $wget_video = "wget '{$recording->url}' ".
"-O /vod/unickoid={$recording->recording_id}_mcid={$recording->course_ext_id}_mrid={$recording->room_ext_id}".
"_date=".gmdate('Y-m-d', $epoch/1000). //$dt->format('Y-m-d').
"_roomname=".preg_replace("/\"|\-\-/",'',preg_replace("/\/|\s|\'|\(|\)/", '-',$recording->room_name)).".mp4";
$ok = exec($wget_video);
print_r($ok);//die; // Download one one at a time.
} else {
// We already downloaded it, then... delete it...
echo " - [Needs deleting!] \n";
// disable delete temporarly (nadav 28/2/2018)
if ((isset($_GET['delete']) && $_GET['delete'] == 'yes') OR $argv[1] == 'delete') {
$response = delete_recording((int)$recording->recording_id, $apiUrl, $consumerKey, $consumerSecret);
echo "\nDeleted? = ".var_dump($response)."\n";
}
}
}
}
//$sessions = get_user_sessions(0, 10, $apiUrl, $consumerKey, $consumerSecret);
//echo "sessions\n";
//var_dump($sessions);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment