From 11bcdbc1070552dfc99df332d554e99057281d1b Mon Sep 17 00:00:00 2001
From: Paul Duncan <pabs@pablotron.org>
Date: Fri, 6 Mar 2020 20:00:14 -0500
Subject: asdf

---
 .gitignore        |  1 +
 README.md         | 19 +++++++++++++++++++
 bin/get-stock.py  | 22 ++++++++++++++++++++++
 public/face.js    | 26 +++++++++++++++++++++++++-
 public/index.html | 22 +++++++++++++++-------
 5 files changed, 82 insertions(+), 8 deletions(-)
 create mode 100644 README.md
 create mode 100755 bin/get-stock.py

diff --git a/.gitignore b/.gitignore
index 45d62d8..f5f0a5a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
 *.sw?
+public/current.json
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..ba8fe8a
--- /dev/null
+++ b/README.md
@@ -0,0 +1,19 @@
+= Alonzo Mood Calculator
+
+Calculate Alonzo's likely mood for the day.
+
+== Installation
+
+Setup instructions (Debian):
+
+    # install yfinance
+    sudo pip3 install yfinance
+
+    # run bin/get-stock.py once to get the current prices
+    bin/get-stock.py ORCL > public/current.json
+    
+    # set up cron job that calls bin/get-stock.py daily
+    # example:
+    @daily bin/get-stock.py ORCL > public/current.json
+
+    # symlink apache or whatever to public/ directory
diff --git a/bin/get-stock.py b/bin/get-stock.py
new file mode 100755
index 0000000..334cab6
--- /dev/null
+++ b/bin/get-stock.py
@@ -0,0 +1,22 @@
+#!/usr/bin/python3
+
+#
+# get-stock.py: fetch current stock ticker information and write it to
+# as JSON to standard output.
+#
+# Usage:
+#
+#   # fetch current google price and write it to current.json
+#   get-stock.py GOOG > current.json
+#
+
+import sys
+import json
+import yfinance
+
+# check command-line arguments
+if len(sys.argv) < 2:
+  raise RuntimeError('missing code')
+
+# get ticker info, convert to json, print to stdout
+print(json.dumps(yfinance.Ticker(sys.argv[1]).info))
diff --git a/public/face.js b/public/face.js
index 2e8d722..11cb309 100644
--- a/public/face.js
+++ b/public/face.js
@@ -1,6 +1,14 @@
 jQuery(function($) {
   "use strict";
 
+  var BASE_PRICE = 55.73;
+  var CURRENT = 10;
+  var DATA_URL = './current.json';
+
+  function to_percent(ask) {
+    return Math.round((r.ask - BASE_PRICE) / BASE_PRICE * 10000) / 100.0;
+  }
+
   /**
    * get the current time, in milliseconds since the epoch.
    */
@@ -175,8 +183,24 @@ jQuery(function($) {
   $('#rate').keyup(tick).focus();
 
   $('.set-rate').click(function() {
-    $('#rate').val($(this).data('val'));
+    var val = $(this).data('val');
+
+    // set value
+    $('#rate').val((val == 'current') ? current : val);
+
+    // stop event
     return false;
   });
+
+  $.ajax({
+    method: 'GET',
+    url: DATA_URL,
+    dataType: 'json'
+  }).fail(function(r) {
+    alert("Couldn't fetch current data.");
+  }).done(function(r) {
+    CURRENT = to_percent(r.ask);
+    $('#rate').val(CURRENT);
+  });
 });
 
diff --git a/public/index.html b/public/index.html
index d852330..43eeed9 100644
--- a/public/index.html
+++ b/public/index.html
@@ -18,7 +18,7 @@
 
       <p id='controls'>
         <label for='rate'>Stock Price Change (Percent):</label><br/>
-        <input 
+        <input
           type='number'
           id='rate'
           value='0'
@@ -26,14 +26,22 @@
         />
 
         <button
-          class='set-rate' 
+          class='set-rate'
           data-val='-10'
           title='Set change to minimum.'
         >
           Min
         </button>
 
-        <button 
+        <button
+          class='set-rate'
+          data-val='0'
+          title='Set change to zero.'
+        >
+          Clear
+        </button>
+
+        <button
           class='set-rate'
           data-val='10'
           title='Set change to maximum.'
@@ -41,12 +49,12 @@
           Max
         </button>
 
-        <button 
+        <button
           class='set-rate'
-          data-val='0'
-          title='Reset change to zero.'
+          data-val='current'
+          title='Set to today&apos; price.'
         >
-          Reset
+          Current
         </button>
       </p>
     </div>
-- 
cgit v1.2.3