<commit_before>util = require'util'
simplehttp = util.simplehttp
json = util.json
urlEncode = util.urlEncode


quote = (a, withname, withoutprice) ->
  if not withname
    withname = false
  if not withoutprice
    withoutprice = false
  data = simplehttp "https://query1.finance.yahoo.com/v10/finance/quoteSummary/#{urlEncode a}?modules=price"
  data = json.decode(data)
  res = data['quoteSummary']['result'][1]
  price = res.price

  out = {}

  c =  price.currency
  if price.currencySymbol
    c = price.currencySymbol
  unless withoutprice
    out[#out+1] = c

  current_price = price.regularMarketPrice.fmt
  current_per = price.regularMarketChangePercent.fmt
  current_raw_per = price.regularMarketChangePercent.raw
  regularmarket = true
  premarket = false
  postmarket = false
  if price.preMarketTime
    if price.preMarketTime > price.regularMarketTime
      current_price = price.preMarketPrice.fmt
      current_per = price.preMarketChangePercent.fmt
      current_raw_per = price.preMarketChangePercent.raw
      premarket = true
    elseif price.postMarketTime and price.postMarketTime > price.regularMarketTime
      current_price = price.postMarketPrice.fmt
      current_per = price.postMarketChangePercent.fmt
      current_raw_per = price.postMarketChangePercent.raw
      postmarket = true

  unless withoutprice
    out[#out+1] = current_price
  if current_raw_per > 0
    current_per = util.green current_per
  else
    current_per = util.red current_per
  out[#out+1] = "("..current_per ..")"

  --if price.preMarketPrice.fmt
  --  out[#out+1] = "("..price.preMarketPrice.fmt..")"

  --if price.postMarketPrice.fmt
  --  out[#out+1] = "("..price.postMarketPrice.fmt..")"
  --
  --

  if withname
    if price.longName == json.null
      out[#out+1] = price.shortName
    else
      out[#out+1] = price.longName

    if premarket or postmarket
      out[#out+1] = "🥱"

  return table.concat(out, ' ')


sayquote = (s, d, a) =>
  say quote(a, true)

meme = (s, d, a) =>
  stonks = {'GME', 'AMC', 'BTC-USD', 'TSLA'}
  out = {}
  for stonk in *stonks
    out[#out+1] = stonk .. ' ' .. quote(stonk)

  say table.concat(out, ' ')


idx = (s, d, a) =>
  stonks = {'OSEBX.OL', '^IXIC', '^GSPC', '^DJI', '^NYA', '^N225'}
  out = {}
  for stonk in *stonks
    out[#out+1] = stonk .. ' ' .. quote(stonk, true, true)

  say table.concat(out, ' ')

PRIVMSG:
  '^%pq (.*)$': sayquote
  '^%pstonks': meme
  '^%pidx$': idx<commit_msg>stonks: be better<commit_after>util = require'util'
simplehttp = util.simplehttp
json = util.json
urlEncode = util.urlEncode


quote = (a, withname, withoutprice) ->
  if not withname
    withname = false
  if not withoutprice
    withoutprice = false
  data = simplehttp "https://query1.finance.yahoo.com/v10/finance/quoteSummary/#{urlEncode a}?modules=price"
  data = json.decode(data)
  res = data['quoteSummary']['result'][1]
  unless res return ''
  price = res.price

  out = {}

  unless withname
    out[#out+1] = price.symbol

  c =  price.currency
  if price.currencySymbol
    c = price.currencySymbol
  unless withoutprice
    out[#out+1] = c

  current_price = price.regularMarketPrice.fmt
  current_per = price.regularMarketChangePercent.fmt
  current_raw_per = price.regularMarketChangePercent.raw
  regularmarket = true
  premarket = false
  postmarket = false
  if price.preMarketTime
    if price.preMarketTime > price.regularMarketTime
      current_price = price.preMarketPrice.fmt
      current_per = price.preMarketChangePercent.fmt
      current_raw_per = price.preMarketChangePercent.raw
      premarket = true
    elseif price.postMarketTime and price.postMarketTime > price.regularMarketTime
      current_price = price.postMarketPrice.fmt
      current_per = price.postMarketChangePercent.fmt
      current_raw_per = price.postMarketChangePercent.raw
      postmarket = true

  unless withoutprice
    out[#out+1] = current_price
  if current_raw_per > 0
    current_per = util.green current_per
  else
    current_per = util.red current_per
  out[#out+1] = " "..current_per .." "

  --if price.preMarketPrice.fmt
  --  out[#out+1] = "("..price.preMarketPrice.fmt..")"

  --if price.postMarketPrice.fmt
  --  out[#out+1] = "("..price.postMarketPrice.fmt..")"
  --
  --

  if withname
    if price.longName == json.null
      out[#out+1] = price.shortName
    else
      out[#out+1] = price.longName

    if premarket or postmarket
      out[#out+1] = "🥱"

  return table.concat(out, ' ')

multiquote = (stonks) ->
  out = {}
  for stonk in *stonks
    out[#out+1] = quote(stonk,  false, true)

  return table.concat(out, ' | ')

sayquote = (s, d, a) =>
  if string.find(a, ' ')
    say multiquote(util.split(a, ' '))
  else
    say quote(a, true, false)

meme = (s, d, a) =>
  stonks = {'GME', 'AMC', 'BTC-USD', 'TSLA'}
  say multiquote(stonks)

idx = (s, d, a) =>
  stonks = {'OSEBX.OL', '^IXIC', '^GSPC', '^DJI', '^NYA', '^N225'}
  say multiquote(stonks)

crypto = (s, d) =>
  say multiquote {'BTC-USD', 'ETH-USD', 'XRP-USD', 'DOGE-USD'}


PRIVMSG:
  '^%pq (.*)$': sayquote
  '^%pstonks': meme
  '^%pidx$': idx
  '^%pcrypto$': crypto
{'fraction_non_alphanumeric': 0.14296905601253426, 'fraction_numerical': 0.007050528789659225, 'mean_word_length': 3.2879932829554996, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3263325', 'n_tokens_mistral': 1915, 'n_tokens_neox': 1817, 'n_words': 378}