3 # Copyright 2021 Oskar Sharipov <oskarsh[at]riseup[dot]net
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
22 from requests
.exceptions
import ConnectionError
, Timeout
, TooManyRedirects
24 logging
.basicConfig(format
="%(asctime)s - %(levelname)s - %(message)s")
25 BASE_URL
= "https://pro-api.coinmarketcap.com/v1/{}"
28 Every element must be the same structure:
31 "the string used in \"currency costs {} it was expected\"",
32 lambda asked, from_cap: whether_approaches(asked, from_cap)),
37 ">": ("more than", lambda asked
, from_cap
: from_cap
> asked
),
38 "<": ("less than", lambda asked
, from_cap
: from_cap
< asked
),
42 def currencies(key
: str):
43 headers
= {"Accepts": "application/json", "X-CMC_PRO_API_KEY": key
}
44 session
= requests
.Session()
45 session
.headers
.update(headers
)
47 url
= BASE_URL
.format("cryptocurrency/listings/latest?limit=5000")
49 response
= session
.get(url
)
53 'Couldn\'t fetch currency listings. Possibly "key" is invalid.'
57 except (ConnectionError
, Timeout
, TooManyRedirects
) as e
:
58 logging
.error("Couldn't fetch currency listings: %s.", e
)
62 def cmp_function(s
: str):
64 than_what
= float(s
[1:])
65 then_what
, then_how
= WHEN_TRANSLATER
[s
[0]]
69 + " it was expected. Its current price is {price:.4f} USD."
71 f
= lambda from_cap
: then_what
if then_how(than_what
, from_cap
) else None
73 except (IndexError, ValueError, KeyError):
74 logging
.error('Asked "when" is a trash: "%s".', s
)
80 document
= hjson
.load(sys
.stdin
)
83 except hjson
.scanner
.HjsonDecodeError
as e
:
84 logging
.error("Invalid syntax in stdin: %s", e
)
87 logging
.error('No "key" or "watch" cell in stdin.')
90 listings
= currencies(document
["key"])
94 watching
= document
["watch"]
95 functions
= [None] * len(watching
)
96 for i
, w
in enumerate(watching
):
97 functions
[i
] = cmp_function(w
["when"])
100 for currency
in listings
:
101 for i
in range(len(watching
)):
102 if watching
[i
]["symbol"] != currency
["symbol"]:
105 s
= functions
[i
](currency
["quote"]["USD"]["price"])
108 "Fetched currency listings are ugly. Where is the price placed?\n%s",
109 hjson
.dumps(currency
),
114 symbol
=currency
["symbol"], price
=currency
["quote"]["USD"]["price"]
122 if __name__
== "__main__":
125 logging
.info("Terminating...")