from lxml import html
import requests
import os, sys

# this relies on a chrome plug in:  https://chrome.google.com/webstore/detail/downalbum/cgjnhhjpfcdhbhlcmmjppicjmgfkppok
#use the plug-in, it allows you to download an album, along with an .htm file. This script extracts the relevant data from that .htm and outputs an .html set to go on dave's site

# set this to what you want the title of the html page to be, in quotes
pagetitle = "Psychoanalysis and Neuroscience"

# This is the text at the top of the page, eg "From a Columbia-Barnard-Rutgers workshop in the philosophy of mind at the Brooklyn Zen Center in Gowanus, August 2017". In quotes.

pagedescription = "Psychoanalysis and Neuroscience: A Debate \"Is Psychoanalysis Relevant to Neuroscience\", May 3, 2019 followed by a workshop on Consciousness, Memory and the Freudian Unconscious, May 4, 2019, NYU"

# This is the name of the album, eg, "Summer2016", if the pic folder is "Summer2016@@". 
albumname = "psychoanalysisneuroscience"

#write in the name of the target downloaded htm file
with open(r'psychoanalysisneuroscience.htm', "r", encoding="latin-1") as f:
    page = f.read()
tree = html.fromstring(page)


albumbase = tree.xpath('//div[@rel="gallery"]')


tagsbypic = [tree.xpath('//div[@rel="gallery"]' + '[' + str(n+1) +']' + '//div[@class="tagName"]/span[text()]') for n in range(0,len(albumbase))]
#testalbumsecondelement = tree.xpath('//div[@rel="gallery"]' + '[2]' + '//div[@class="tagName"]/span[text()]')

#still needs text conversion

capsbypic = [tree.xpath('//div[@rel="gallery"]' + '[' + str(n+1) +']' + '//div[@class="caption"]/p[text()]') for n in range(0,len(albumbase))]



idbypic =[tree.xpath('//div[@rel="gallery"]' + '[' + str(n+1) +']' + '//a[@class="fancybox"]/@href') for n in range(0,len(albumbase))]

#picidsproto = [element.text for element in idbypic]

picidproto = [str(element).split("_files/") for element in idbypic]
picid = [f[-1] for f in picidproto]


htmlstring = """

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<meta charset="utf-8">
<style>
img { 
    max-width: 1024px;
    max-height: 1024px;
    }
</style>
    <TITLE>""" + pagetitle + """</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#FF0000">
<br>

<h3>""" + pagedescription +  """</h3>
<br>
<b>

"""

for n in range(0,len(albumbase)):
    picfilename = picid[n].replace("']","")

    if len(capsbypic[n]) != 0:
        caption = [element.text for element in capsbypic[n]]
        
        if len(tagsbypic[n]) != 0:
            tags = [element.text for element in tagsbypic[n]]
            dedupedtags = list(dict.fromkeys(tags))
            tagstring = ", ".join(dedupedtags)

            htmlstring += """
            <img src=\"""" + albumname + """@@/""" + picfilename + """\">
            <p>""" + caption[0] + """</p>
            <!-- """ + tagstring + """ -->
            <br>
            """
        else:

            htmlstring += """
                <img src=\"""" + albumname + """@@/""" + picfilename + """\">
                <p>""" + caption[0] + """</p>
                <!-- -->
                <br>
                """
    else:

        if len(tagsbypic[n]) != 0:
            tags = [element.text for element in tagsbypic[n]]
            dedupedtags = list(dict.fromkeys(tags))
            tagstring = ", ".join(dedupedtags)

            htmlstring += """
            <img src=\"""" + albumname + """@@/""" + picfilename + """\">
            <p>""" + tagstring + """</p>
            <!--  -->
            <br>
            """

        else:
            htmlstring += """
            <img src=\"""" + albumname + """@@/""" + picfilename + """\">
            <p></p>
            <!-- -->
            <br>
            """

htmlstring += """

</b>

<hr>

Go to:
<ul>
<li><a href="http://consc.net/photos/">Photo
    Galleries</a></li>
<li><a href="http://consc.net">My Home Page</a></li>
</ul>
</BODY>
</HTML>
"""


Html_file = open(albumname + ".html","w",encoding='utf8')
Html_file.write(htmlstring)
Html_file.close()           

#WORKS!  JUST BE SURE TO FIX THE ACCENTS AND FORMATTING ON THE NAMES... MIGHT HAVE TO DO FIND/REPLACE
