<commit_before>import logging
import random

import requests
from bs4 import BeautifulSoup

import util


# CONSTANTS related to the APIs we need to use.
MB_API_ROOT = "https://musicbrainz.org/ws/2/"
RELEASE_ROOT = MB_API_ROOT + "release/?query="

COVER_ART_API_URL = "https://coverartarchive.org/release"

# Hits per hour.
MB_RATE_LIMIT = 1800

# Being a good citizen - produce a useful user_agent.
OWNER_URL = "https://github.com/andrewmichaud/isthisska_bot"
USER_AGENT = "isthisska_twitterbot/1.0 (" + OWNER_URL + ") (<EMAIL>)"
HEADERS = {"User-Agent": USER_AGENT}

# For caching(ish) results from MusicBrainz, and storing our eventual output image.
RELEASE_COUNT_DICT = {}
ALBUM_ART_FILENAME = "album_art.jpg"

LOG = logging.getLogger("root")


def produce_random_album_art():
    """
    Retrieve a random piece of album art from MusicBrainz/Album Art Archive.
    Save it as album_art.jpg
    """
    # Get a random album.
    letter = random.choice("abcdefghijklmnopqrstuvwxyz")
    if letter not in RELEASE_COUNT_DICT:
        gen_dict(letter)

    count = RELEASE_COUNT_DICT[letter]

    # Try a few times - not all albums MusicBrainz lists has album art.
    for i in range(15):

        # Get a random release.
        resp = perform_random_release_search(letter, count)
        if resp.status_code == 503:
            msg = "Rate limited, or other service error."
            LOG.error(msg)
            raise APIException(msg, resp.status_code)

        elif resp.status_code != 200:
            msg = "Unhandled error - code {}. Exiting.".format(resp.status_code)
            LOG.error(msg)
            raise APIException(msg, resp.status_code)

        LOG.info("Performed release search, no issues.")

        # who needs error handling.
        # Get release MBID to pull up the album art.
        soup = BeautifulSoup(resp.text, "html.parser")
        release = soup.find("release")
        LOG.debug("Release: {}".format(release))

        if release is None:
            raise APIException("Couldn't find release!")

        release_id = release.attrs["id"]
        LOG.info("Got release id {}.".format(release_id))

        # Get an image - at last!

        resp = perform_album_art_search(release_id)
        if resp.status_code == 404:
            LOG.error("Got 404 trying to get album art!")
            continue

        # this doesn't seem fragile at all.
        data = resp.json()
        album_art_url = data["images"][0]["image"]

        with open(ALBUM_ART_FILENAME, "wb") as f:
            art_data = get_image(album_art_url)
            f.write(art_data.content)

        break


def gen_dict(letter):
    """Generate count of releases in musicbrainz for letter search."""
    resp = perform_letter_search(letter)
    soup = BeautifulSoup(resp.text, "html.parser")

    release_list = soup.find("release-list")
    count = release_list.attrs["count"]

    LOG.debug("Got count {}.".format(count))<commit_msg>wrap release_list in none check<commit_after>import logging
import random

import requests
from bs4 import BeautifulSoup

import util


# CONSTANTS related to the APIs we need to use.
MB_API_ROOT = "https://musicbrainz.org/ws/2/"
RELEASE_ROOT = MB_API_ROOT + "release/?query="

COVER_ART_API_URL = "https://coverartarchive.org/release"

# Hits per hour.
MB_RATE_LIMIT = 1800

# Being a good citizen - produce a useful user_agent.
OWNER_URL = "https://github.com/andrewmichaud/isthisska_bot"
USER_AGENT = "isthisska_twitterbot/1.0 (" + OWNER_URL + ") (<EMAIL>)"
HEADERS = {"User-Agent": USER_AGENT}

# For caching(ish) results from MusicBrainz, and storing our eventual output image.
RELEASE_COUNT_DICT = {}
ALBUM_ART_FILENAME = "album_art.jpg"

LOG = logging.getLogger("root")


def produce_random_album_art():
    """
    Retrieve a random piece of album art from MusicBrainz/Album Art Archive.
    Save it as album_art.jpg
    """
    # Get a random album.
    letter = random.choice("abcdefghijklmnopqrstuvwxyz")
    if letter not in RELEASE_COUNT_DICT:
        gen_dict(letter)

    count = RELEASE_COUNT_DICT[letter]

    # Try a few times - not all albums MusicBrainz lists has album art.
    for i in range(15):

        # Get a random release.
        resp = perform_random_release_search(letter, count)
        if resp.status_code == 503:
            msg = "Rate limited, or other service error."
            LOG.error(msg)
            raise APIException(msg, resp.status_code)

        elif resp.status_code != 200:
            msg = "Unhandled error - code {}. Exiting.".format(resp.status_code)
            LOG.error(msg)
            raise APIException(msg, resp.status_code)

        LOG.info("Performed release search, no issues.")

        # who needs error handling.
        # Get release MBID to pull up the album art.
        soup = BeautifulSoup(resp.text, "html.parser")
        release = soup.find("release")
        LOG.debug("Release: {}".format(release))

        if release is None:
            raise APIException("Couldn't find release!")

        release_id = release.attrs["id"]
        LOG.info("Got release id {}.".format(release_id))

        # Get an image - at last!

        resp = perform_album_art_search(release_id)
        if resp.status_code == 404:
            LOG.error("Got 404 trying to get album art!")
            continue

        # this doesn't seem fragile at all.
        data = resp.json()
        album_art_url = data["images"][0]["image"]

        with open(ALBUM_ART_FILENAME, "wb") as f:
            art_data = get_image(album_art_url)
            f.write(art_data.content)

        break


def gen_dict(letter):
    """Generate count of releases in musicbrainz for letter search."""
    resp = perform_letter_search(letter)
    soup = BeautifulSoup(resp.text, "html.parser")

    release_list = soup.find("release-list")
    if release_list is None:
        raise APIException("Couldn't find release!")

    count = release_list.attrs["count"]

    LOG.debug("Got count {}.".format(count))
{'fraction_non_alphanumeric': 0.10606313679639218, 'fraction_numerical': 0.00768331384666778, 'mean_word_length': 2.8140127388535032, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 6, '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': '4714363', 'n_tokens_mistral': 1949, 'n_tokens_neox': 1830, 'n_words': 501}