Skip to content

Commit

Permalink
feat: add plugins crypto ✅ (#1535)
Browse files Browse the repository at this point in the history
  • Loading branch information
dajneem23 authored Oct 7, 2023
1 parent c430d64 commit 9f08a38
Show file tree
Hide file tree
Showing 7 changed files with 308 additions and 0 deletions.
100 changes: 100 additions & 0 deletions source/plugins/community/crypto/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<!-- Header -->
<table>
<tr><td colspan="2"><a href="/README.md#-plugins">← Back to plugins index</a></td></tr>
<tr><th colspan="2"><h3>🪙 Crypto</h3></th></tr>
<tr><td colspan="2" align="center">
<p>This plugin generates an SVG image containing crypto metrics from a given address. It uses the CoinGecko API to fetch crypto prices.</p>
<p>For more information, check the <a href="https://www.coingecko.com/vi/api/documentation">CoinGecko API documentation</a>.</p>
</td></tr>
<tr><th>Authors</th><td><a href="https://github.com/dajneem23">@dajneem23</a></td></tr>
<tr>
<th rowspan="3">Supported Features<br><sub><a href="metadata.yml">→ Full specification</a></sub></th>
<td>
<ul>
<li><a href="/source/templates/classic/README.md"><code>📗 Classic template</code></a></li>
<li><a href="/source/templates/repository/README.md"><code>📘 Repository template</code></a></li>
</ul>
</td>
</tr>
<tr>
<td>
<ul>
<li><code>👤 Users</code></li>
<li><code>👥 Organizations</code></li>
<li><code>📓 Repositories</code></li>
</ul>
</td>
</tr>
<tr>
<td><code>🗝️ plugin_crypto</code></td>
</tr>
<tr>
<td colspan="2" align="center">
<img src="https://via.placeholder.com/468x60?text=No%20preview%20available" alt=""></img>
<img width="900" height="1" alt="">
</td>
</tr>
</table>
<!-- /Header -->

## ➡️ Available Options

<!-- Options -->
<table>
<tr>
<td align="center" nowrap="nowrap">Option</td><td align="center" nowrap="nowrap">Description</td>
</tr>
<tr>
<td nowrap="nowrap"><h4><code>plugin_crypto</code></h4></td>
<td rowspan="2"><p>Enable crypto plugin</p><img width="900" height="1" alt=""></td>
</tr>
<tr>
<td nowrap="nowrap">Type: <code>boolean</code><br>Default: <code>no</code><br></td>
</tr>
<tr>
<td nowrap="nowrap"><h4><code>plugin_crypto_id</code></h4></td>
<td rowspan="2"><p>Crypto id (from Coingecko)</p><img width="900" height="1" alt=""></td>
</tr>
<tr>
<td nowrap="nowrap">Type: <code>string</code><br>Default: ""<br>Example: bitcoin<br></td>
</tr>
<tr>
<td nowrap="nowrap"><h4><code>plugin_crypto_vs_currency</code></h4></td>
<td rowspan="2"><p>The target currency of market data (usd, eur, jpy, etc.)</p><img width="900" height="1" alt=""></td>
</tr>
<tr>
<td nowrap="nowrap">Type: <code>string</code><br>Default: "usd"<br>Example: "usd"<br></td>
</tr>
<tr>
<td nowrap="nowrap"><h4><code>plugin_crypto_days</code></h4></td>
<td rowspan="2"><p>Data up to number of days ago (eg. 1,14,30,max)</p><img width="900" height="1" alt=""></td>
</tr>
<tr>
<td nowrap="nowrap">Type: <code>string</code><br>Default: "1"<br>Example: 1<br></td>
</tr>
<tr>
<td nowrap="nowrap"><h4><code>plugin_crypto_precision</code></h4></td>
<td rowspan="2"><p>The number of decimal places to use</p><img width="900" height="1" alt=""></td>
</tr>
<tr>
<td nowrap="nowrap">Type: <code>number</code><br>Default: 2<br>Example: 2<br></td>
</tr>
</table>
<!-- /Options -->

<!--examples-->

```yaml
name: Crypto Metrics
uses: lowlighter/metrics@latest
with:
filename: metrics.plugin.crypto.svg
token: NOT_NEEDED
base: ""
plugin_crypto: yes
plugin_crypto_id: bitcoin
plugin_crypto_vs_currency: usd
plugin_crypto_days: 1
plugin_crypto_precision: 2
```
<!--/examples-->
13 changes: 13 additions & 0 deletions source/plugins/community/crypto/examples.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
- name: Crypto Metrics
uses: lowlighter/metrics@latest
with:
filename: metrics.plugin.crypto.svg
token: NOT_NEEDED
base: ""
plugin_crypto: yes
plugin_crypto_id: bitcoin
plugin_crypto_vs_currency: usd
plugin_crypto_days: 1
plugin_crypto_precision: 2
prod:
skip: true
73 changes: 73 additions & 0 deletions source/plugins/community/crypto/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//Setup
export default async function({login, q, imports, data, account}, {enabled = false, extras = false} = {}) {
//Plugin execution
try {
//Check if plugin is enabled and requirements are met
if ((!q.crypto) || (!imports.metadata.plugins.crypto.enabled(enabled, {extras})))
return null

//Load inputs
let {id, days, vs_currency, precision } = imports.metadata.plugins.crypto.inputs({data, account, q})
if (!id)
throw { error: { message: "Crypto currency id is not set" } }

console.debug(`metrics/compute/${login}/plugins > crypto > querying api for crypto`)

const {
data: coin
} = await imports.axios.get(`https://api.coingecko.com/api/v3/coins/${id}`,
{
params: {
market_data:true
}
})

if (!coin) {
throw {error: {message: "Crypto currency not found"}}
}

const {
data: { prices },
} = await imports.axios.get(`https://api.coingecko.com/api/v3/coins/${id}/market_chart`, {
params: {
vs_currency,
days,
precision
},
})


const chart = imports.Graph.timeline(
prices.map((y, _) => ({ x: new Date(y[0]), y: y[1] })),
{
low: Math.min(...prices.map((y, _) => y[1])),

high: Math.max(...prices.map((y, _) => y[1])),

points: false, text: false,
width: 480 * (1 + data.large),
height: 200
}
)


//Results
return {
chart,
id,
precision,
days:{"1":"Today", "14":"2 Weeks", "30":"1 Month", max :"All-time"}[days],
symbol: coin.symbol,
name: coin.name,
current_price: coin.market_data.current_price[vs_currency],
price_change_percentage_24h: coin.market_data.price_change_percentage_24h,
vs_currency,
logo:coin.image.small,
}
}
//Handle errors
catch (error) {
throw imports.format.error(error)
}
}

52 changes: 52 additions & 0 deletions source/plugins/community/crypto/metadata.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: 🪙 Crypto
category: community
description: |
Generates a SVG image containing crypto metrics from a given address.
This plugin using coingecko API to fetch crypto prices.
check https://www.coingecko.com/vi/api/documentation for more information.
examples:
default: https://via.placeholder.com/468x60?text=No%20preview%20available
authors:
- dajneem23
supports:
- user
- organization
- repository
scopes: []
inputs:

plugin_crypto:
description: |
Enable crypto plugin
type: boolean
default: no

plugin_crypto_id:
description: |
Crypto id
type: string
default: ""
example: bitcoin

plugin_crypto_vs_currency:
description: |
The target currency of market data (usd, eur, jpy, etc.)
type: string
default: "usd"
example: "usd"

plugin_crypto_days:
description: |
Data up to number of days ago (eg. 1,14,30,max)
type: string
default: "1"
example: 1

plugin_crypto_precision:
description: |
The number of decimal places to use
type: number
default: 2
example: 2


1 change: 1 addition & 0 deletions source/templates/classic/partials/_.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"support",
"stackoverflow",
"leetcode",
"crypto",
"stock",
"achievements",
"screenshot",
Expand Down
68 changes: 68 additions & 0 deletions source/templates/classic/partials/crypto.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<% if (plugins.crypto) { %>
<section>
<h2 class="field">
<img width="20" src="<%= plugins.crypto.logo %>" />
<%= plugins.crypto.symbol ? `${plugins.crypto.symbol?.toUpperCase()}` : "" %>
</h2>
<% if (plugins.crypto.error) { %>
<div class="row fill-width">
<section>
<div class="field error">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16">
<path
fill-rule="evenodd"
d="M2.343 13.657A8 8 0 1113.657 2.343 8 8 0 012.343 13.657zM6.03 4.97a.75.75 0 00-1.06 1.06L6.94 8 4.97 9.97a.75.75 0 101.06 1.06L8 9.06l1.97 1.97a.75.75 0 101.06-1.06L9.06 8l1.97-1.97a.75.75 0 10-1.06-1.06L8 6.94 6.03 4.97z"
></path>
</svg>
<%= plugins.crypto.error.message %>
</div>
</section>
</div>
<% } else { %>
<div class="row">
<section>
<div class="field">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16">
<path
fill-rule="evenodd"
d="M6 2a.75.75 0 01.696.471L10 10.731l1.304-3.26A.75.75 0 0112 7h3.25a.75.75 0 010 1.5h-2.742l-1.812 4.528a.75.75 0 01-1.392 0L6 4.77 4.696 8.03A.75.75 0 014 8.5H.75a.75.75 0 010-1.5h2.742l1.812-4.529A.75.75 0 016 2z"
></path>
</svg>
<%= `${plugins.crypto.current_price.toFixed(plugins.crypto.precision || 2) } ${plugins.crypto.vs_currency?.toUpperCase()}` %>
</div>
</section>
<section>
<div class="field">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16">
<path
fill-rule="evenodd"
d="M4.75 0a.75.75 0 01.75.75V2h5V.75a.75.75 0 011.5 0V2h1.25c.966 0 1.75.784 1.75 1.75v10.5A1.75 1.75 0 0113.25 16H2.75A1.75 1.75 0 011 14.25V3.75C1 2.784 1.784 2 2.75 2H4V.75A.75.75 0 014.75 0zm0 3.5h8.5a.25.25 0 01.25.25V6h-11V3.75a.25.25 0 01.25-.25h2zm-2.25 4v6.75c0 .138.112.25.25.25h10.5a.25.25 0 00.25-.25V7.5h-11z"
></path>
</svg>
<%= plugins.crypto.days %>
</div>
</section>
<section>
<div class="field">
<% if (plugins.crypto.price_change_percentage_24h > 0) { %>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16">
<path
fill-rule="evenodd"
d="M3.47 7.78a.75.75 0 010-1.06l4.25-4.25a.75.75 0 011.06 0l4.25 4.25a.75.75 0 01-1.06 1.06L9 4.81v7.44a.75.75 0 01-1.5 0V4.81L4.53 7.78a.75.75 0 01-1.06 0z"
></path>
</svg>
<% } else { %>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16">
<path
fill-rule="evenodd"
d="M13.03 8.22a.75.75 0 010 1.06l-4.25 4.25a.75.75 0 01-1.06 0L3.47 9.28a.75.75 0 011.06-1.06l2.97 2.97V3.75a.75.75 0 011.5 0v7.44l2.97-2.97a.75.75 0 011.06 0z"
></path>
</svg>
<% } %> <%= plugins.crypto.price_change_percentage_24h ?plugins.crypto.price_change_percentage_24h.toFixed(2) : 0 %>%
</div>
</section>
</div>
<div class="crypto-chart"><%- plugins.crypto.chart %></div>
<% } %>
</section>
<% } %>
1 change: 1 addition & 0 deletions source/templates/repository/partials/_.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"rss",
"screenshot",
"stock",
"crypto",
"contributors",
"sponsors",
"licenses"
Expand Down

1 comment on commit 9f08a38

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented on 9f08a38 Oct 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@check-spelling-bot Report

🔴 Please review

See the 📜action log for details.

Unrecognized words (6)

bitcoin
coingecko
dajneem
eur
jpy
usd

Previously acknowledged words that are now absent Fnc gionkunz panosoft
To accept ✔️ these unrecognized words as correct (and remove the previously acknowledged and now absent words), run the following commands

... in a clone of the [email protected]:lowlighter/metrics.git repository
on the master branch (ℹ️ how do I use this?):

update_files() {
perl -e '
my @expect_files=qw('".github/actions/spelling/expect.txt"');
@ARGV=@expect_files;
my @stale=qw('"$patch_remove"');
my $re=join "|", @stale;
my $suffix=".".time();
my $previous="";
sub maybe_unlink { unlink($_[0]) if $_[0]; }
while (<>) {
if ($ARGV ne $old_argv) { maybe_unlink($previous); $previous="$ARGV$suffix"; rename($ARGV, $previous); open(ARGV_OUT, ">$ARGV"); select(ARGV_OUT); $old_argv = $ARGV; }
next if /^(?:$re)(?:(?:\r|\n)*$| .*)/; print;
}; maybe_unlink($previous);'
perl -e '
my $new_expect_file=".github/actions/spelling/expect.txt";
use File::Path qw(make_path);
use File::Basename qw(dirname);
make_path (dirname($new_expect_file));
open FILE, q{<}, $new_expect_file; chomp(my @words = <FILE>); close FILE;
my @add=qw('"$patch_add"');
my %items; @items{@words} = @words x (1); @items{@add} = @add x (1);
@words = sort {lc($a)."-".$a cmp lc($b)."-".$b} keys %items;
open FILE, q{>}, $new_expect_file; for my $word (@words) { print FILE "$word\n" if $word =~ /\w/; };
close FILE;
system("git", "add", $new_expect_file);
'
}

comment_json=$(mktemp)
curl -L -s -S \
-H "Content-Type: application/json" \
"https://api.github.com/repos/lowlighter/metrics/comments/129403646" > "$comment_json"
comment_body=$(mktemp)
jq -r ".body // empty" "$comment_json" | tr -d "\\r" > $comment_body
rm $comment_json

patch_remove=$(perl -ne 'next unless s{^</summary>(.*)</details>$}{$1}; print' < "$comment_body")

patch_add=$(perl -e '$/=undef; $_=<>; if (m{Unrecognized words[^<]*</summary>\n*```\n*([^<]*)```\n*</details>$}m) { print "$1" } elsif (m{Unrecognized words[^<]*\n\n((?:\w.*\n)+)\n}m) { print "$1" };' < "$comment_body")

update_files
rm $comment_body
git add -u
Warnings (2)

See the 📜action log for details.

ℹ️ Warnings Count
ℹ️ limited-references 1
ℹ️ no-newline-at-eof 1

See ℹ️ Event descriptions for more information.

If the flagged items are false positives

If items relate to a ...

  • binary file (or some other file you wouldn't want to check at all).

    Please add a file path to the excludes.txt file matching the containing file.

    File paths are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your files.

    ^ refers to the file's path from the root of the repository, so ^README\.md$ would exclude README.md (on whichever branch you're using).

  • well-formed pattern.

    If you can write a pattern that would match it,
    try adding it to the patterns.txt file.

    Patterns are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your lines.

    Note that patterns can't match multiline strings.

Please sign in to comment.