class UserFactory(factory.alchemy.SQLAlchemyModelFactory): class Meta: model = UserModel sqlalchemy_session = db.session id = factory.Sequence(lambda n: n + 1) email = factory.fuzzy.FuzzyText(length=20, suffix="@example.com") icloud = None password_hash = b"" date_joined = utils.now() date_last_activity = utils.now() album = True single = True ep = True live = False soundtrack = False remix = False other = False
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.")
class ArtistImportFactory(factory.alchemy.SQLAlchemyModelFactory): class Meta: model = UserArtistImportModel sqlalchemy_session = db.session user_id = 0 import_name = factory.Faker("name") import_mbid = factory.fuzzy.FuzzyAttribute(lambda: utils.uuid()) import_method = "factory" found_mbid = None date_added = utils.now() date_checked = None
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.")
class UserArtistFactory(factory.alchemy.SQLAlchemyModelFactory): class Meta: model = UserArtistModel sqlalchemy_session = db.session user_id = factory.SelfAttribute("user.id") mbid = factory.SelfAttribute("artist.mbid") date_followed = utils.now() follow_method = factory.fuzzy.FuzzyChoice( ["V2", "apple", "spotify", "lastfm"]) following = True user = factory.SubFactory(UserFactory) artist = factory.SubFactory(ArtistFactory)
class ArtistFactory(factory.alchemy.SQLAlchemyModelFactory): class Meta: model = ArtistModel sqlalchemy_session = db.session mbid = factory.fuzzy.FuzzyAttribute(lambda: utils.uuid()) name = factory.Faker("name") sort_name = factory.SelfAttribute("name") disambiguation = factory.fuzzy.FuzzyText(length=12) art = False date_added = utils.now() date_art_check = None date_checked = None date_updated = None links = {}
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 run_command(): limit = 100 users = (User.query.order_by( User.date_v2_import.asc().nullsfirst()).limit(limit).all()) for user in users: username = user.email if user.email else user.icloud data = utils.grab_json( "https://www.numutracker.com/v2/json2.php?importv2={}&key={}". format(username, numu_app.config.get("NUMU_V2_API_KEY"))) 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) artists = data.get("artists") if artists: imported = import_processor.save_imports(user.id, artists, "v2") print("Imported {} artists from V2...".format(imported)) listens = data.get("listens", []) releases_added = 0 for listen in listens: release_mbid = listen.get("mbid") release = repo.get_release_by_mbid(release_mbid) if release: user_release, notify = release_processor.add_user_release( release, user_id=user.id, follow_method="v2") user_release.listened = True user_release.date_listened = listen.get("listen_date") repo.save(user_release) if notify: releases_added += 1 print("Listens imported: {}".format(releases_added)) user.date_v2_import = utils.now() repo.save(user) repo.commit()
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: numu_app.logger.info("Unable to achieve lock.")
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.")