Skip to content

Commit

Permalink
feat: mac app not need env
Browse files Browse the repository at this point in the history
  • Loading branch information
loic-roux-404 committed Jun 13, 2021
1 parent 94d5adc commit a33ad3b
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 27 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ jobs:
${{ runner.os }}-node-
- run: npm i

- name: semver plugins
env:
CMC_PRO_API_KEY: ${{secrets.CMC_PRO_API_KEY}}
run: |
npm install '@semantic-release/changelog' \
'@semantic-release/exec' \
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
build
.DS_Store
package-lock.json
.env
15 changes: 6 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
# Number Pull cryptocurrency prices

Pull crypto prices from **coinmarketcap** in Mac os Apple Numbers
Pull crypto prices from **coinmarketcap** in Mac os Apple Numbers

#### Tools used
- jxa (javascript for automation)
- jxa (javascript for automation)
- Apple Numbers Objective C exposed library
- [creat-jxa-app](https://github.com/aheissenberger/macos-jxa-bundler/tree/main/packages/create-jxa-app)

## Usage

- First table in first sheet need to be of the form.
- name your sheet : 'pull-crypto-sheet'
- name table(s): 'pull-crypto-table'
- The table need to be of the form.

|CMC coin symbol | price | last refresh |
|:--------------:|-------:|-------------:|
Expand All @@ -24,19 +26,14 @@ In a second part we need to run the jxa script
- **Or** download the the js script in Github release and use it from **cli** :
`osascript pull-cryptos-prices.js`

## Roadmap

- [ ] Config in a Numbers table instead of always taking the first
- [ ] Refacto index.js script with more SRP functions

## Contribution

#### To debug :

```bash
npm run build && npm run run-script
```
or
or
```bash
npm run start &;
npm run run-script # on each edit
Expand Down
Binary file modified assets/example-crypto-portfolio.numbers
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"source": "src/index.js",
"dependencies": {
"cjxa-scripts": "0.0.13",
"crypto-js": "^4.0.0",
"jxabundler": "^1.0.12"
},
"scripts": {
Expand Down
18 changes: 11 additions & 7 deletions src/cmcApi.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { run, getEnv } from "./utils/shell"
import { run } from "./utils/shell"

export default {
url: 'pro-api.coinmarketcap.com',
headers: {
'X-CMC_PRO_API_KEY': getEnv('CMC_PRO_API_KEY')
'X-CMC_PRO_API_KEY': '57c7edd3-62c2-4172-9c9e-1ee3130f52b2'
},
defaultsParams: {
convert: 'USD',
Expand All @@ -14,7 +14,7 @@ export default {
createUrl (endpoint, params) {
let url = `https://${this.url}/${endpoint}?`;

for (const [key, value] of Object.entries({ ...this.defaultsParams, ...params })) {
for (const [key, value] of Object.entries({ ...this.defaultsParams, ...params })) {
url += `${key}=${value}&`;
}

Expand All @@ -26,9 +26,13 @@ export default {
.join(' ');
},
fetch(url, additionalsHeaders = {}) {
return JSON.parse(
run(`curl ${this.createHeaders(additionalsHeaders)} -fsL -G '${url}'`)
)
try {
return JSON.parse(
run(`curl ${this.createHeaders(additionalsHeaders)} -fsL -G '${url}'`)
)
} catch(e) {
throw new Error('Error while fetching ' + url)
}
},
getQuotes(symbols) {
const url = this.createUrl(
Expand All @@ -40,4 +44,4 @@ export default {

return data
}
};
};
18 changes: 10 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
import cmcApi from './cmcApi';
import { friendlyDate } from './utils/date';
import NumbersDoc from './model/NumbersDoc';

/**
* Only for Macos-x (Less Yosemite)
* Pull crypto price in your number sheet
* Needed :
* - first sheet with a table of tree columns in the body
* Usage :
* - osascript pull-cryptos-prices.js
* - osascript pull-cryptos-prices.js
*/
(() => {
'use strict';

const app = Application('Numbers');
app.includeStandardAdditions = true;
// TODO: use a config table
const table = app.documents()[0].sheets()[0].tables()[0];
const NbDoc = new NumbersDoc({
sheetName: 'pull-crypto-sheet',
tableName: 'pull-crypto-table'
})
const table = NbDoc.findTableByNameWithConfig();
const cellsRange = table.cellRange();

// Simple Helper
const testValidValueCell = cell => cell.column().address() === 1 &&
cell.row().address() > table.headerRowCount() &&
cell.row().address() <= (table.rowCount() - table.footerRowCount()) &&
cell.value() != null;
cell.row().address() > table.headerRowCount() &&
cell.row().address() <= (table.rowCount() - table.footerRowCount()) &&
cell.value() != null;

const quotes = cmcApi.getQuotes(
cellsRange
Expand Down
28 changes: 28 additions & 0 deletions src/model/NumbersDoc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export default class NumbersDoc {
constructor(config) {
this.app = Application('Numbers')
this.app.includeStandardAdditions = true
this.config = config
this.document = this.getDocument()
}

getDocument() {
if (0 in this.app.documents()) {
return this.app.documents()[0]
} else {
throw new Error('No document open')
}
}

findTableByName({ sheetName, tableName }) {
return this.document
.sheets()
.find(sheet => sheet.name().toLowerCase() === sheetName.toLowerCase())
.tables()
.find(table => table.name().toLowerCase() === tableName.toLowerCase())
}

findTableByNameWithConfig() {
return this.findTableByName(this.config)
}
}
9 changes: 8 additions & 1 deletion src/utils/shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,11 @@ export const run = command => {
return app.doShellScript(command)
}

export const getEnv = key => run(`echo $${key}`)
export const getEnv = key => {
try {
run('source .env')
return run(`echo $${key}`)
} catch(e) {
throw new Error(`impossible to get env ${key}`)
}
}

0 comments on commit a33ad3b

Please sign in to comment.