Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <section class="bcls-section" id="workingSample">
  <h2>Audio content report generator</h2>
  <p>You can view the results as:</p>
  <ul>
    <li><a href="#csvDisplay">CSV data</a></li>
    <li><a href="#tableDisplay">Table</a></li>
  </ul>

  <fieldset class="bcls-fieldset">
    <legend>Input</legend>

    <p>If you do not enter account information, a Brightcove sample account will be used.</p>

    <p>Account id: <input id="account_id" type="text" value="" placeholder="1752604059001" /></p>

    <p>Client id: <input id="client_id" type="text" size="60" value="" placeholder="c5d0a622-5479-46d8-8d8a-5f034b943fab" /></p>

    <p>Client secret: <input id="client_secret" type="text" size="60" value="" placeholder="w7NQYu0vUloM4GYYy2SXAxrvyFpt8fwI35qAFZcS13-VIgs0itwKNsAwHOS80sOWK" /></p>

    <p>Optional: retrieve videos with this tag: <input type="text" id="tag" value="" placeholder="foo" style="width:30%;display:inline;" /></p>

    <p>Videos to retrieve: <select name="videoCount" id="videoCount">
        <option value="25">25</option>
        <option value="50">50</option>
        <option value="100">100</option>
        <option value="500">500</option>
        <option value="All">All</option>
      </select></p>

    <aside class="bcls-aside bcls-aside--warning">If you select <code>All</code>, be prepared to wait awhile!</aside>

    <p><button class="bcls-button" id="makeReport">Generate the Report</button></p>
  </fieldset>

  <fieldset class="bcls-fieldset">
    <legend>Log</legend>

    <div id="logger" style="color:rgb(237, 104, 38)">
      <p id="logText">Waiting for input...</p>
    </div>

    <p>Current API request</p>

    <pre class="line-numbers">
<code id="apiRequest" class="language-http">API request will appear here...</code></pre>
  </fieldset>
</section>

<section id="csvDisplay" class="bcls-section">
  <h2>CSV Data</h2>

  <p>Copy and paste the data below into a an empty text file, and save it with a .csv extension. You can then open the file to view it in your spreadsheet application.</p>
  <textarea name="csvData" id="csvData" class="bcls-code" style="height:10em;"></textarea>
</section>
<section id="tableDisplay" class="bcls-section">
  <h2>Report table</h2>
  <table id="reportTable">
    <thead>
      <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Description</th>
        <th>Duration</th>
        <th>Renditions</th>
        <th>Created</th>
        <th>Updated</th>
      </tr>
    </thead>
    <tbody id="tableBody"></tbody>
  </table>
</section>

              
            
!

CSS

              
                /* * The body style is just for the
 * background color of the codepen.
 * Do not include in your code.
 */
body {
  background-color: #111;
  color: #f5f5f5;
  font-family: sans-serif;
  margin: 2em;
}

/*
 * Styles essential to the sample
 * are below
 */
.bcls-code {
  color: #111;
  padding: 1em;
  width: 90%;
  min-height: 5em;
  background-color: #fff;
  font-size: 1rem;
}

.bcls-button {
  padding: .5em;
  background-color: #333;
  border: 1px yellow solid;
  border-radius: .5em;
  color: yellow;
  cursor: pointer;
}

fieldset {
  color: #64AAB2;
  border: 1px #64AAB2 solid;
  border-radius: .5em;
}

code {
  color: #E4B842;
}

table {
  border-collapse: collapse;
  border: 1px #64AAB2 solid;
}

table th, table td {
  border: 1px #64AAB2 solid;
  padding: 4px;
}

a {
  color: rgb(237, 104, 38);
}
              
            
!

JS

              
                var BCLS = (function(window, document) {
  var account_id,
    client_id,
    client_secret,
    // api stuff
    proxyURL = 'https://solutions.brightcove.com/bcls/bcls-proxy/bcls-proxy-v2.php',
    baseURL = 'https://cms.api.brightcove.com/v1/accounts/',
    limit = 25,
    totalVideos = 0,
    totalCalls = 0,
    callNumber = 0,
    videosCompleted = 0,
    videosArray = [],
    audiosArray = [],
    summaryData = {},
    csvStr,
    summaryCsvStr,
    customFields = [],
    // elements
    account_id_element = document.getElementById('account_id'),
    client_id_element = document.getElementById('client_id'),
    client_secret_element = document.getElementById('client_secret'),
    tag = document.getElementById('tag'),
    videoCount = document.getElementById('videoCount'),
    makeReport = document.getElementById('makeReport'),
    tableBody = document.getElementById('tableBody'),
    content,
    logger = document.getElementById('logger'),
    logText = document.getElementById('logText'),
    csvData = document.getElementById('csvData'),
    apiRequest = document.getElementById('apiRequest'),
    allButtons = document.getElementsByName('button'),
    pLogGettingVideos = document.createElement('p'),
    pLogGettingRenditions = document.createElement('p'),
    pLogFinish = document.createElement('p'),
    spanIntro2 = document.createElement('span'),
    spanOf2 = document.createElement('span'),
    spanRenditionsTotal = document.createElement('span'),
    spanRenditionsCount = document.createElement('span'),
    spanRenditionsTotalEl,
    spanRenditionsCountEl;

  /**
   * tests for all the ways a variable might be undefined or not have a value
   * @param {String|Number} x the variable to test
   * @return {Boolean} true if variable is defined and has a value
   */
  function isDefined(x) {
    if (x === '' || x === null || x === undefined) {
      return false;
    }
    return true;
  }

  /*
   * tests to see if a string is json
   * @param {String} str string to test
   * @return {Boolean}
   */
  function isJson(str) {
    try {
      JSON.parse(str);
    } catch (e) {
      return false;
    }
    return true;
  }

  /*
   * tests to see if a string is json
   * @param {String} str string to test
   * @return {Boolean}
   */
  function isAudio(rendition) {
    if (rendition.hasOwnProperty('audio_only') && rendition.audio_only === false) {
      return false;
    } else if (rendition.hasOwnProperty('media_type') && rendition.media_type === 'video') {
      return false;
    }
    return true;
  }

  /**
   * get selected value for single select element
   * @param {htmlElement} e the select element
   */
  function getSelectedValue(e) {
    return e.options[e.selectedIndex].value;
  }

  /**
   * disables all buttons so user can't submit new request until current one finishes
   */
  function disableButtons() {
    var i,
      iMax = allButtons.length;
    for (i = 0; i < iMax; i++) {
      allButtons[i].setAttribute('disabled', 'disabled');
    }
  }

  /**
   * re-enables all buttons
   */
  function enableButtons() {
    var i,
      iMax = allButtons.length;
    for (i = 0; i < iMax; i++) {
      allButtons[i].removeAttribute('disabled');
    }
  }

  function processRenditions(video, renditions) {
    var i,
      iMax = renditions.length,
      audioRenditions = 0;
    if (video.id === '5831706803001') {}
    // separate renditions by type
    for (i = 0; i < iMax; i++) {
      if (isAudio(renditions[i])) {
        audioRenditions++;
      } else {
        // if any non-audio renditions, stop
        break;
      }
    }
    // check to see if all renditions are audio
    if (audioRenditions === renditions.length) {
      video.renditionCount = audioRenditions;
      audiosArray.push(video);
      return;
    } else {
      return;
    }
  }

  function writeReport() {
    var i,
      iMax,
      item,
      tr,
      td,
      frag = document.createDocumentFragment();
    csvStr = '"ID","Name","Description","Number of Renditions""Date Added","Date Last Modified",\r\n';
    if (audiosArray.length > 0) {
      iMax = audiosArray.length;
      for (i = 0; i < iMax; i++) {
        item = audiosArray[i];
        // replace any line breaks in description, as that will break the CSV
        if (item.description) {
          item.description = item.description.replace(/(?:\r\n|\r|\n)/g, ' ');
        }
        // generate the video detail row
        // add csv row
        csvStr += '"' + item.id + '","' + item.name + '","' + item.description + '","' + (item.duration / 1000) + '","' + item.renditionCount + '","' + item.created_at + '","' + item.updated_at + '",\r\n';
        // create the table row
        tr = document.createElement('tr');
        td = document.createElement('td');
        td.textContent = item.id;
        tr.appendChild(td);
        td = document.createElement('td');
        td.textContent = item.name;
        tr.appendChild(td);
        td = document.createElement('td');
        td.textContent = item.description;
        tr.appendChild(td);
        td = document.createElement('td');
        td.textContent = item.duration / 1000;
        tr.appendChild(td);
        td = document.createElement('td');
        td.textContent = item.renditionCount;
        tr.appendChild(td);
        td = document.createElement('td');
        td.textContent = item.created_at;
        tr.appendChild(td);
        td = document.createElement('td');
        td.textContent = item.updated_at;
        tr.appendChild(td);
        frag.appendChild(tr);
      }
      csvData.textContent += csvStr;
      tableBody.appendChild(frag);
      pLogFinish.textContent = 'Finished! See the results or get the CSV data below.';
      // reportDisplay.innerHTML = summaryReportStr + reportStr;
      enableButtons();
    } else {
      csvData.textContent = 'No audio-only content was found';
      pLogFinish.textContent = 'Finished! No audio-only content was found'
    }
  }

  /**
   * sets up the data for the API request
   * @param {String} id the id of the button that was clicked
   */
  function createRequest(id) {
    var endPoint = '',
      parsedData,
      options = {};
    options.proxyURL = proxyURL;
    options.account_id = account_id;
    if (isDefined(client_id) && isDefined(client_secret)) {
      options.client_id = client_id;
      options.client_secret = client_secret;
    }
    // disable buttons to prevent a new request before current one finishes
    disableButtons();
    switch (id) {
      case 'getCount':
        endPoint = account_id + '/counts/videos';
        if (isDefined(tag.value)) {
          endPoint += '&q=%2Btags:' + tag.value;
        }
        options.url = baseURL + endPoint;
        options.requestType = 'GET';
        apiRequest.textContent = options.url;
        makeRequest(options, function(response) {
          parsedData = JSON.parse(response);
          // set total videos
          video_count = parsedData.count;
          if (totalVideos === "All") {
            totalVideos = video_count;
          } else {
            totalVideos = (totalVideos < video_count) ? totalVideos : video_count;
          }
          totalCalls = Math.ceil(totalVideos / limit);
          logText.textContent = totalVideos + ' videos found; getting account custom fields';
          createRequest('getVideos');
        });
        break;
      case 'getVideos':
        var offset = (limit * callNumber);
        endPoint = account_id + '/videos?limit=' + limit + '&offset=' + offset;
        if (isDefined(tag.value)) {
          endPoint += '&q=%2Btags:' + tag.value;
        }
        options.url = baseURL + endPoint;
        options.requestType = 'GET';
        apiRequest.textContent = options.url;
        makeRequest(options, function(response) {
          parsedData = JSON.parse(response);
          videosArray = videosArray.concat(parsedData);
          callNumber++;
          if (callNumber < totalCalls) {
            createRequest('getVideos');
          } else {
            callNumber = 0;
            spanRenditionsCountEl.textContent = callNumber + 1;
            spanRenditionsTotalEl.textContent = totalVideos;
            totalCalls = totalVideos;
            createRequest('getVideoRenditions');
          }
        });
        break;
      case 'getVideoRenditions':
        if (callNumber < totalCalls) {
          var video = videosArray[callNumber];
          switch (videosArray[callNumber].delivery_type) {
            case 'remote':
              // won't be any renditions
              video.renditions = null;
              callNumber++;
              if (callNumber < totalCalls) {
                createRequest('getVideoRenditions');
              } else {
                // write the report
                writeReport();
              }
              break;
            case 'unknown':
              // won't be any renditions
              video.renditions = null;
              callNumber++;
              if (callNumber < totalCalls) {
                createRequest('getVideoRenditions');
              } else {
                // create csv headings
                writeReport();
              }
              break;
            case 'live_origin':
              // live stream; don't process
              video.renditions = null;
              callNumber++;
              if (callNumber < totalCalls) {
                createRequest('getVideoRenditions');
              } else {
                // write the report
                writeReport();
              }
              break;
            case 'static_origin':
              // legacy ingest
              endPoint = account_id + '/videos/' + video.id + '/assets/renditions';
              break;
            case 'dynamic_origin':
              // dynamic delivery
              endPoint = account_id + '/videos/' + video.id + '/assets/dynamic_renditions';
              break;
            default:
              console.log('should not be here - unknown delivery_type');
          }
          options.url = baseURL + endPoint;
          options.requestType = 'GET';
          apiRequest.textContent = options.url;
          spanRenditionsCountEl.textContent = callNumber + 1;
          makeRequest(options, function(response) {
            var renditions = JSON.parse(response);
            if (renditions.length > 0 && !renditions[0].hasOwnProperty('error_code')) {
              processRenditions(video, renditions);
            }
            videosCompleted++;
            logText.textContent = totalVideos + ' videos found; videos retrieved: ' + videosCompleted;
            callNumber++;
            if (callNumber === totalCalls) {
              // write the report
              writeReport();
            } else {
              createRequest('getVideoRenditions');
            }
          });
        }
        break;
      default:
        console.log('default case - should not be here');
    }
  }

  /**
   * send API request to the proxy
   * @param  {Object} options for the request
   * @param  {String} options.url the full API request URL
   * @param  {String="GET","POST","PATCH","PUT","DELETE"} requestData [options.requestType="GET"] HTTP type for the request
   * @param  {String} options.proxyURL proxyURL to send the request to
   * @param  {String} options.client_id client id for the account (default is in the proxy)
   * @param  {String} options.client_secret client secret for the account (default is in the proxy)
   * @param  {JSON} [options.requestBody] Data to be sent in the request body in the form of a JSON string
   * @param  {Function} [callback] callback function that will process the response
   */
  function makeRequest(options, callback) {
    var httpRequest = new XMLHttpRequest(),
      response,
      requestParams,
      dataString,
      proxyURL = options.proxyURL,
      // response handler
      getResponse = function() {
        try {
          if (httpRequest.readyState === 4) {
            if (httpRequest.status >= 200 && httpRequest.status < 300) {
              response = httpRequest.responseText;
              // some API requests return '{null}' for empty responses - breaks JSON.parse
              if (response === '') {
                response = null;
              }
              // return the response
              callback(response);
            } else {
              logger.appendChild(document.createTextNode('There was a problem with the request. Request returned ' + httpRequest.status));
            }
          }
        } catch (e) {
          logger.appendChild(document.createTextNode('Caught Exception: ' + e));
        }
      };
    /**
     * set up request data
     * the proxy used here takes the following request body:
     * JSON.strinify(options)
     */
    // set response handler
    httpRequest.onreadystatechange = getResponse;
    // open the request
    httpRequest.open('POST', proxyURL);
    // open and send request
    httpRequest.send(JSON.stringify(options));
  }


  function init() {
    // event listeners
    csvData.addEventListener('click', function() {
      this.select();
    });
    // set up the log elements
    content = document.createTextNode('Getting renditions for video ');
    spanIntro2.appendChild(content);
    content = document.createTextNode(' of ');
    spanOf2.appendChild(content);
    spanRenditionsCount.setAttribute('id', 'spanRenditionsCount');
    spanRenditionsTotal.setAttribute('id', 'spanRenditionsTotal');
    pLogGettingRenditions.appendChild(spanIntro2);
    pLogGettingRenditions.appendChild(spanRenditionsCount);
    pLogGettingRenditions.appendChild(spanOf2);
    pLogGettingRenditions.appendChild(spanRenditionsTotal);
    logger.appendChild(pLogGettingRenditions);
    spanRenditionsCountEl = document.getElementById('spanRenditionsCount');
    spanRenditionsTotalEl = document.getElementById('spanRenditionsTotal');
    logger.appendChild(pLogFinish);

  }

  // button event handlers
  makeReport.addEventListener('click', function() {
    // in case of re-run, cleal the results
    csvData.textContent = '';
    // get the inputs
    client_id = client_id_element.value;
    client_secret = client_secret_element.value;
    account_id = (isDefined(account_id_element.value)) ? account_id_element.value : '1752604059001';
    totalVideos = getSelectedValue(videoCount);
    if (totalVideos !== 'All') {
      totalVideos = Number.parseInt(totalVideos, 10)
    }
    // only use entered account id if client id and secret are entered also
    if (!isDefined(client_id) || !isDefined(client_secret) || !isDefined(account_id)) {
      logger.appendChild(document.createTextNode('To use your own account, you must specify an account id, and client id, and a client secret - since at least one of these is missing, a sample account will be used'));
      account_id = '1752604059001';
    }
    // get video count
    createRequest('getCount');

  });

  init();
})(window, document);

              
            
!
999px

Console