Skip to content

Instantly share code, notes, and snippets.

@wnielson
Last active August 29, 2015 14:22
Show Gist options
  • Save wnielson/bed435005c0bd338e672 to your computer and use it in GitHub Desktop.
Save wnielson/bed435005c0bd338e672 to your computer and use it in GitHub Desktop.
client = require('./client')
apirequest = require('../node_modules/googleapis/lib/apirequest')
get_alternate_videos = (docid, callback) ->
parameters = {
options: {
url: "https://docs.google.com/get_video_info?docid={docid}",
method: "GET"},
context: client.drive,
params: {
docid: docid },
requiredParams: ["docid"],
pathParams: ["docid"]}
mediaUrls = []
apirequest parameters, (err, body, resp) ->
if err != null
# TODO: Handle error...
return
urls = decodeURIComponent decodeURIComponent body
urls = urls.replace ///\&url\=https:\/\////g, '\@'
itagDB = {}
containerDB = {'x-flv':'flv', 'webm': 'webm', 'mp4;+codecs="avc1.42001E,+mp4a.40.2"': 'mp4'}
reg = ///(\d+)/(\d+)x(\d+)/(\d+/\d+/\d+)\&?\,?///g
match = reg.exec urls
while match != null
itag = match[1]
codec = match[4]
itagDB[itag] = {
width: match[2],
height: match[3],
codec: codec
}
# Rename some codecs
if codec == '9/0/115'
itagDB[itag]['codec'] = 'h.264/aac'
else if codec == '99/0/0'
itagDB[itag]['codec'] = 'VP8/vorbis'
# Find next match
match = reg.exec urls
reg = ///\@([^\@]+)///g
match = reg.exec urls
while match != null
videoURL = 'https://'+match[1]
reg2 = ///itag\=(\d+)[\s\S]*?type\=video\/([^\&]+)\&quality\=(\w+)///g
q = reg2.exec videoURL
while q != null
itag = q[1]
container = q[2]
quality = q[3]
# Add new video to list
mediaUrls.push {
quality: quality,
width: itagDB[itag].width,
height: itagDB[itag].height,
codec: itagDB[itag].codec,
container: container,
extension: containerDB[container],
url: videoURL}
# Next match
q = reg2.exec videoURL
# Next match
match = reg.exec urls
# Print out results
console.log "Found "+mediaUrls.length+" alternate videos"
# Call the callback
if callback
callback mediaUrls
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment