<commit_before>import ast
import logging
import shutil
import subprocess
import util

PLAYLIST_PATH = util.read_config('general')['playlist']


def load_csv():
    songs = dict()
    for l in read_playlist_lines():
        if not l.startswith('#'):
            parts = l.split(',')
            if 0 == len(parts):
                continue
            elif 1 == len(parts):
                played = False
            else:
                played = ast.literal_eval(parts[1].strip())
            songs[parts[0].replace('\n', '')] = played
    logging.info("Loaded %d songs, of which %d have been played" % (len(songs), sum(songs.values())))
    return songs


def read_playlist_without_newlines():
    """ This gets the CSV data, and handles the non-existence of the file """
    try:
        # open read/write, so that we can make sure no new lines are created
        with open(PLAYLIST_PATH, 'r') as f:
            # read the file -- make a list of lines with no empty ones (and line separators removed)
            f_data = "\n".join([x.strip() for x in f.readlines() if x.strip()])
    except (IOError, OSError):
        f_data = ''
    return f_data


def read_playlist_lines():
    return read_playlist_without_newlines().split('\n')


def modify_playlist_lines(callback):
    # edit in place instead of re-writing the file to preserve additional information
    lines = read_playlist_lines()

    # re-open the file otherwise the length of the file is different (len(False) vs. len(True))
    with open(PLAYLIST_PATH, 'w') as f:
        for line in lines:
            try:
                line = callback(line)
            except Exception as ex:
                logging.exception('Problem running callback on line: %s\n%s' % (line, ex))

            # make sure there is a new line
            line = "%s\n" % line.strip()

            # write the line
            f.write(line)


def playlist_line_has_been_played(line):
    try:
        return ast.literal_eval(line.split(',')[1])
    except:
        return False


def remove_commas_from_string(input_string):
    return str(input_string).translate(None, ',')


def get_title_from_youtube_url(url):
    try:
        output = str(subprocess.check_output('youtube-dl --get-title %s --no-warnings' % url, stderr=subprocess.STDOUT,
                                             shell=True)).strip()
    except subprocess.CalledProcessError as ex:
        output = str(ex.output).strip()
    except OSError as ex:
        output = 'youtube-dl not found: %s' % ex
    except Exception as ex:
        output = 'Something bad happened: %s' % ex
    return remove_commas_from_string(output)


def fix_playlist_song_titles():
    def callback(line):<commit_msg>Deprecated some functions<commit_after>import ast
import logging
import shutil
import subprocess
import util

PLAYLIST_PATH = util.read_config('general')['playlist']


@DeprecationWarning
def load_csv():
    songs = dict()
    for l in read_playlist_lines():
        if not l.startswith('#'):
            parts = l.split(',')
            if 0 == len(parts):
                continue
            elif 1 == len(parts):
                played = False
            else:
                played = ast.literal_eval(parts[1].strip())
            songs[parts[0].replace('\n', '')] = played
    logging.info("Loaded %d songs, of which %d have been played" % (len(songs), sum(songs.values())))
    return songs


@DeprecationWarning
def read_playlist_without_newlines():
    """ This gets the CSV data, and handles the non-existence of the file """
    try:
        # open read/write, so that we can make sure no new lines are created
        with open(PLAYLIST_PATH, 'r') as f:
            # read the file -- make a list of lines with no empty ones (and line separators removed)
            f_data = "\n".join([x.strip() for x in f.readlines() if x.strip()])
    except (IOError, OSError):
        f_data = ''
    return f_data


@DeprecationWarning
def read_playlist_lines():
    return read_playlist_without_newlines().split('\n')


@DeprecationWarning
def modify_playlist_lines(callback):
    # edit in place instead of re-writing the file to preserve additional information
    lines = read_playlist_lines()

    # re-open the file otherwise the length of the file is different (len(False) vs. len(True))
    with open(PLAYLIST_PATH, 'w') as f:
        for line in lines:
            try:
                line = callback(line)
            except Exception as ex:
                logging.exception('Problem running callback on line: %s\n%s' % (line, ex))

            # make sure there is a new line
            line = "%s\n" % line.strip()

            # write the line
            f.write(line)


@DeprecationWarning
def playlist_line_has_been_played(line):
    try:
        return ast.literal_eval(line.split(',')[1])
    except:
        return False


def remove_commas_from_string(input_string):
    return str(input_string).translate(None, ',')


def get_title_from_youtube_url(url):
    try:
        output = str(subprocess.check_output('youtube-dl --get-title %s --no-warnings' % url, stderr=subprocess.STDOUT,
                                             shell=True)).strip()
    except subprocess.CalledProcessError as ex:
        output = str(ex.output).strip()
    except OSError as ex:
        output = 'youtube-dl not found: %s' % ex
    except Exception as ex:
        output = 'Something bad happened: %s' % ex
    return remove_commas_from_string(output)


@DeprecationWarning
def fix_playlist_song_titles():
    def callback(line):
{'fraction_non_alphanumeric': 0.10806916426512968, 'fraction_numerical': 0.0018011527377521613, 'mean_word_length': 2.4598130841121497, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6170821', 'n_tokens_mistral': 1658, 'n_tokens_neox': 1612, 'n_words': 491}