def test_save_imports_duplicates(self): repo = Repo(autocommit=False) import_processor = ImportProcessor(repo=repo) artists = [{"name": "Eels"}, {"name": "Eels", "mbid": "some-mbid"}] artists_added = import_processor.save_imports(self.user.id, artists, "test") artist_import = repo.get_artist_import(self.user.id, "Eels") assert artists_added == 1 assert artist_import.import_name == "Eels"
def setUp(self): self.repo = Repo(autocommit=True) self.release_processor = ReleaseProcessor(repo=self.repo) self.user = UserFactory(email="*****@*****.**") self.artist = ArtistFactory( name="Tulsa", mbid="f123ef70-f563-43c2-b0e6-8f9afc0a38ad") self.user_artist = UserArtistFactory(user=self.user, artist=self.artist) self.repo.save(self.user, self.artist)
class TestTesting(BaseTestCase): def setUp(self): self.repo = Repo(autocommit=True) self.user = UserFactory(email="*****@*****.**") self.repo.save(self.user) def test_testing(self): imported = UserArtistImport(user_id=self.user.id, import_name="Nine Inch Nails") self.repo.save(imported) assert (UserArtistImport.query.filter_by( user_id=self.user.id).first().import_name == "Nine Inch Nails")
def user_processing(): """Perform quick check of user imports.""" repo = Repo() process_name = "user_processing" lock = Lock.query.filter_by(process_name=process_name).first() if lock is None: lock = Lock(process_name=process_name, lock_acquired=False) if lock.lock_acquired is False: lock.lock_acquired = True lock.date_acquired = utils.now() repo.save(lock) repo.commit() run_command() lock.lock_acquired = False repo.save(lock) repo.commit() else: numu_app.logger.info("Unable to achieve lock.")
def mb_processing(): """Handle all processing tasks related to musicbrainz.""" repo = Repo() process_name = "mb_processing" lock = Lock.query.filter_by(process_name=process_name).first() if lock is None: lock = Lock(process_name=process_name, lock_acquired=False) if lock.lock_acquired is False or ( lock.lock_acquired is True and lock.date_acquired < utils.now() - timedelta(hours=1)): lock.lock_acquired = True lock.date_acquired = utils.now() repo.save(lock) repo.commit() run_command() lock.lock_acquired = False repo.save(lock) repo.commit() else: numu_app.logger.info("Unable to achieve lock.")
def check_art(): """Check for artist and release art, and save to storage.""" repo = Repo() process_name = "check_art" lock = Lock.query.filter_by(process_name=process_name).first() if lock is None: lock = Lock(process_name=process_name, lock_acquired=False) if lock.lock_acquired is False or ( lock.lock_acquired is True and lock.date_acquired < utils.now() - timedelta(hours=1) ): lock.lock_acquired = True lock.date_acquired = utils.now() repo.save(lock) repo.commit() run_command() lock.lock_acquired = False repo.save(lock) repo.commit() else: numu_app.logger.info("Unable to achieve lock.")
def run_command(): repo = Repo() limit = 200 numu_app.logger.info("Starting MB process...") # Scan user imports imports_processed = ImportProcessor(repo=repo).import_user_artists( check_musicbrainz=True) # If imports have been taken care of, update artists and releases. if imports_processed == 0: # Scan artists date_offset = datetime.now() - timedelta(days=3) artists = (Artist.query.filter( or_(Artist.date_checked < date_offset, Artist.date_checked.is_(None))).order_by( Artist.date_checked.asc().nullsfirst()).limit(limit).all()) for artist in artists: numu_app.logger.info("Updating Artist: {} - Last check: {}".format( artist, artist.date_checked)) updated_artist = ArtistProcessor(repo=repo).update_artist(artist) if updated_artist: releases_added = ReleaseProcessor().add_releases( updated_artist) numu_app.logger.info( "Added Releases: {}".format(releases_added)) repo.commit() # Scan releases date_offset = datetime.now() - timedelta(days=14) releases = (Release.query.filter( Release.date_checked < date_offset).order_by( Release.date_checked.asc()).limit(limit).all()) for release in releases: numu_app.logger.info( "Updating Release: {} - Last check: {}".format( release, release.date_checked)) release = ReleaseProcessor(repo=repo).update_release(release) if release: # If release was deleted, it'll be None by the time we get here. release.date_checked = utils.now() repo.save(release) repo.commit()
def setUp(self): self.repo = Repo(autocommit=True) self.artist_processor = ArtistProcessor(repo=self.repo) self.import_processor = ImportProcessor(repo=self.repo) self.user = UserFactory(email="*****@*****.**") self.repo.save(self.user)
class TestUserArtists(BaseTestCase): def setUp(self): self.repo = Repo(autocommit=True) self.artist_processor = ArtistProcessor(repo=self.repo) self.import_processor = ImportProcessor(repo=self.repo) self.user = UserFactory(email="*****@*****.**") self.repo.save(self.user) # ------------------------------------ # Saving Imports # ------------------------------------ def test_save_imports(self): artists = [{"name": "Eels"}, {"name": "Nine Inch Nails"}, {"name": "Spoon"}] artists_added = self.import_processor.save_imports( self.user.id, artists, "test" ) artist_import = self.repo.get_artist_import(self.user.id, "Nine Inch Nails") assert artists_added == 3 assert artist_import.import_name == "Nine Inch Nails" def test_save_imports_duplicates(self): repo = Repo(autocommit=False) import_processor = ImportProcessor(repo=repo) artists = [{"name": "Eels"}, {"name": "Eels", "mbid": "some-mbid"}] artists_added = import_processor.save_imports(self.user.id, artists, "test") artist_import = repo.get_artist_import(self.user.id, "Eels") assert artists_added == 1 assert artist_import.import_name == "Eels" def test_save_imports_empty(self): artists = [] artists_added = self.import_processor.save_imports( self.user.id, artists, "test" ) assert artists_added == 0 def test_save_imports_existing(self): artist_import = ArtistImportFactory( user_id=self.user.id, import_name="Nine Inch Nails", import_mbid="b7ffd2af-418f-4be2-bdd1-22f8b48613da", ) self.repo.save(artist_import) artists = [{"name": "Nine Inch Nails"}] artists_added = self.import_processor.save_imports( self.user.id, artists, "test" ) assert artists_added == 0 # ------------------------------------ # Processing Imports # ------------------------------------ def test_import_user_artists_no_mb(self): artist = ArtistFactory( mbid="b7ffd2af-418f-4be2-bdd1-22f8b48613da", name="Nine Inch Nails" ) artist_import = ArtistImportFactory( user_id=self.user.id, import_name="Nine Inch Nails", import_mbid="b7ffd2af-418f-4be2-bdd1-22f8b48613da", ) self.repo.save(artist, artist_import) self.import_processor.import_user_artists( check_musicbrainz=False, user_id=self.user.id ) user_artist = self.repo.get_user_artist( user_id=self.user.id, mbid="b7ffd2af-418f-4be2-bdd1-22f8b48613da" ) assert user_artist.artist.name == "Nine Inch Nails" def test_import_user_artists_all_no_mb(self): artist = ArtistFactory( mbid="b7ffd2af-418f-4be2-bdd1-22f8b48613da", name="Nine Inch Nails" ) artist_import = ArtistImportFactory( user_id=self.user.id, import_name="Nine Inch Nails", import_mbid="b7ffd2af-418f-4be2-bdd1-22f8b48613da", ) self.repo.save(artist, artist_import) self.import_processor.import_user_artists(check_musicbrainz=False) user_artist = self.repo.get_user_artist( user_id=self.user.id, mbid="b7ffd2af-418f-4be2-bdd1-22f8b48613da" ) assert user_artist.artist.name == "Nine Inch Nails" def test_import_user_artists_all_no_mb_not_found(self): artist_import = ArtistImportFactory( user_id=self.user.id, import_name="Nine Inch Nails", import_mbid="b7ffd2af-418f-4be2-bdd1-22f8b48613da", ) self.repo.save(artist_import) self.import_processor.import_user_artists(check_musicbrainz=False) user_artist = self.repo.get_user_artist( user_id=self.user.id, mbid="b7ffd2af-418f-4be2-bdd1-22f8b48613da" ) assert user_artist is None def test_import_user_artists_with_mb(self): artist_import = ArtistImportFactory( user_id=self.user.id, import_name="Tulsa", import_mbid="f123ef70-f563-43c2-b0e6-8f9afc0a38ad", ) self.repo.save(artist_import) self.import_processor.import_user_artists( check_musicbrainz=True, user_id=self.user.id ) user_artist = self.repo.get_user_artist( user_id=self.user.id, mbid="f123ef70-f563-43c2-b0e6-8f9afc0a38ad" ) user_releases = self.repo.get_user_releases_for_artist( user=self.user, mbid="f123ef70-f563-43c2-b0e6-8f9afc0a38ad" ) assert user_artist.artist.name == "Tulsa" assert len(user_releases) > 0
def __init__(self, repo=None, mbz=None): self.repo = repo or Repo() self.mbz = mbz or musicbrainz self.logger = numu_app.logger self.artist_processor = ArtistProcessor(repo=self.repo, mbz=self.mbz)
class TestReleases(BaseTestCase): def setUp(self): self.repo = Repo(autocommit=True) self.release_processor = ReleaseProcessor(repo=self.repo) self.user = UserFactory(email="*****@*****.**") self.artist = ArtistFactory( name="Tulsa", mbid="f123ef70-f563-43c2-b0e6-8f9afc0a38ad") self.user_artist = UserArtistFactory(user=self.user, artist=self.artist) self.repo.save(self.user, self.artist) def test_add_release_from_mb(self): releases_added = self.release_processor.add_releases(self.artist) self.release_processor.update_user_releases(self.artist) user_releases = self.repo.get_user_releases_for_artist( self.user, self.artist.mbid) assert releases_added == 1 assert len(user_releases) == 1 def test_update_release_no_update(self): """When a release is updated with identical information, date_checked should update but not date_updated.""" self.release_processor.add_release( "dd5d8373-4ae3-3908-9235-c871e49ebd76") release = self.repo.get_release_by_mbid( "dd5d8373-4ae3-3908-9235-c871e49ebd76") updated_release = self.release_processor.update_release(release) assert updated_release.date_updated != updated_release.date_checked def test_update_release_with_mock(self): release = ReleaseFactory(artist_names=self.artist.name) release.artists.append(self.artist) user_release = UserReleaseFactory(user=self.user, release=release) self.repo.save(user_release) mb_release = { "first-release-date": "2015-01-01", "title": "Random Title", "artist-credit-phrase": "Tulsa", "primary-type": "Album", "artist-credit": [{ "artist": {} }], } self.release_processor._update_release(release, mb_release) release = self.repo.get_release_by_mbid(release.mbid) assert release.title == "Random Title" user_release = self.repo.get_user_release(self.user.id, release.mbid) assert user_release.release.title == "Random Title" def test_update_release_with_mock_delete(self): release = ReleaseFactory(artist_names=self.artist.name) release.artists.append(self.artist) self.repo.save(release) mb_release = { "first-release-date": "2015-01-01", "title": "Random Title", "artist-credit-phrase": "Tulsa", "primary-type": "Album", } self.release_processor._update_release(release, mb_release) release = self.repo.get_release_by_mbid(release.mbid) assert release is None def test_update_release_from_mb_with_followers_and_listeners(self): pass def test_delete_release_with_listeners(self): pass
def setUp(self): self.repo = Repo(autocommit=True) self.user = UserFactory(email="*****@*****.**") self.repo.save(self.user)
def import_numu_v2(): """ Import data from Numu API v2 Imports: - artists - listening history - filters """ user = g.user repo = Repo() import_processor = ImportProcessor() release_processor = ReleaseProcessor() username = user.email if user.email else user.icloud result = {} data = grab_json( "https://www.numutracker.com/v2/json2.php?importv2={}&key={}".format( username, numu_app.config.get("NUMU_V2_API_KEY") ) ) result["raw_data"] = data filters = data.get("filters") if filters: user.album = bool(filters["album"]) user.ep = bool(filters["ep"]) user.single = bool(filters["single"]) user.live = bool(filters["live"]) user.soundtrack = bool(filters["soundtrack"]) user.remix = bool(filters["remix"]) user.other = bool(filters["other"]) repo.save(user) repo.commit() artists = data.get("artists") if artists: imported = import_processor.save_imports(user.id, artists, "v2") result["artists_imported"] = imported listens = data.get("listens") if listens: releases_added = 0 for listen in listens: release_mbid = listen.get("mbid") release = release_processor.add_release(release_mbid) if release: user_release, notify = release_processor.add_user_release( release, user_id=user.id ) user_release.listened = True user_release.date_listened = listen.get("listen_date") repo.save(user_release) if notify: releases_added += 1 repo.commit() result["releases_added"] = releases_added return response.success(result)
def __init__(self, repo=None, mbz=None): self.repo = repo or Repo() self.mbz = mbz or musicbrainz self.logger = numu_app.logger
class TestArtists(BaseTestCase): def setUp(self): self.repo = Repo(autocommit=True) self.artist_processor = ArtistProcessor(repo=self.repo) self.user = UserFactory(email="*****@*****.**") self.repo.save(self.user) def test_add_artist_from_mb_by_name(self): artist = self.artist_processor.add_artist(name="Nine Inch Nails") assert artist.name == "Nine Inch Nails" def test_add_artist_blacklisted_mbid(self): artist = self.artist_processor.add_artist( mbid="125ec42a-7229-4250-afc5-e057484327fe") assert artist is None def test_add_artist_blacklisted_mbid_by_name(self): artist = self.artist_processor.add_artist(name="Chromalox") assert artist is None def test_add_artist_from_mb_by_mbid(self): artist = self.artist_processor.add_artist( mbid="b7ffd2af-418f-4be2-bdd1-22f8b48613da") assert artist.mbid == "b7ffd2af-418f-4be2-bdd1-22f8b48613da" def test_update_artist_data_with_followers(self): user_artist = UserArtistFactory(user=self.user) self.repo.save(user_artist) artist = user_artist.artist new_data = { "name": "Foodly Boofer", "sort-name": "Boofer Foodly", "disambiguation": "Well known boofer of foodlies", } self.artist_processor._update_artist_data(artist, new_data) artist = self.repo.get_artist_by_mbid(artist.mbid) assert artist.name == new_data["name"] assert artist.date_updated is not None def test_update_artist_from_mb_mocked(self): user_artist = UserArtistFactory(user=self.user) self.repo.save(user_artist) artist = user_artist.artist original_name = artist.name mb_result_mock = { "status": 200, "artist": { "id": artist.mbid, "name": "Brad Root", "sort-name": "Root Brad", "disambiguation": "Cool guy from Los Angeles", }, } updated_artist = self.artist_processor._update_artist( artist, mb_result_mock) assert updated_artist.name == "Brad Root" artist = self.repo.get_artist_by_name(original_name) assert artist is None user_artists = self.repo.get_user_artists_by_user_id(self.user.id) assert len(user_artists) == 1 def test_update_artist_from_mb_mocked_new_mbid(self): user_artist = UserArtistFactory(user=self.user) self.repo.save(user_artist) artist = user_artist.artist new_mbid = utils.uuid() mb_result_mock = { "status": 200, "artist": { "id": new_mbid, "name": artist.name, "sort-name": artist.sort_name, "disambiguation": artist.disambiguation, }, } updated_artist = self.artist_processor._update_artist( artist, mb_result_mock) assert updated_artist.name == artist.name assert updated_artist.mbid == new_mbid user_artists = self.repo.get_user_artists_by_user_id(self.user.id) assert len(user_artists) == 1 assert user_artists[0].mbid == new_mbid def test_update_artist_from_mb_mocked_removed(self): user_artist = UserArtistFactory(user=self.user) self.repo.save(user_artist) mb_result_mock = {"status": 404} updated_artist = self.artist_processor._update_artist( user_artist.artist, mb_result_mock) assert updated_artist is None user_artists = self.repo.get_user_artists_by_user_id(self.user.id) assert len(user_artists) == 0 def test_replace_artist_when_both_have_releases(self): artist = ArtistFactory() old_artist_mbid = artist.mbid release = ReleaseFactory() artist.releases.append(release) new_artist = ArtistFactory() new_artist.releases.append(release) self.repo.save(artist, new_artist) self.artist_processor.replace_artist(artist, new_artist) old_artist = self.repo.get_artist_by_mbid(old_artist_mbid) assert old_artist is None def test_replace_artist_with_followers(self): user_artist = UserArtistFactory(user=self.user) self.repo.save(user_artist) new_artist = ArtistFactory() self.artist_processor.replace_artist(user_artist.artist, new_artist) user_artists = self.repo.get_user_artists_by_user_id(self.user.id) assert len(user_artists) == 1 assert user_artists[0].mbid == new_artist.mbid def test_replace_artist_with_followers_and_listeners(self): user_artist = UserArtistFactory(user=self.user) artist = user_artist.artist release = ReleaseFactory(artist_names=artist.name, artists=[artist]) user_release = UserReleaseFactory(release=release, user=self.user) new_artist = ArtistFactory(name=artist.name) self.repo.save(user_release, new_artist) user_releases = self.repo.get_user_releases_for_artist( self.user, artist.mbid) assert len(user_releases) == 1 user_artist = self.repo.get_user_artist(self.user.id, artist.mbid) assert user_artist.artist.name == artist.name self.artist_processor.replace_artist(artist, new_artist) user_releases = self.repo.get_user_releases_for_artist( self.user, new_artist.mbid) assert len(user_releases) == 1 user_artist = self.repo.get_user_artist(self.user.id, new_artist.mbid) assert user_artist.artist.name == artist.name user_artists = self.repo.get_user_artists_by_user_id(self.user.id) assert len(user_artists) == 1 def test_replace_artist_with_followers_and_listeners_already_exists(self): user_artist = UserArtistFactory(user=self.user) artist = user_artist.artist release = ReleaseFactory(artist_names=artist.name, artists=[artist]) user_release = UserReleaseFactory(release=release, user=self.user) new_artist = ArtistFactory(name=artist.name) new_user_artist = UserArtistFactory(user=self.user, artist=new_artist) self.repo.save(user_release, new_user_artist) self.artist_processor.replace_artist(artist, new_artist) user_artist = self.repo.get_user_artist(self.user.id, new_artist.mbid) assert user_artist.artist.name == artist.name def test_delete_artist_with_followers(self): user_artist = UserArtistFactory(user=self.user) artist = user_artist.artist release = ReleaseFactory(artist_names=artist.name, artists=[artist]) user_release = UserReleaseFactory(release=release, user=self.user) new_artist = ArtistFactory(name=artist.name) self.repo.save(user_release, new_artist) artist_mbid = user_artist.artist.mbid self.artist_processor.delete_artist(user_artist.artist, "test deletion") artist_releases = self.repo.get_artist_releases(artist_mbid) assert len(artist_releases) == 0 user_artists = self.repo.get_user_artists_by_user_id(self.user.id) assert len(user_artists) == 0 artist = self.repo.get_artist_by_mbid(artist_mbid) assert artist is None user_artist_releases = self.repo.get_user_releases_for_artist( self.user, artist_mbid) assert user_artist_releases is None
from backend import utils from backend.models import Lock, User from backend.repo import Repo from backend.user_artists import ImportProcessor from backend.releases import ReleaseProcessor from numu import app as numu_app repo = Repo() import_processor = ImportProcessor(repo=repo) release_processor = ReleaseProcessor(repo=repo) @numu_app.cli.command() def import_user_data(): """Import user data from V2 automatically.""" process_name = "import_user_data" lock = Lock.query.filter_by(process_name=process_name).first() if lock is None: lock = Lock(process_name=process_name, lock_acquired=False) if lock.lock_acquired is False: lock.lock_acquired = True lock.date_acquired = utils.now() repo.save(lock) repo.commit() run_command() lock.lock_acquired = False repo.save(lock) repo.commit() else: