from splinter import Browser
from bs4 import BeautifulSoup

import time 
import tweepy
import pandas as pd

executable_path = {"executable_path": "/Users/gazellebahramian/Downloads/chromedriver"}
browser = Browser("chrome", **executable_path, headless=False)

def scrape():
    final_data = {}
    output = marsNews()
    final_data["mars_news"] = output[0]
    final_data["mars_paragraph"] = output[1]
    final_data["mars_image"] = marsImage()
    final_data["mars_weather"] = marsWeather()
    final_data["mars_facts"] = marsFacts()
    final_data["mars_hemisphere"] = marsHem()

    return final_data

def marsNews():
    news_url = "https://mars.nasa.gov/news/"
    browser.visit(news_url)
    html = browser.html
    soup = BeautifulSoup(html, "html.parser")
    article = soup.find("div", class_='list_text')
    news_title = article.find("div", class_="content_title").text
    news_p = article.find("div", class_ ="article_teaser_body").text
    output = [news_title, news_p]
    return output

def marsImage():
    image_url = "https://www.jpl.nasa.gov/spaceimages/?search=&category=Mars"
    browser.visit(image_url)
    html = browser.html
    soup = BeautifulSoup(html, "html.parser")
    image = soup.find("img", class_="thumb")["src"]
    featured_image_url = "https://www.jpl.nasa.gov/spaceimages/images/largesize/PIA22377_hires.jpg"
    return featured_image_url

def marsWeather():
    consumer_key = '4oaVujwymb3zGPtw39jaIPCVu'
    consumer_secret = 'fEbVOlv4az4gPQKL4C1dI6860JBpXZqXausdey1ENO3mISvLnW'
    access_token = '969400010327523328-hjqt80cG3UNMyNrRT4CTw1kuqHytB9r'
    access_token_secret = 'urpx9bRyeuTpprb6tlPI18ngP8lnKtEdzjITIsvrv9kUQ'

    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    api = tweepy.API(auth, parser=tweepy.parsers.JSONParser())

    target_user = "MarsWxReport"
    tweet = api.user_timeline(target_user, count =1)
    m_weather = ((tweet)[0]['text'])
    return m_weather


def marsFacts():
    import pandas as pd
    spacefacts_url = "https://space-facts.com/mars/"
    browser.visit(spacefacts_url)
    marsd = pd.read_html(spacefacts_url)
    marsd = pd.DataFrame(marsd[0])
    marsd.columns = ["Description", "Value"]
    marsd = marsd.set_index("Description")
    mars_facts = marsd.to_html(index = True, header =True)
    return mars_facts


def marsHem():
    import time 
    hemispheres_url = "https://astrogeology.usgs.gov/search/results?q=hemisphere+enhanced&k1=target&v1=Mars"
    browser.visit(hemispheres_url)
    html = browser.html
    soup = BeautifulSoup(html, "html.parser")
    mars_hemisphere = []

    products = soup.find("div", class_ = "result-list" )
    hemispheres = products.find_all("div", class_="item")

    for hemisphere in hemispheres:
        title = hemisphere.find("h3").text
        title = title.replace("Enhanced", "")
        end_link = hemisphere.find("a")["href"]
        image_link = "https://astrogeology.usgs.gov/" + end_link    
        browser.visit(image_link)
        html = browser.html
        soup=BeautifulSoup(html, "html.parser")
        downloads = soup.find("div", class_="downloads")
        image_url = downloads.find("a")["href"]
        dictionary = {"title": title, "img_url": image_url}
        mars_hemisphere.append(dictionary)
    return mars_hemisphere

