Skip to content

Commit

Permalink
Do not parse tags when date < lastUpdate
Browse files Browse the repository at this point in the history
  • Loading branch information
leeroybrun committed Dec 11, 2014
1 parent db817ca commit f2e0167
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
30 changes: 19 additions & 11 deletions src/background/ShazamService.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
$.get('https://www.shazam.com/myshazam/download-history')
.done(function(data) {
if(data) {
Logger.info('[Shazam] Parsing tags...');

Shazam.parseTags(lastUpdate, data, callback);
} else {
Logger.error('[Shazam] Cannot download Shazam history.');
Expand All @@ -50,31 +48,41 @@

// Parse tags from tags history
parseTags: function(lastUpdate, data, callback) {
Logger.info('[Shazam] Parsing tags...');

var tags = [];
var stopParsing = false;
var tagsEl = $(data).find('tr');

Logger.info('[Shazam] Start parsing of '+ tagsEl.length +' elements...');

$(data).find('tr').each(function() {
if($('td', this).length === 0) {
return;
for(var i = 0; i < tagsEl.length && stopParsing === false; i++) {
if($('td', tagsEl[i]).length === 0) {
continue;
}

var date = new Date($('td.time', this).text());
var date = new Date($('td.time', tagsEl[i]).text());

if(date > lastUpdate) {
var idMatch = (new RegExp('t([0-9]+)', 'g')).exec($('td:nth-child(1) a', this).attr('href'));
var idMatch = (new RegExp('t([0-9]+)', 'g')).exec($('td:nth-child(1) a', tagsEl[i]).attr('href'));
if(!idMatch) {
return;
continue;
}

var tag = {
shazamId: idMatch[1],
name: $('td:nth-child(1) a', this).text().trim(),
artist: $('td:nth-child(2)', this).text().trim(),
name: $('td:nth-child(1) a', tagsEl[i]).text().trim(),
artist: $('td:nth-child(2)', tagsEl[i]).text().trim(),
date: date
};

tags.push(tag);
} else {
// Tag's date is lower than last update date = the following tags were already fetched in previous updates
Logger.info('[Shazam] Stop parsing, we reached the last tag not already fetched.');
stopParsing = true;
}
});
}

callback(null, tags);
}
Expand Down
10 changes: 9 additions & 1 deletion src/background/TagsService.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,13 @@
Logger.info('[Tags] Updating since '+ Tags.lastUpdate +'.');

Shazam.getTags(Tags.lastUpdate, function(err, tags) {
if(!err) {
if(!err && Array.isArray(tags)) {
Logger.info('[Tags] Got '+ tags.length +' tags from Shazam.');

if(tags.length === 0) {
return callback();
}

Tags.lastUpdate = new Date();

async.eachSeries(tags, function(tag, cbe) {
Expand All @@ -72,6 +78,8 @@
}, function() {
Tags.updatePlaylist(callback);
});
} else {
callback(err);
}
});
},
Expand Down

0 comments on commit f2e0167

Please sign in to comment.