Skip to content

Commit

Permalink
initial caching system
Browse files Browse the repository at this point in the history
  • Loading branch information
antidiestro committed Aug 19, 2014
1 parent 923bec4 commit 19e2769
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 49 deletions.
1 change: 1 addition & 0 deletions lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Votes = new Meteor.Collection("votes");
Cache = {};
Cache.Spotify = new Meteor.Collection("cache_spotify");
Cache.YouTube = new Meteor.Collection("cache_youtube");
Cache.Freebase = new Meteor.Collection("cache_freebase");

// Dependencies for YouTube player
youtubeApiReady = false;
Expand Down
90 changes: 62 additions & 28 deletions lib/sky_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@ Sky.api = {
},
videoInfo: function(video_id){
Sky.log('api,youtube', 'Retrieving info for ID '+video_id);
var videoInfo = Meteor.http.get('https://www.googleapis.com/youtube/v3/videos?part=id%2Csnippet%2CcontentDetails%2CtopicDetails&id='+video_id+'&maxResults=1&key=AIzaSyCjkQ_YauVPcAHM541qjYVtWOX7kjYFSlE');
return videoInfo.data.items[0];

var cacheCheck = Cache.YouTube.findOne({youtube_id: video_id});

if ( cacheCheck ) {
return cacheCheck.cacheData;
} else {
var videoInfo = Meteor.http.get('https://www.googleapis.com/youtube/v3/videos?part=id%2Csnippet%2CcontentDetails%2CtopicDetails&id='+video_id+'&maxResults=1&key=AIzaSyCjkQ_YauVPcAHM541qjYVtWOX7kjYFSlE');
Cache.YouTube.insert({youtube_id: video_id, cacheData: videoInfo.data.items[0]});
return videoInfo.data.items[0];
}
},
similarVideos: function(video_id){
Sky.log('api,youtube', 'Retrieving similar videos for ID '+video_id);
Expand Down Expand Up @@ -43,45 +51,71 @@ Sky.api = {
return Meteor.http.get('https://api.spotify.com/v1/search?q=track:'+encodeURIComponent(track)+'%20artist:'+encodeURIComponent(artist)+'&type=track&limit=1').data;
},
checkVideoForMusic: function(doc){
if ( doc.youtube_info.topicDetails ) {
if ( doc.youtube_info.topicDetails.topicIds ) {
doc.youtube_info.topicDetails.topicIds.forEach(function(topic, i){
if ( Sky.api.freebase.checkForRecording(topic) ) {
var topicInfo = Sky.api.freebase.topicInfo(topic);
var trackName = topicInfo.data.property['/type/object/name'].values[0].text;
var artistName = topicInfo.data.property['/music/recording/artist'].values[0].text;

var spotifySearch = Sky.api.spotify.findWithArtist(trackName, artistName);
// Check for cached YouTube object
var trackInfo;
var cacheCheck = Cache.YouTube.findOne({youtube_id: doc.youtube_id});

if ( spotifySearch.tracks.items.length > 0 ) {
var trackInfo = spotifySearch.tracks.items[0];
var cleanVideoTitle = cleanVideoName(doc.youtube_info.snippet.title, trackInfo.name, trackInfo.artists[0].name);
// If cached object has a Spotify ID associated to it, go that way.
if ( cacheCheck && cacheCheck.spotify_id ) {
var spotifyTrackCache = Cache.Spotify.findOne({spotify_id: cacheCheck.spotify_id});
trackInfo = spotifyTrackCache.cacheData;
} else {
if ( doc.youtube_info.topicDetails ) {
if ( doc.youtube_info.topicDetails.topicIds ) {
doc.youtube_info.topicDetails.topicIds.forEach(function(topic, i){
if ( Sky.api.freebase.checkForRecording(topic) ) {
var topicInfo = Sky.api.freebase.topicInfo(topic);
var trackName = topicInfo.data.property['/type/object/name'].values[0].text;
var artistName = topicInfo.data.property['/music/recording/artist'].values[0].text;

var spotifySearch = Sky.api.spotify.findWithArtist(trackName, artistName);

var dataToUpdate = {
title: trackInfo.name,
artist_name: trackInfo.artists[0].name,
subtitle: cleanVideoTitle,
image_url: trackInfo.album.images[1].url,
type: 'track',
spotify_id: trackInfo.id,
spotify_artist_id: trackInfo.artists[0].id
}
if ( spotifySearch.tracks.items.length > 0 ) {
trackInfo = spotifySearch.tracks.items[0];

var spotifyTrackCache = Cache.Spotify.findOne({spotify_id: trackInfo.id});

if ( !spotifyTrackCache ) {
Cache.Spotify.insert({spotify_id: trackInfo.id, cacheData: trackInfo});
}

Videos.update({_id: doc._id}, { $set: dataToUpdate });
Cache.YouTube.update({youtube_id: doc.youtube_id}, { $set: { spotify_id: trackInfo.id } });
}
}
}
});
});
}
}
}

Videos.update({_id: doc._id}, { $set: { isAnalyzed: true } });
var cleanVideoTitle = cleanVideoName(doc.youtube_info.snippet.title, trackInfo.name, trackInfo.artists[0].name);

var dataToUpdate = {
title: trackInfo.name,
artist_name: trackInfo.artists[0].name,
subtitle: cleanVideoTitle,
image_url: trackInfo.album.images[1].url,
type: 'track',
spotify_id: trackInfo.id,
spotify_artist_id: trackInfo.artists[0].id,
isAnalyzed: true
}

Videos.update({_id: doc._id}, { $set: dataToUpdate });

Sky.generateRecommendations(doc._id);
}
},
freebase: {
topicInfo: function(topic){
return Meteor.http.get('https://www.googleapis.com/freebase/v1/topic'+topic);
var cacheCheck = Cache.Freebase.findOne({topic_id: topic});

if ( cacheCheck ) {
return cacheCheck.cacheData;
} else {
var topicInfo = Meteor.http.get('https://www.googleapis.com/freebase/v1/topic'+topic);
Cache.Freebase.insert({topic_id: topic, cacheData: topicInfo});
return topicInfo;
}
},
checkForRecording: function(topic){
var checkTopic = Meteor.http.get('https://www.googleapis.com/freebase/v1/search?query='+topic+'&filter=(all%20type:/music/recording)').data;
Expand Down
58 changes: 37 additions & 21 deletions server/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,29 +118,45 @@ Meteor.methods({
}
},
insertSpotifySong: function(track, room_id){
var query = track.name+' '+track.artist_name;
var youtubeSearch = Sky.api.youTube.search(query, 1);
if ( youtubeSearch.items.length > 0 ) {
var videoInfo = Sky.api.youTube.videoInfo(youtubeSearch.items[0].id.videoId);
var data = {
room_id: room_id,
youtube_id: videoInfo.id,
title: track.name,
artist_name: track.artist_name,
duration: moment.duration(videoInfo.contentDetails.duration).asSeconds(),
type: 'track',
source: 'spotify',
spotify_id: track.id,
spotify_artist_id: track.artists[0].id,
isAnalyzed: true
};
if ( track.image_url ) {
data.image_url = track.image_url;
}
var thisVideo = Videos.insert(data);
var videoInfo;
var cacheCheck = Cache.Spotify.findOne({spotify_id: track.id});

if ( cacheCheck && cacheCheck.youtube_id ) {
videoInfo = Sky.api.youTube.videoInfo(cacheCheck.youtube_id);
} else {
console.log('Video not found.')
var query = track.name+' '+track.artist_name;
var youtubeSearch = Sky.api.youTube.search(query, 1);
if ( youtubeSearch.items.length > 0 ) {
videoInfo = Sky.api.youTube.videoInfo(youtubeSearch.items[0].id.videoId);
if ( cacheCheck ) {
Cache.Spotify.update({_id: cacheCheck._id}, { $set: { youtube_id: youtubeSearch.items[0].id.videoId } });
} else {
Cache.Spotify.insert({spotify_id: track.id, youtube_id: youtubeSearch.items[0].id.videoId, cacheData: track});
}
} else {
console.log('Video not found.');
return;
}
}

var data = {
room_id: room_id,
youtube_id: videoInfo.id,
title: track.name,
artist_name: track.artist_name,
duration: moment.duration(videoInfo.contentDetails.duration).asSeconds(),
type: 'track',
source: 'spotify',
spotify_id: track.id,
spotify_artist_id: track.artists[0].id,
isAnalyzed: true
};

if ( track.image_url ) {
data.image_url = track.image_url;
}

var thisVideo = Videos.insert(data);
},
insertYouTubeVideo: function(video, room_id){
var videoInfo = Sky.api.youTube.videoInfo(video.id.videoId);
Expand Down

0 comments on commit 19e2769

Please sign in to comment.