#! python.3
# Check current stock
# Written by: <NAME>

# TODO: Type "Help" and get a list of common ones I like

import webbrowser, sys
import requests
import re

print("[+] Check the Stock")
print("[+] You normally like CERN, CTL")
print("[+] What Stock symbol are you wanting?")
symbol = input()


#webbrowser.open('https://money.cnn.com/quote/quote.html?symb=' + symbol)

# Use requests to access the page. If it fails, print exception
res = requests.get('https://money.cnn.com/quote/quote.html?symb=' + symbol.upper())
try:
    res.raise_for_status()
except Exception as exc:
    print('There was a problem: %s' % (exc))

# RegEx to extract information from page
title = re.search(r'\<title\>(.*?)\<\/title\>', res.text).group(1)

try:
    current = re.search(r'ToHundredth\" streamFeed\=\"BatsUS\"\>(.*?)\<\/span\>\<div', res.text).group(1)
except AttributeError:
    print("[!] Invalid Stock Symbol")
    sys.exit(1)

# RegEx to see if Positive or Negative Change
try:
    change = re.search(r'streamFeed\=\"BatsUS\"\>\<span class\=\"posData\"\>(.*?)\<\/span\>\<\/span\>\<span', res.text).group(1)
except AttributeError:
    change = re.search(r'streamFeed\=\"BatsUS\"\>\<span class\=\"negData\"\>(.*?)\<\/span\>\<\/span\>\<span', res.text).group(1)

print("[*] " + title + " [*]")
print("[+] Current is: " + current)
print("[+] Current Change is: " + str(change))
