#!/usr/bin/env python

"""
An unofficial API for pitchfork.com reviews.

author: Michal Czaplinski
email: mmczaplinski@gmail.com
"""

import json
import difflib
from bs4 import BeautifulSoup

from urllib.parse import urljoin, quote
from urllib.request import urlopen
from urllib.request import Request


def replace_breaks(html):
    """Replace all the <br> tags in the html with newlines."""
    breakline = html.br
    while html.br is not None:
        breakline.insert_before('\n')
        html.br.decompose()
        breakline = html.br
    return html


class Review:
    """
    Class representing the fetched review.

    Includes methods for getting the score, the text of the review
    (editorial), the album cover, label, year as well as the true
    (matched) album and artist names.
    """

    def __init__(self, searched_artist, searched_album, matched_artist,
                 matched_album, query, url, soup):
        """Create a review object."""
        self.searched_artist = searched_artist
        self.searched_album = searched_album
        self.matched_artist = matched_artist
        self.matched_album = matched_album
        self.query = query
        self.url = url
        self.soup = soup

    def score(self):
        """Return the album score."""
        rating = self.soup.find(class_='score').text
        rating = float(rating.strip())
        return rating

    def abstract(self):
        """Return the text of the abstract."""
        return self.soup.find(class_='review-detail__abstract').get_text()

    def best_new_music(self):
        return self.soup.find(class_='bnm-arrows') != None

    def editorial(self):
        """Return the main review text."""
        return self.soup.find(class_='contents dropcap').get_text()

    def full_text(self):
        """Return a combination of the abstract and editorial."""
        return "{}{}".format(self.abstract(), self.editorial())

    def cover(self):
        """Return the link to the album cover."""
        artwork = self.soup.find(class_='album-art')
        image_link = artwork.img['src'].strip()
        return image_link

    def artist(self):
        """Return the artist name that pitchfork matched to our search."""
        artist = self.matched_artist.strip()
        return artist

    def album(self):
        """Return the album name that pitchfork matched to our search."""
        album = self.matched_album.strip()
        return album

    def label(self):
        """Return the name of the record label that released the album."""
        labels = list(set(self.soup.findAll(class_='labels-list__item')))
        label = ' / '.join([l.get_text() for l in labels])
        return label

    def year(self):
        """
        Return the year the album was released.

        In case of a reissue album, the year of original release as well as
        the year of the reissue is given separated by '/'.
        """
        year = self.soup.find(class_='single-album-tombstone__meta-year').get_text()[3:]
        return year

    def methods(self):
        """Print a list of methods."""
        help_text = """score()
abstract()
best_new_music()
editorial()
full_text()
cover()
artist()