Beispiel #1
0
def update_db_for_itunes_data(itunes_data, db_field):
    db_tracks = session.query(SavedTrack).filter(SavedTrack.path.in_([i['file_path'] for i in itunes_data])).all()
    itunes_key = [key for key in itunes_data[0].keys() if key != 'file_path'][0]  # gross
    for db_track in db_tracks:
        db_track.__setattr__(db_field, [track[itunes_key] for track in itunes_data if track['file_path'] ==
                                        db_track.path][0])
        session.add(db_track)

    session.commit()
Beispiel #2
0
 def acquire_track_model(self):
     # determine if fingerprint present, if not generate
     if not self.fingerprint:
         self.query_fingerprint()
     # use fingerprint to query model
     self.model = session.query(SavedTrack).get(self.fingerprint)
     # if 0 results, create model
     if not self.model:
         LOGGER.info('Track not found in database; creating...')
         self.model = SavedTrack()
Beispiel #3
0
def run():
    # TODO: break this into smaller functions
    LOGGER.info('Running music downloader...')
    tracks_to_download = get_tracks_to_download()
    if not tracks_to_download:
        LOGGER.info('No queued tracks found in database')
        return
    LOGGER.info('Found {} tracks from database to download...'.format(len(tracks_to_download)))
    options = {
        'format': 'bestaudio/best',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
        }],
        'quiet': False}

    for queued_track in tracks_to_download:
        exists = session.query(SavedTrack).filter(SavedTrack.artist == queued_track.artist,
                                                  SavedTrack.title == queued_track.title).count() > 0
        if exists:
            LOGGER.info('Track already exists as Saved Track, deleting Queued track and skipping download.')
            session.delete(queued_track)
            session.commit()
            continue
        track_save_name = u'{} - {}'.format(queued_track.artist, queued_track.title)
        final_track_path = TRACK_DIRECTORY + track_save_name + '.mp3'
        holding_track_path = HOLD_DIRECTORY + track_save_name + '.mp3'
        LOGGER.info('Downloading track: {}'.format(track_save_name))
        options['outtmpl'] = u'{}/{}.%(ext)s'.format(HOLD_DIRECTORY, track_save_name)
        ydl = youtube_dl.YoutubeDL(options)
        download_link = build_download_link(queued_track.youtube_video_id)
        # download the track
        try:
            ydl.download([download_link])
        except youtube_dl.utils.DownloadError as e:
            LOGGER.warning('youtube-dl encountered an error: {}' .format(e.message))
            continue
        saved_track = SavedTrack()
        saved_track.update_from_dict(queued_track.as_dict())
        saved_track.path = final_track_path
        saved_track.md5 = calculate_md5(holding_track_path)
        fingerprint_duration = fingerprint_file(holding_track_path, 30)
        saved_track.fingerprint = fingerprint_duration[1]
        saved_track.duration = fingerprint_duration[0]

        session.merge(saved_track)
        session.delete(queued_track)
        session.commit()
        os.rename(holding_track_path, final_track_path)
        LOGGER.info('Complete. Downloaded track data committed to database.')
Beispiel #4
0
    def match_meta_genre(self, genre):
        if not genre:
            return
        genre_mappings = [row.as_dict() for row in session.query(Genre).all()]
        prioritized_meta_genres = ['indie', 'electronic']

        cleaned_genre = clean_genre(genre)

        for prioritized_meta_genre in prioritized_meta_genres:
            for genre_mapping in [genre_mapping for genre_mapping in genre_mappings if genre_mapping['meta_genre'] ==
                    prioritized_meta_genre]:
                if clean_genre(genre_mapping['genre']) in cleaned_genre:
                    return genre_mapping['meta_genre'].title()

        for genre_mapping in [genre_mapping for genre_mapping in genre_mappings if genre_mapping['meta_genre'] not in
                prioritized_meta_genres]:
            if clean_genre(genre_mapping['genre']) in cleaned_genre:
                return genre_mapping['meta_genre'].title()
def run():
    tracks_to_search_for = session.query(QueuedTrack).filter(QueuedTrack.youtube_video_id == None).all()
    tracks_to_search_for = [track.as_dict() for track in tracks_to_search_for]

    try:
        for track in tracks_to_search_for:
            query = track['artist'] + ' ' + track['title']
            params['q'] = query

            resp = requests.get(url=base_url, params=params, verify=False)
            json_response = resp.json()
            if json_response['items']:
                track.update(get_best_result(resp.json(), query))
                track.pop('similarity_score')
                session.merge(QueuedTrack(**track))
    except:
        raise

    finally:
        session.commit()
Beispiel #6
0
def get_tracks_to_download():
    """Returns ORM for tracks that have no youtube_video_id"""
    tracks_to_download = session.query(QueuedTrack).filter(QueuedTrack.youtube_video_id != None).all()

    return tracks_to_download