Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

YouTube Shell/Remixer Queries URL for default start/end times #142

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions coffee/src/shells/videolink.shell.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,12 @@ class VideoLinkShell.RemixView extends LinkShell.RemixView
_setTimeInputMax: =>
@timeRangeView.setMax @model.timeTotal()

_setClipRange: =>
@timeRangeView.values {
start: @model.timeStart()
end: @model.timeEnd()
}


onChangeTimes: (changed) =>
changes = {}
Expand Down
49 changes: 46 additions & 3 deletions coffee/src/shells/youtube.shell.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ YouTubeShell = acorn.shells.YouTubeShell =
]



class YouTubeShell.Model extends VideoLinkShell.Model


Expand Down Expand Up @@ -57,6 +56,20 @@ class YouTubeShell.Model extends VideoLinkShell.Model

pattern.exec(link)[3]

parseTime: (time) =>
validTimePatterns = [
/(\d+)/
/(\d+)m(\d+)s/
]

# the second one is more general, work backwards
match = validTimePatterns[1].exec time
if match
return 60 * parseInt(match[1]) + parseInt(match[2])
match = validTimePatterns[0].exec time
if validTimePatterns[0].test time
return parseInt(match[1])
return undefined

embedLink: (options) =>
# see https://developers.google.com/youtube/player_parameters for options
Expand Down Expand Up @@ -95,16 +108,46 @@ class YouTubeShell.RemixView extends VideoLinkShell.RemixView
super
@metaData().sync success: @onMetaDataSync

_timeLinkParam: (keys) =>
unless _.isArray keys
keys = [keys]
param = acorn.util.fetchParameters this.model.link(), keys
return @model.parseTime _.values(param)[0]

# Default start/end can only be set once player metadata
# is avaiilable and initialized. Otherwise, default values
# will override the start/end times.
initializeDefaultClip: =>
firstNumber = (args) ->
_.find args, _.isNumber

start = firstNumber [
@model.timeStart()
@_timeLinkParam ["t", "start"]
0
]

end = firstNumber [
@model.timeEnd()
@_timeLinkParam ["end"]
@model.timeTotal()
]

end = if end >= start then end else @model.timeTotal()
# Clip range must be set before progress bar is initialized
@model.timeStart(start)
@model.timeEnd(end)
@_setClipRange()

onMetaDataSync: (data) =>
@model._fetchedDefaults ?= {}
@model._fetchedDefaults = title: data.data.title
@model._updateAttributesWithDefaults()
@model.timeTotal data.data.duration

@model._updateAttributesWithDefaults()
@initializeDefaultClip()
@_setTimeInputMax()


metaData: =>
if @model.metaDataUrl() and not @_metaData
@_metaData = new athena.lib.util.RemoteResource
Expand Down
17 changes: 17 additions & 0 deletions coffee/src/util/util.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,24 @@ util.parseUrl = (url) ->

result

# function takes a URL and an array of parameters
# returns JSON object with matching parameters and values, if any
# otherwise, returns empty object
# http://stackoverflow.com/a/8649003
util.fetchParameters = (url, params) ->
url = $.trim url
url = "http://#{url}" unless /^([a-z0-9]+:)?\/\//i.test url
anchor = document.createElement 'a'
anchor.href = url

search = anchor.search.substring(1)
search = '{"' + search.replace(/&/g, '","').replace(/\=/g, '":"') + '"}'

parameters = JSON.parse search, (key, value) ->
if key is "" then value else decodeURIComponent value

_.pick parameters, params


# track mouse location at all times
util.mouseLocationTracker = (->
Expand Down
1 change: 0 additions & 1 deletion coffee/src/views/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
`import "shell_editor_view"`
`import "shell_options_view"`
`import "shell_selector_view"`
`import "slider_handle_view"`
`import "sliding_bar_view"`
`import "sliding_object_view"`
`import "sources_view"`
Expand Down