Skip to content

Commit

Permalink
videolink: url time parsing logic working
Browse files Browse the repository at this point in the history
  • Loading branch information
anshukla committed Aug 8, 2013
1 parent 156b144 commit 73f7ff2
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
35 changes: 34 additions & 1 deletion 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,19 @@ 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
if validTimePatterns[1].test time
return 60 * parseInt(validTimePatterns[1].exec(time)[1]) + parseInt(validTimePatterns[1].exec(time)[2])

This comment has been minimized.

Copy link
@jbenet

jbenet Aug 9, 2013

Member

might want to do something like:

match = validTimePatterns[1].exec(time)
if match
  return 60 * parseInt(match[1]) + parseInt(match[2])

it's clearer.

else if validTimePatterns[0].test time
return parseInt(validTimePatterns[0].exec(time)[0])
else
return undefined

embedLink: (options) =>
# see https://developers.google.com/youtube/player_parameters for options
Expand Down Expand Up @@ -102,6 +114,27 @@ class YouTubeShell.RemixView extends VideoLinkShell.RemixView
@model.timeTotal data.data.duration

@model._updateAttributesWithDefaults()
start = acorn.util.fetchParameters this.model.link(), ["t", "start"]

This comment has been minimized.

Copy link
@jbenet

jbenet Aug 9, 2013

Member

this start = is immediately overridden below.

# take the first possible valid parameter

start = @model.timeStart()
unless _.isNumber start
start = acorn.util.fetchParameters this.model.link(), ["t", "start"]
start = @model.parseTime _.values(start)[0]
unless _.isNumber start
start = 0

end = @model.timeEnd()
unless _.isNumber end
end = acorn.util.fetchParameters this.model.link(), ["end"]
end = @model.parseTime _.values(end)[0]
unless _.isNumber end
end = @model.timeTotal()

This comment has been minimized.

Copy link
@jbenet

jbenet Aug 9, 2013

Member

I'd write a function (even a private one in YT shell, or a util) to do:

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

Which makes the rest:

start = @model.timeStart()
unless _.isNumber start
  start = @_timeLinkParam ["t", "start"]
unless _.isNumber start
  start = 0

end = @model.timeEnd()
unless _.isNumber end
  end = @_timeLinkParam ["end"]
unless _.isNumber end
  end = @model.timeTotal()

which I might still turn into:

# local helper
firstNumber = =>
  _.find (arguments), _.isNumber

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

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

@model.timeStart(start)
@model.timeEnd(end)

@_setTimeInputMin()
@_setTimeInputMax()


Expand Down
19 changes: 19 additions & 0 deletions coffee/src/util/util.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,26 @@ 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, '":"') + '"}'

This comment has been minimized.

Copy link
@jbenet

jbenet Aug 9, 2013

Member

is this robust? What if there are quotes in the parameters? (e.g.: &foo=bar"&baz)


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

This comment has been minimized.

Copy link
@jbenet

jbenet Aug 9, 2013

Member

search is "" will always be false because above, we have search = '{"' + ... + '"}'



console.log(parameters)

This comment has been minimized.

Copy link
@jbenet

jbenet Aug 9, 2013

Member

comment out or remove :)

This comment has been minimized.

Copy link
@jbenet

jbenet Aug 9, 2013

Member

Ah, handled in ddafc3b

_.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

1 comment on commit 73f7ff2

@jbenet
Copy link
Member

@jbenet jbenet commented on 73f7ff2 Aug 9, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, solid approach! :) a few stylistic/logic things above

Please sign in to comment.