<commit_before>from __future__ import unicode_literals

import re

from mopidy.models import Ref

# TOOD: replace with v2 api...
import pycountry


def unparse_uri(variant, identifier):
    return b'dirble:%s:%s' % (variant, identifier)


def parse_uri(uri):
    result = re.findall(r'^dirble:([a-z]+)(?::(\d+|[a-z]{2}))?$', uri)
    if result:
        return result[0]
    return None, None


def station_to_ref(station):
    name = station.get('name').strip()  # TODO: fallback to streams URI?
    country = station.get('country', '??')
    uri = unparse_uri('station', station['id'])
    return Ref.track(uri=uri, name='%s - %s' % (country, name))


def category_to_ref(category):
    uri = unparse_uri('category', category['id'])
    return Ref.directory(uri=uri, name=category.get('title', uri))


def country_to_ref(country_code):
    uri = unparse_uri('country', country_code.lower())
    try:
        country = pycountry.countries.get(alpha2=country_code.upper())
        return Ref.directory(uri=uri, name=country.name)
    except KeyError:
        return Ref.directory(uri=uri, name=country_code.upper())<commit_msg>Note why we still use pycountries<commit_after>from __future__ import unicode_literals

import re

from mopidy.models import Ref

import pycountry


def unparse_uri(variant, identifier):
    return b'dirble:%s:%s' % (variant, identifier)


def parse_uri(uri):
    result = re.findall(r'^dirble:([a-z]+)(?::(\d+|[a-z]{2}))?$', uri)
    if result:
        return result[0]
    return None, None


def station_to_ref(station):
    name = station.get('name').strip()  # TODO: fallback to streams URI?
    country = station.get('country', '??')
    uri = unparse_uri('station', station['id'])
    return Ref.track(uri=uri, name='%s - %s' % (country, name))


def category_to_ref(category):
    uri = unparse_uri('category', category['id'])
    return Ref.directory(uri=uri, name=category.get('title', uri))


def country_to_ref(country_code):
    uri = unparse_uri('country', country_code.lower())
    try:
        # NOTE: /countries doesn't provide names, just codes so we need this.
        country = pycountry.countries.get(alpha2=country_code.upper())
        return Ref.directory(uri=uri, name=country.name)
    except KeyError:
        return Ref.directory(uri=uri, name=country_code.upper())
{'fraction_non_alphanumeric': 0.15308322552824494, 'fraction_numerical': 0.00301854247520483, 'mean_word_length': 4.190156599552573, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '489288', 'n_tokens_mistral': 776, 'n_tokens_neox': 758, 'n_words': 172}