from pathlib import Path
import sys

notebook_directory_parent = Path.cwd().resolve().parent
if str(notebook_directory_parent) not in sys.path:
    sys.path.append(str(notebook_directory_parent))

"""
Explanation: Setup
Setup that is specific only to Jupyter notebooks
End of explanation
"""


from T1000.utilities.configure_paths import (DataPaths, NISTChemistryWebbookPaths)
from T1000.NistChemBook import species_list

from bs4 import BeautifulSoup

import csv
import requests

"""
Explanation: Setup to use Python libraries/modules
End of explanation
"""


csv_as_list = species_list.ReadAndClean.to_list()

csv_as_list[:5]

"""
Explanation: Species List
Download the Species list.
https://webbook.nist.gov/chemistry/download/
End of explanation
"""


DataPaths().raw()

list(DataPaths().raw().glob('**/*'))

species_txt_path = \
    next(path for path in list(DataPaths().raw().glob('**/*')) if "species" in str(path) and ".txt" in str(path))

f = open(species_txt_path, 'r', newline='')
csv_reader = csv.reader(f, delimiter="\t")

csv_as_list = list(csv_reader)
f.close()

for row in csv_as_list:
    if len(row) != 3:
        print(len(row))

"""
Explanation: Scratch work
End of explanation
"""


species_list_columns = species_list.ScrapeWebpage.species_list_columns()

species_list_columns

ready_data = species_list.ReadAndClean.to_database_ready_data(species_list_columns)

ready_data[:5]

"""
Explanation: Scrape Species List (Download) page
cf. https://www.digitalocean.com/community/tutorials/how-to-scrape-web-pages-with-beautiful-soup-and-python-3
End of explanation
"""


NISTChemistryWebbookPaths.species_list()

page = requests.get(NISTChemistryWebbookPaths.species_list())

# Use Python's built-in html.parser
soup = BeautifulSoup(page.text, 'html.parser')

soup.find(id="main").find('ul').find_all('li')[0].text

"""
Explanation: Scratchwork
End of explanation
"""


