#!/usr/bin/env python
import pylast          # pip install pylast
import sys
from HTMLParser import HTMLParser
import config           # confi file
import pthapi           # local copy, taken from what I was using with pthbetter
import bio              # get bio from last.fm if it's empty on PTH
import similar          # get similar artists from lastfm
import image            # get image from last.fm if it's missing on PTH
import albums           # get album covers for artists discography
from classes import *


def main():
    parser = HTMLParser()

    # last.fm connection
    lastfm = pylast.LastFMNetwork(api_key=config.api_key,
                                  api_secret=config.api_secret)
    # pth connection
    pth = pthapi.PthAPI(config.pth_username, config.pth_password)
    print "authkey= ", pth.authkey, "\n"

    if len(sys.argv) > 2 and sys.argv[1] == '-C':
        collage_id = sys.argv[2]
        collage = Collage(pth.request("collage", id=collage_id))
        print "Apotheosis of Collage:", parser.unescape(collage.name), '\n'
        print"ALBUM COVERS\n"
        albums.get_covers(collage, pth, lastfm)
    else:
        artist_id = sys.argv[1]     # ID of artist to find similar artists for

        # get artist name and info from id
        artist = Artist(pth.request("artist", id=artist_id))

        print "Apotheosis of Artist:", parser.unescape(artist.name), '\n'

        print "SIMILAR ARTISTS\n"
        new_similar = similar.find(artist, lastfm)
        similar.add(artist, new_similar, pth)
        print parser.unescape("\n".join(new_similar)), '\n'

        print "ARTIST IMAGE\n"
        if not image.is_fine(artist.image):
            image.fix(artist, pth, lastfm)

        print "ARTIST BIO\n"
        if bio.missing(artist):
            print "missing ...",
            bio.add(artist, lastfm, pth)

        print"ALBUM COVERS\n"
        albums.get_covers(artist, pth, lastfm)

    pth.logout()


if __name__ == '__main__':
    main()
