<commit_before>(function(StorageHelper, ChromeHelper, Tags, Logger, Spotify){
	var Shazam = {
		newTags: [],

		data: new StorageHelper('Shazam'),

		openLogin: function() {
			Logger.info('[Shazam] Opening login page...');
			ChromeHelper.focusOrCreateTab('https://www.shazam.com/myshazam');
		},

		loginStatus: function(callback) {
			$.get('https://www.shazam.com/fragment/myshazam')
				.done(function() {
					Logger.info('[Shazam] login status : logged.');
					callback(true);
				})
				.fail(function(jqXHR, textStatus) {
					if(jqXHR.status === 401) {
						Logger.info('[Shazam] login status : not logged (401).');
						Logger.error(textStatus);
						callback(false);
					} else {
						Logger.info('[Shazam] login status : logged.');
						callback(true);
					}
				});
		},

		updateTags: function(path, callback) {
			if(typeof callback === 'undefined' && typeof path === 'function') {
				callback = path;
				path = null;
			}

			callback = callback || function(){};
			path = path || '/fragment/myshazam';

			function saveTags(error) {
				if(error) {
					Tags.save(function() {
						callback(error);
					});
				} else {
					Shazam.data.set({'lastTagsUpdate': (new Date()).toString()}, function() {
						Tags.save(function() {
							callback();
						});
					});
				}
			}

			Shazam.data.get('lastTagsUpdate', function(items) {
				var lastTagsUpdate = new Date(items.lastTagsUpdate) || new Date(0);
				lastTagsUpdate = (!isNaN(lastTagsUpdate.valueOf())) ? lastTagsUpdate : new Date(0);

				Logger.info('[Shazam] Updating tags since '+ lastTagsUpdate +'.');

				$.get('https://www.shazam.com'+ path)
					.done(function(data) {
						if(data && typeof data === 'object' && data.feed.indexOf('ms-no-tags') == -1) {
							var lastTagDate = new Date(parseInt($('<div/>').append(data.feed).find('article').last().find('.tl-date').attr('data-time')));

							Logger.info('[Shazam] Parsing tags for '+ path +'.');

							Shazam.parseTags(lastTagsUpdate, data.feed, function() {
								if(data.previous && data.feed.indexOf('ms-no-tags') == -1 && lastTagDate > lastTagsUpdate) {
									Logger.info('[Shazam] Tags parsed. Waiting 2s before next page...');
									setTimeout(function() {
										Shazam.updateTags(data.previous, callback);
									}, 2000);
								} else {
									Logger.info('[Shazam] All tags fetched.');
									saveTags(false);
								}
							});
						} else {
							Logger.info('[Shazam] All tags fetched.');
							saveTags(false);
						}
					})
					.fail(function(jqXHR, textStatus) {
						Logger.info('[Shazam] Tags fetch error : '+textStatus+'.');
						if(jqXHR.status === 401) {
							saveTags(true);
						} else {
							saveTags(false);
						}
					});
			});
		},

		parseTags: function(lastTagsUpdate, data, callback) {
			$('<div/>').append(data).find('article').each(function() {
				var date = parseInt($('.tl-date', this).attr('data-time'));

				if(new Date(date) > lastTagsUpdate) {
					var tag = {
						id: $(this).attr('data-track-id'),
						name: $('[data-track-title]', this).text().trim(),
						artist: $('[data-track-artist]', this).text().trim().replace(/^by /, ''),
						date: date,
						image: $('img[itemprop="image"]', this).attr('src')
					};

					tag.query = Spotify.genQuery(tag.name, tag.artist);

					Tags.add(tag);
				}
			});

			callback();
		}
	};

	window.s2s.Shazam = Shazam;
})(window.s2s.StorageHelper, window.s2s.ChromeHelper, window.s2s.Tags, window.s2s.Logger, window.s2s.Spotify);<commit_msg>ShazamService rewritten - Now use Download History from MyShazam -> one download -> all tags - No tags logic in ShazamService, will be consumed by TagsService instead<commit_after>(function(ChromeHelper, Logger){
	var Shazam = {
		openLogin: function() {
			Logger.info('[Shazam] Opening login page...');
			ChromeHelper.focusOrCreateTab('https://www.shazam.com/myshazam');
		},

		loginStatus: function(callback) {
			$.get('https://www.shazam.com/fragment/myshazam')
				.done(function() {
					Logger.info('[Shazam] login status : logged.');
					callback(true);
				})
				.fail(function(jqXHR, textStatus) {
					if(jqXHR.status === 401) {
						Logger.info('[Shazam] login status : not logged (401).');
						Logger.error(textStatus);
						callback(false);
					} else {
						Logger.info('[Shazam] login status : logged.');
						callback(true);
					}
				});
		},

		getTags: function(lastUpdate, callback) {
			Logger.info('[Shazam] Downloading tags history...');
			$.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.');
						Logger.error('[Shazam] Data returned : "'+ data +'"');

						return callback(new Error('Cannot download Shazam history.'));
					}
				})
				.fail(function(jqXHR, textStatus) {
					Logger.info('[Shazam] Tags fetch error : '+textStatus+'.');

					return callback(new Error('Tags fetch error : '+textStatus));
				});
		},

		parseTags: function(lastUpdate, data, callback) {
			var tags = [];

			$(data).find('tr').each(function() {
				if($('td', this).length == 0) {
					return;
				}

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

				if(date > lastUpdate) {
					var tag = {
						name: $('td:nth-child(1) a', this).text().trim(),
						artist: $('td:nth-child(2)', this).text().trim(),
						date: date
					};

					tags.push(tag);
				}
			});

			callback(null, tags);
		}
		
	};

	window.s2s.Shazam = Shazam;
})(window.s2s.ChromeHelper, window.s2s.Logger);
{'fraction_non_alphanumeric': 0.18912237330037082, 'fraction_numerical': 0.006357054564718347, 'mean_word_length': 2.91701244813278, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 10, 'https://': 6, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1859575', 'n_tokens_mistral': 2508, 'n_tokens_neox': 1888, 'n_words': 336}