"""$<ticker symbol> for a quote on a stock price"""
from __future__ import print_function
import logging
import re
try:
    from urllib import quote
except ImportError:
    from urllib.request import quote

from bs4 import BeautifulSoup
import requests

logger = logging.getLogger(__name__)

def stockprice(ticker):
    url = "https://www.google.com/finance?q={0}"
    soup = BeautifulSoup(requests.get(url.format(quote(ticker))).text, "html5lib")

    try:
        company, ticker = re.findall(u"^(.+?)\xa0\xa0(.+?)\xa0", soup.text, re.M)[0]
    except IndexError:
        logging.info("Unable to find stock {0}".format(ticker))
        return ""
    price = soup.select("#price-panel .pr span")[0].text
    change, pct = soup.select("#price-panel .nwp span")[0].text.split()
    pct.strip('()')

    emoji = ":chart_with_upwards_trend:" if change.startswith("+") else ":chart_with_downwards_trend:"

    return "{0} {1} {2}: {3} {4} {5} {6}".format(emoji, company, ticker, price, change, pct, emoji)


def on_message(msg, server):
    text = msg.get("text", "")
    matches = re.findall(r"\$[a-zA-Z]\w{0,3}", text)
    if not matches:
        return

    prices = [stockprice(ticker[1:].encode("utf8")) for ticker in matches]
    return "\n".join(p for p in prices if p)

@@ -770,16 +770,90 @@
 split()%0A
+    time = %22 %22.join(soup.select(%22.mdata-dis%22)%5B0%5D.parent.text.split()%5B:4%5D)%0A
     pct.
@@ -931,16 +931,30 @@
 ith(%22+%22)
+ %5C%0A           
  else %22:
@@ -1023,16 +1023,20 @@
  %7B5%7D %7B6%7D
+ %7B7%7D
 %22.format
@@ -1036,16 +1036,29 @@
 .format(
+%0A            
 emoji, c
@@ -1092,16 +1092,22 @@
 ge, pct,
+ time,
  emoji)%0A
@@ -1176,92 +1176,372 @@
     
-matches = re.findall(r%22%5C$%5Ba-zA-Z%5D%5Cw%7B0,3%7D%22, text)%0A    if not matches:%0A        return%0A
+basic_re = r%22%5CB%5C$(%5Ba-zA-Z%5D%5Cw%7B0,3%7D(?:%5C.%5Cw%7B1,3%7D)?)%5Cb%22%0A    # slack linkifies some tickers, which results in: $%3Chttp://wbc.ax%7Cwbc.ax%3E%0A    link_re = r%22%5C$%3Chttp://(%5Ba-zA-Z%5D%5Cw%7B0,3%7D(?:%5C.%5Cw%7B1,3%7D)?)%5C%7C%22%0A    matches = re.findall(r%22%7B%7D%7C%7B%7D%22.format(basic_re, link_re, text), text)%0A    if not matches:%0A        return%0A%0A    tickers = (''.join(list(x)).encode(%22utf8%22) for x in matches)
 %0A   
@@ -1550,17 +1550,17 @@
 rices = 
-%5B
+(
 stockpri
@@ -1572,27 +1572,8 @@
 cker
-%5B1:%5D.encode(%22utf8%22)
 ) fo
@@ -1588,16 +1588,16 @@
  in 
-matches%5D
+tickers)
 %0A   

Add time to stock ticker plugin (#82)