Feat plugin stock (#196)
This commit is contained in:
34
source/plugins/stock/README.md
Normal file
34
source/plugins/stock/README.md
Normal file
@@ -0,0 +1,34 @@
|
||||
### 💹 Stock prices
|
||||
|
||||
The *stock* plugin lets you display the stock market price of a given company.
|
||||
|
||||
<table>
|
||||
<td align="center">
|
||||
<img src="https://github.com/lowlighter/lowlighter/blob/master/metrics.plugin.stock.svg">
|
||||
<img width="900" height="1" alt="">
|
||||
</td>
|
||||
</table>
|
||||
|
||||
<details>
|
||||
<summary>💬 Obtaining a RapidAPI Yahoo Finance token</summary>
|
||||
|
||||
Create a [RapidAPI account](https://rapidapi.com) and subscribe to [Yahoo Finance API](https://rapidapi.com/apidojo/api/yahoo-finance1) to get a token.
|
||||
|
||||

|
||||
|
||||
</details>
|
||||
|
||||
#### ℹ️ Examples workflows
|
||||
|
||||
[➡️ Available options for this plugin](metadata.yml)
|
||||
|
||||
```yaml
|
||||
- uses: lowlighter/metrics@latest
|
||||
with:
|
||||
# ... other options
|
||||
plugin_stock: yes
|
||||
plugin_stock_token: ${{ secrets.STOCK_TOKEN }} # RapidAPI Yahoo Finance token
|
||||
plugin_stock_symbol: TSLA # Display Tesla stock price
|
||||
plugin_stock_duration: 1d # Display last day of market
|
||||
plugin_stock_interval: 5m # Use precision of 5 minutes for each record
|
||||
```
|
||||
60
source/plugins/stock/index.mjs
Normal file
60
source/plugins/stock/index.mjs
Normal file
@@ -0,0 +1,60 @@
|
||||
//Setup
|
||||
export default async function({login, q, imports, data, account}, {enabled = false, token} = {}) {
|
||||
//Plugin execution
|
||||
try {
|
||||
//Check if plugin is enabled and requirements are met
|
||||
if ((!enabled)||(!q.stock))
|
||||
return null
|
||||
|
||||
//Load inputs
|
||||
let {symbol, interval, duration} = imports.metadata.plugins.stock.inputs({data, account, q})
|
||||
if (!token)
|
||||
throw {error:{message:"A token is required"}}
|
||||
if (!symbol)
|
||||
throw {error:{message:"A company stock symbol is required"}}
|
||||
symbol = symbol.toLocaleUpperCase()
|
||||
|
||||
//Query API for company informations
|
||||
console.debug(`metrics/compute/${login}/plugins > stock > querying api for company`)
|
||||
const {data:{quoteType:{shortName:company}}} = await imports.axios.get("https://apidojo-yahoo-finance-v1.p.rapidapi.com/stock/v2/get-profile", {
|
||||
params:{symbol, region:"US"},
|
||||
headers:{"x-rapidapi-key":token},
|
||||
})
|
||||
|
||||
//Query API for sotck charts
|
||||
console.debug(`metrics/compute/${login}/plugins > stock > querying api for stock`)
|
||||
const {data:{chart:{result:[{meta, timestamp, indicators:{quote:[{close}]}}]}}} = await imports.axios.get("https://apidojo-yahoo-finance-v1.p.rapidapi.com/stock/v2/get-chart", {
|
||||
params:{interval, symbol, range:duration, region:"US"},
|
||||
headers:{"x-rapidapi-key":token},
|
||||
})
|
||||
const {currency, regularMarketPrice:price, previousClose:previous} = meta
|
||||
|
||||
//Generating chart
|
||||
console.debug(`metrics/compute/${login}/plugins > stock > generating chart`)
|
||||
const chart = await imports.chartist("line", {
|
||||
width:480,
|
||||
height:160,
|
||||
showPoint:false,
|
||||
axisX:{showGrid:false, labelInterpolationFnc:(value, index) => index%Math.floor(close.length/4) === 0 ? value : null},
|
||||
axisY:{scaleMinSpace:20},
|
||||
showArea:true,
|
||||
}, {
|
||||
labels:timestamp.map(timestamp => new Intl.DateTimeFormat("en-GB", {month:"2-digit", day:"2-digit", hour:"2-digit", minute:"2-digit"}).format(new Date(timestamp*1000))),
|
||||
series:[close],
|
||||
})
|
||||
|
||||
//Results
|
||||
return {chart, currency, price, previous, delta:price-previous, symbol, company, interval, duration}
|
||||
}
|
||||
//Handle errors
|
||||
catch (error) {
|
||||
let message = "An error occured"
|
||||
if (error.isAxiosError) {
|
||||
const status = error.response?.status
|
||||
const description = error.response?.data?.message ?? null
|
||||
message = `API returned ${status}${description ? ` (${description})` : ""}`
|
||||
error = error.response?.data ?? null
|
||||
}
|
||||
throw {error:{message, instance:error}}
|
||||
}
|
||||
}
|
||||
58
source/plugins/stock/metadata.yml
Normal file
58
source/plugins/stock/metadata.yml
Normal file
@@ -0,0 +1,58 @@
|
||||
name: "💹 Stock prices"
|
||||
cost: N/A
|
||||
categorie: other
|
||||
index: 1
|
||||
supports:
|
||||
- user
|
||||
- organization
|
||||
inputs:
|
||||
|
||||
# Enable or disable plugin
|
||||
plugin_stock:
|
||||
description: Display stock prices of a given company
|
||||
type: boolean
|
||||
default: no
|
||||
|
||||
# RapidAPI Yahoo finance token
|
||||
# Case insensitive
|
||||
plugin_stock_token:
|
||||
description: Yahoo Finance token
|
||||
type: token
|
||||
default: ""
|
||||
|
||||
# Company stock symbol (required)
|
||||
plugin_stock_symbol:
|
||||
description: Company stock symbol
|
||||
type: string
|
||||
default: ""
|
||||
|
||||
# Time range to display (relative to current date)
|
||||
plugin_stock_duration:
|
||||
description: Time range to display
|
||||
type: string
|
||||
default: 1d
|
||||
values:
|
||||
- 1d # Today
|
||||
- 5d # 5 days
|
||||
- 1mo # 1 month
|
||||
- 3mo # 3 months
|
||||
- 6mo # 6 months
|
||||
- 1y # 1 year
|
||||
- 2y # 2 years
|
||||
- 5y # 5 years
|
||||
- 10y # 10 years
|
||||
- ytd # Year to date
|
||||
- max # All time
|
||||
|
||||
# Time invervals between each records over the given time range
|
||||
plugin_stock_interval:
|
||||
description: Time intervals between records
|
||||
type: string
|
||||
default: 5m
|
||||
values:
|
||||
- 1m # 1 minute
|
||||
- 2m # 2 minutes
|
||||
- 5m # 5 minutes
|
||||
- 15m # 15 minutes
|
||||
- 60m # 60 minutes
|
||||
- 1d # 1 day
|
||||
17
source/plugins/stock/tests.yml
Normal file
17
source/plugins/stock/tests.yml
Normal file
@@ -0,0 +1,17 @@
|
||||
- name: Stock plugin (default)
|
||||
uses: lowlighter/metrics@latest
|
||||
with:
|
||||
token: NOT_NEEDED
|
||||
plugin_stock: yes
|
||||
plugin_stock_token: MOCKED_TOKEN
|
||||
plugin_stock_symbol: OCTO
|
||||
|
||||
- name: Stock plugin (complete)
|
||||
uses: lowlighter/metrics@latest
|
||||
with:
|
||||
token: NOT_NEEDED
|
||||
plugin_stock: yes
|
||||
plugin_stock_token: MOCKED_TOKEN
|
||||
plugin_stock_symbol: OCTO
|
||||
plugin_stock_duration: 5d
|
||||
plugin_stock_interval: 5m
|
||||
Reference in New Issue
Block a user