<commit_before>-# %iframe#player(type="text/html" width="1" height="1" frameborder="0")
#player

%label#now_playing Now Playing:
%label#next_up Next Up:
%input#song_input(placeholder="Enter artist/song")
%button#play Play
%button#next Next

:coffeescript
  loadPlayerAPI = (callback) ->
    # Load player api asynchronously.
    tag = document.createElement('script')
    tag.src = "http://www.youtube.com/player_api"
    firstScriptTag = document.getElementsByTagName('script')[0]
    firstScriptTag.parentNode.insertBefore(tag, firstScriptTag)
    done = false
    player = null

    window.onYouTubePlayerAPIReady = ->
      callback()

  loadPlayerAPI ->
    getSongs("<NAME>")

    $("#play").click ->
      getSongs($("#song_input").val())
      $("#song_input").val('')

    $("#next").click ->
      nextSong()

    $("#song_input").bind "keydown", "return", ->
      getSongs($("#song_input").val())
      $("#song_input").val('')

  entries = []
  currentIndex = -1

  getSongs = (terms) ->
    url = "http://gdata.youtube.com/feeds/api/videos/-/{http%3A%2F%2Fgdata.youtube.com%2Fschemas%2F2007%2Fcategories.cat}Music?alt=json&callback=?"

    $.getJSON url, {
      enablejsapi: 1
      q: terms
    }, (result) ->
      console.log result

      entries = processSearchResults(result.feed.entry)

      nextSong()

  processSearchResults = (results) ->
    results.map (result) ->
      id: result.id.$t.split('/').last()
      title: result.title.$t

  nextSong = () ->
    currentIndex += 1
    entry = entries[currentIndex]

    id = entry.id
    createPlayer(id)

    title = entry.title
    $("#now_playing").text("Now Playing: \#{title}")

    if nextTitle = entries[currentIndex + 1]?.title
      $("#next_up").text("Next Up: \#{nextTitle}")
    else
      $("#next_up").text("Next Up: ...")

  createPlayer = (id) ->
    srcUrl = "http://www.youtube.com/embed/\#{id}?autoplay=1"
    $("#player").attr("src", srcUrl)

  createPlayer = (id) ->
    $("#player").empty()

    window.player = new YT.Player 'player',
      width: 1
      height: 1
      playerVars:
        controls: 0
      videoId: id
      events:
        onReady: (event) ->
          event.target.playVideo()
        onStateChange: (event) ->
          if event.data == YT.PlayerState.ENDED
            nextSong()
<commit_msg>Added playlist and search results<commit_after>%style
  :sass
    @import util

    #playlist
      +inline-block
      min-height: 300px
      padding: 1em
      vertical-align: top
      width: 240px

    #search_results
      +inline-block
      padding: 1em
      vertical-align: top
      width: 240px

    .song
      cursor: pointer
      overflow: auto
      width: 100%

      &:hover
        background-color: $hover

      .duration
        float: right
        overflow: hidden
        text-align: right
        width: 30px

      .title
        +ellipsis
        float: left
        width: 185px

#player

.controls
  %label#now_playing Now Playing:
  %input#song_input(placeholder="Enter artist/song")
  %button#search Search
  %button#next Next

#playlist.module
  %h2 Playlist
  %ul.songs
#search_results.module
  %h2 Search Results
  %ul.songs

#song_template.template
  %li.song(data-id="${id}" title="${title}")
    .title ${title}
    .duration ${durationFormat(duration)}

:coffeescript
  window.player = null

  window.durationFormat = (time) ->
    minutes = (time / 60).floor()
    seconds = (time % 60)
    seconds = "0" + seconds if seconds < 10

    minutes + ":" + seconds

  $("#search_results .song").live "click", ->
    $(this).appendTo("#playlist .songs")

  loadPlayerAPI = (callback) ->
    # Load player api asynchronously.
    tag = document.createElement('script')
    tag.src = "http://www.youtube.com/player_api"
    firstScriptTag = document.getElementsByTagName('script')[0]
    firstScriptTag.parentNode.insertBefore(tag, firstScriptTag)
    done = false
    player = null

    window.onYouTubePlayerAPIReady = ->
      callback()

  loadPlayerAPI ->
    getSongs("<NAME>")

    $("#search").click ->
      getSongs($("#song_input").val())
      $("#song_input").val('')

    $("#next").click ->
      nextSong()

    $("#song_input").bind "keydown", "return", ->
      getSongs($("#song_input").val())
      $("#song_input").val('')

  getSongs = (terms) ->
    url = "http://gdata.youtube.com/feeds/api/videos/-/{http%3A%2F%2Fgdata.youtube.com%2Fschemas%2F2007%2Fcategories.cat}Music?alt=json&callback=?"

    $.getJSON url, {
      enablejsapi: 1
      q: terms
    }, (result) ->
      console.log result

      entries = processSearchResults(result.feed.entry)

      $("#song_template").tmpl(entries).appendTo($("#search_results .songs").empty())

      if !player || player.state == -1
        nextSong()

  processSearchResults = (results) ->
    results.map (result) ->
      unless result.yt$noembed
        duration: result.media$group.yt$duration.seconds
        id: result.id.$t.split('/').last()
        title: result.title.$t
    .compact()

  nextSong = () ->
    song = $("#playlist .songs .song").first()

    song = $("#search_results .songs .song").first() unless song.length

    if song.length
      id = song.data("id")
      title = song.attr("title")

      createPlayer(id)
      $("#now_playing").text("Now Playing: \#{title}")

      song.remove()

  createPlayer = (id) ->
    srcUrl = "http://www.youtube.com/embed/\#{id}?autoplay=1"
    $("#player").attr("src", srcUrl)

  createPlayer = (id) ->
    $("#player").empty()

    window.player = new YT.Player 'player',
      width: 1
      height: 1
      playerVars:
        controls: 0
      videoId: id
      events:
        onReady: (event) ->
          event.target.playVideo()

        onStateChange: (event) ->
          if event.data == YT.PlayerState.ENDED
            nextSong()

{'fraction_non_alphanumeric': 0.16434827408552294, 'fraction_numerical': 0.011506096513824489, 'mean_word_length': 2.64, 'pattern_counts': {'":': 1, '<': 6, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3799422', 'n_tokens_mistral': 1974, 'n_tokens_neox': 1860, 'n_words': 356}