Skip to content
This repository has been archived by the owner on May 27, 2020. It is now read-only.

Commit

Permalink
Shows youtube videos of the tracks from last.fm.
Browse files Browse the repository at this point in the history
  • Loading branch information
riopro committed Nov 19, 2010
1 parent 4ed6e9c commit 4577684
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ source "http://rubygems.org"
gem 'heroku', :group => :development
gem 'sinatra'
gem 'crack'
gem 'youtube-g'
7 changes: 7 additions & 0 deletions quick_tunes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
require 'rubygems'
require 'sinatra'
require 'crack/xml'
require 'youtube_g'

LASTFM_API_KEY = "2eda7af022a1e005cb1d8f3b7583b612"

Expand All @@ -25,8 +26,14 @@
get '/' do
@artist = params[:artist].to_s.downcase
unless @artist == ''
@results = []
hash = top_tracks_hash(top_tracks_xml(@artist))
@tracks = top_tracks(hash, 5)
@tracks.each do |track|
youtube_search = YouTubeG::Client.new.videos_by(:query => track.to_s.downcase, :page => 1, :per_page => 1)
video = youtube_search.videos.first
@results << {:artist => @artist, :track => track, :video => video}
end
end
erb :index
end
Expand Down
23 changes: 15 additions & 8 deletions views/index.erb
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
<div id="search_box">
<form action="/" id="search">
<label for="artist">Artist:</label>
<form action="/" id="search_form">
<label for="artist">Artist or band:</label>
<input type="text" name="artist" id="artist" value="<%= params[:artist] %>"/>
<input type="submit" value="Search">
<input type="submit" value="Search" id="search_form_submit">
</form>
</div>

<% unless @artist == '' %>
<div id="results_box">
<p>Top 5 tracks from <a href="http://www.last.fm/music/<%= @artist %>"><%= @artist %></a> according to <a href="http://last.fm">Last.fm</a>:</p>
<ul>
<% @tracks.each do |track| %>
<p>Top 5 tracks from <a href="http://www.last.fm/music/<%= @artist %>"><%= @artist %></a> according to <a href="http://last.fm">Last.fm</a>:</p>
<ol>
<% @results.each do |result| %>

<li>
<a href="http://www.youtube.com/results?search_query=<%= CGI::escape(track).to_s %>&aq=f"><%= track %></a>
<p><%= result[:track] %></p>
<% if result[:video].nil? %>
<p>No video found.</p>
<% else %>
<p><%= result[:video].embed_html %></p>
<% end %>
<p><a href="http://www.youtube.com/results?search_query=<%= CGI::escape(result[:track]).to_s.downcase %>&aq=f">other videos</a></p>
</li>
<% end %>
</ul>
</ol>
</div>
<% end %>
10 changes: 10 additions & 0 deletions views/layout.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
<title>Quick Tunes</title>
</head>

<body>
<h1><a href="/">Quick Tunes</a></h1>
<p>Search for any artist or band and get videos from their top 5 tracks:</p>
<%= yield %>
<p>Powered by <a href="http://pittlandia.net">PiTTlândia</a>. Source-code up for grabs at
<a href="https://github.com/rtopitt/quick_tunes">GitHub</a>.</p>
<script type="text/javascript">
$('#search_form').submit(function(){
$('input[type=submit]', this).attr('value', 'Searching...');
$('input[type=submit]', this).attr('disabled', 'disabled');
});
</script>
</body>
</html>

0 comments on commit 4577684

Please sign in to comment.