#!/usr/bin/env python

import pylast          # pip install pylast
import sys
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

# USE: python apotheosis.py 1234
# RESULT: runs imported apotheosis modules on artist with id 1234 on PTH
def main():
    artist_id = sys.argv[1]     # ID of artist to find similar artists for

    # 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)

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

    print "Apotheosis of Artist:", artist['name'], '\n'
    new_similar = similar.find(artist, lastfm, config)

    print "SIMILAR ARTISTS\n"
    print "\n".join(new_similar), '\n'
    similar.add(artist, new_similar, pth, config)

    print "ARTIST IMAGE\n"
    # if no artist image, rehost one from last.fm and add it
    if image.missing(artist['image']):
        print "missing ...",
        scraped_image = image.get(lastfm, artist['name'])
        if scraped_image == None:
            print "failed to get image from Last.fm :(\n"
        else:
            print "found ...",
            image_to_add = scraped_image

            if (config.ptpimg_api_key != None):
                print "rehosting ...",
                rehosted_image = image.rehost(scraped_image)

                if (rehosted_image == None):
                    print "failed to rehost image :()\n"
                    print "adding image from original host instead ...",
                else:
                    image_to_add = rehosted_image

            print "adding ...",

            image.edit(artist, image_to_add, pth)
            # need to add check here for possible error
            print "done!\n"

    # if bad host, rehost it and add it
    elif image.bad_host(artist['image']) and config.ptpimg_api_key != None:
        print artist['image']
        print "bad host ...",
        print "rehosting ...",
        rehosted_image = image.rehost(artist['image'])
        if (rehosted_image == None):
            print "failed to rehost image :()"
        else:
            print "adding ...",
            image.edit(artist, rehosted_image, pth)
            # need to add check here for possible error
            print "done!\n"

    # update artist data incase it changed
    artist = pth.request("artist", id=artist_id)


    print "ARTIST BIO\n"
    # if no artist bio, add the one on last.fm
    if bio.missing(artist):
        print "missing ...",
        bio.add(artist, lastfm, pth, config)

    print ""
    albums.get_covers(artist, lastfm, pth)

    pth.logout()

if __name__ == '__main__':
    main()
