<commit_before># This file is part of beets.
# Copyright 2013, <NAME>.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.

"""Gets genres for imported music based on Last.fm tags.

Uses a provided whitelist file to determine which tags are valid genres.
The genre whitelist can be specified like so in .beetsconfig:

    [lastgenre]
    whitelist=/path/to/genres.txt

The included (default) genre list was produced by scraping Wikipedia.
The scraper script used is available here:
https://gist.github.com/1241307
"""
import logging
import pylast
import os
import yaml

from beets import plugins
from beets import ui
from beets.util import normpath, plurality
from beets import config
from beets import library

log = logging.getLogger('beets')

LASTFM = pylast.LastFMNetwork(api_key=plugins.LASTFM_KEY)
C14N_TREE = os.path.join(os.path.dirname(__file__), 'genres-tree.yaml')

PYLAST_EXCEPTIONS = (
    pylast.WSError,
    pylast.MalformedResponseError,
    pylast.NetworkError,
)


# Core genre identification routine.

def _tags_for(obj):
    """Given a pylast entity (album or track), returns a list of
    tag names for that entity. Returns an empty list if the entity is
    not found or another error occurs.
    """
    try:
        if isinstance(el, pylast.Album):
            res = super(pylast.Album, obj).get_top_tags()
        else:
            res = obj.get_top_tags()
    except PYLAST_EXCEPTIONS as exc:
        log.debug(u'last.fm error: %s' % unicode(exc))
        return []

    tags = []
    multiple = config['lastgenre']['multiple'].get(bool)
    if multiple:
        min_weight = config['lastgenre']['min_weight'].get(int)
        max_genres = config['lastgenre']['max_genres'].get(int)
    else:
        min_weight = -1
        max_genres = 1

    for el in res:
        # pylast 0.5.x does not use Album.getTopTags, so we don't have a
        # weight for album tags.  However:  Album.getInfo (they use that)
        # returns only ~5 tags, so we use maximum weight for them
        if isinstance(el, pylast.TopItem):
            weight = int(el.weight)
            tag = el.item.get_name().lower()
        else:
            weight = 100
            tag = el.get_name().lower()
        if _is_allowed(tag):
          if min_weight > -1 and min_weight > weight and len(tags) > 0:
            return tags
          log.debug(u'lastfm.tag (min. {}): {} [{}]'.format(min_weight, tag, weight))
          tags.append(tag)<commit_msg>The way of the impatient is filled with useless commits :(<commit_after># This file is part of beets.
# Copyright 2013, <NAME>.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.

"""Gets genres for imported music based on Last.fm tags.

Uses a provided whitelist file to determine which tags are valid genres.
The genre whitelist can be specified like so in .beetsconfig:

    [lastgenre]
    whitelist=/path/to/genres.txt

The included (default) genre list was produced by scraping Wikipedia.
The scraper script used is available here:
https://gist.github.com/1241307
"""
import logging
import pylast
import os
import yaml

from beets import plugins
from beets import ui
from beets.util import normpath, plurality
from beets import config
from beets import library

log = logging.getLogger('beets')

LASTFM = pylast.LastFMNetwork(api_key=plugins.LASTFM_KEY)
C14N_TREE = os.path.join(os.path.dirname(__file__), 'genres-tree.yaml')

PYLAST_EXCEPTIONS = (
    pylast.WSError,
    pylast.MalformedResponseError,
    pylast.NetworkError,
)


# Core genre identification routine.

def _tags_for(obj):
    """Given a pylast entity (album or track), returns a list of
    tag names for that entity. Returns an empty list if the entity is
    not found or another error occurs.
    """
    try:
        if isinstance(obj, pylast.Album):
            res = super(pylast.Album, obj).get_top_tags()
        else:
            res = obj.get_top_tags()
    except PYLAST_EXCEPTIONS as exc:
        log.debug(u'last.fm error: %s' % unicode(exc))
        return []

    tags = []
    multiple = config['lastgenre']['multiple'].get(bool)
    if multiple:
        min_weight = config['lastgenre']['min_weight'].get(int)
        max_genres = config['lastgenre']['max_genres'].get(int)
    else:
        min_weight = -1
        max_genres = 1

    for el in res:
        # pylast 0.5.x does not use Album.getTopTags, so we don't have a
        # weight for album tags.  However:  Album.getInfo (they use that)
        # returns only ~5 tags, so we use maximum weight for them
        if isinstance(el, pylast.TopItem):
            weight = int(el.weight)
            tag = el.item.get_name().lower()
        else:
            weight = 100
            tag = el.get_name().lower()
        if _is_allowed(tag):
          if min_weight > -1 and min_weight > weight and len(tags) > 0:
            return tags
          log.debug(u'lastfm.tag (min. {}): {} [{}]'.format(min_weight, tag, weight))
          tags.append(tag)
{'fraction_non_alphanumeric': 0.09254327563249001, 'fraction_numerical': 0.007656458055925432, 'mean_word_length': 3.0738983050847457, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3079521', 'n_tokens_mistral': 1832, 'n_tokens_neox': 1733, 'n_words': 653}