<commit_before>from __future__ import unicode_literals
import re

import spotipy.util as util
import youtube_dl

from spotify_dl.scaffold import *


def authenticate():
    """Authenticates you to Spotify
    """
    scope = 'user-library-read'
    username = ''
    return util.prompt_for_user_token(username, scope)


def fetch_tracks(sp, playlist, user_id):
    """Fetches tracks from Spotify user's saved
        tracks or from playlist(if playlist parameter is passed
        and saves song name and artist name to songs list
    """
    log.debug('Fetching saved tracks')
    offset = 0
    songs_dict = {}
    if user_id is None:
        current_user_id = sp.current_user()['id']
    else:
        current_user_id = user_id
    while True:
        if playlist is None:
            results = sp.current_user_saved_tracks(limit=50, offset=offset)
        else:
            results = sp.user_playlist_tracks(current_user_id, playlist, None,
                                              limit=50, offset=offset)

        log.debug('Got result json %s', results)
        for item in results['items']:
            track = item['track']

            if track is not None:
                track_name = str(track['name'])
                track_artist = str(track['artists'][0]['name'])
                log.debug('Appending %s to'
                        'songs list', (track['name'] + ' - ' + track['artists'][0]['name']))
                songs_dict.update({track_name: track_artist})
            else:
                log.warning("Track/artist name for %s not found, skipping", track)

            offset += 1

        if results.get('next') is None:
            log.info('All pages fetched, time to leave.'
                     ' Added %s songs in total', offset)
            break
    return songs_dict


def download_songs(info, download_directory, format_string, skip_mp3):
    """
    Downloads songs from the YouTube URL passed to either
       current directory or download_directory, is it is passed
    """
    for item in info:
        log.debug('Songs to download: %s', item)
        url_, track_, artist_ = item
        download_archive = download_directory + 'downloaded_songs.txt'
        outtmpl = download_directory + '%(title)s.%(ext)s'
        ydl_opts = {
            'format': format_string,
            'download_archive': download_archive,
            'outtmpl': outtmpl,
            'noplaylist': True,
            'postprocessor_args': ['-metadata', 'title=' + str(track_),
                                   '-metadata', 'artist=' + str(artist_)],
        }
        if not skip_mp3:
            mp3_postprocess_opts = {
                'key': 'FFmpegExtractAudio',
                'preferredcodec': 'mp3',
                'preferredquality': '192',<commit_msg>add track tagging with id3 (#84)<commit_after>from __future__ import unicode_literals
import re

import spotipy.util as util
import youtube_dl

from spotify_dl.scaffold import *


def authenticate():
    """Authenticates you to Spotify
    """
    scope = 'user-library-read'
    username = ''
    return util.prompt_for_user_token(username, scope)


def fetch_tracks(sp, playlist, user_id):
    """Fetches tracks from Spotify user's saved
        tracks or from playlist(if playlist parameter is passed
        and saves song name and artist name to songs list
    """
    log.debug('Fetching saved tracks')
    offset = 0
    songs_dict = {}
    if user_id is None:
        current_user_id = sp.current_user()['id']
    else:
        current_user_id = user_id
    while True:
        if playlist is None:
            results = sp.current_user_saved_tracks(limit=50, offset=offset)
        else:
            results = sp.user_playlist_tracks(current_user_id, playlist, None,
                                              limit=50, offset=offset)

        log.debug('Got result json %s', results)
        for item in results['items']:
            track = item['track']

            if track is not None:
                track_name = str(track['name'])
                track_artist = str(track['artists'][0]['name'])
                log.debug('Appending %s to'
                        'songs list', (track['name'] + ' - ' + track['artists'][0]['name']))
                songs_dict.update({track_name: track_artist})
            else:
                log.warning("Track/artist name for %s not found, skipping", track)

            offset += 1

        if results.get('next') is None:
            log.info('All pages fetched, time to leave.'
                     ' Added %s songs in total', offset)
            break
    return songs_dict


def download_songs(info, download_directory, format_string, skip_mp3):
    """
    Downloads songs from the YouTube URL passed to either
       current directory or download_directory, is it is passed
    """
    for number, item in enumerate(info):
        log.debug('Songs to download: %s', item)
        url_, track_, artist_ = item
        download_archive = download_directory + 'downloaded_songs.txt'
        outtmpl = download_directory + '%(title)s.%(ext)s'
        ydl_opts = {
            'format': format_string,
            'download_archive': download_archive,
            'outtmpl': outtmpl,
            'noplaylist': True,
            'postprocessor_args': ['-metadata', 'title=' + str(track_),
                                   '-metadata', 'artist=' + str(artist_),
                                   '-metadata', 'track=' + str(number + 1)]
        }
        if not skip_mp3:
            mp3_postprocess_opts = {
                'key': 'FFmpegExtractAudio',
                'preferredcodec': 'mp3',
                'preferredquality': '192',
{'fraction_non_alphanumeric': 0.10540207636811544, 'fraction_numerical': 0.005982755586837938, 'mean_word_length': 2.034703683929525, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 37, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4081550', 'n_tokens_mistral': 1643, 'n_tokens_neox': 1571, 'n_words': 433}