from selenium import webdriver
from urllib.request import urlopen
from bs4 import BeautifulSoup
from selenium.webdriver.chrome.options import Options
import requests

class InvestorsHub:
    def __init__(self):
        self.stock_name = []
        self.company_name = []
        self.stock_price = []
        self.stock_change = []
        self.percent_change = []
        self.volume = []
        self.urls = [
            'https://ih.advfn.com/p.php?pid=toplist-ih-stock&mkt=N&tid=1',
            'https://ih.advfn.com/p.php?pid=toplist-ih-stock&mkt=A&tid=1',
            'https://ih.advfn.com/p.php?pid=toplist-ih-stock&mkt=NY&tid=1'

        ]
        self.potential_stocks = []

    def pull(self):
        index = 0
        while(index < 3):
            self.parse_data = requests.get(self.urls[index])
            self.parsed_data = BeautifulSoup(self.parse_data.content, "html.parser")
            self.table = self.parsed_data.findAll("table", {"id": "toplistData"})
            self.rows = self.table[0].findAll("tr")
            for row in self.rows:
                self.table_data = row.findAll("td")
                try:
                    self.stock_name.append(self.table_data[0].text.strip())
                    self.company_name.append(self.table_data[1].text.strip())
                    self.stock_price.append(self.table_data[2].text.strip())
                    self.stock_change.append(self.table_data[3].text.strip())
                    self.percent_change.append(self.table_data[4].text.strip())
                    self.volume.append(self.table_data[5].text.strip())

                except: IndexError
                continue
            index += 1

    def filter_results(self):
        index = 0
        while(index <=29):
            value = self.percent_change[index].strip('%')
            value = float(value)
            if (int(value) >= 14 and int(value) <= 26):
                price = self.stock_price[index].strip('')
                if(float(price) > .75 and float(price) < 2.00):
                    volume = self.volume[index].strip(',')
                    volume = volume.replace(',','')
                    if(float(volume) > 600000 and float(volume) < 6000000):

                        self.potential_stocks.append(self.stock_name[index])
                        index += 1
                    else:
                        index += 1
                else:
                    index+= 1
            else:
                index += 1


    def results(self):
        return self.potential_stocks
