main_ticker.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, a command-line cryptocurrency wallet
  4. # Copyright (C)2013-2022 The MMGen Project <mmgen@tuta.io>
  5. # Licensed under the GNU General Public License, Version 3:
  6. # https://www.gnu.org/licenses
  7. # Public project repositories:
  8. # https://github.com/mmgen/mmgen https://github.com/mmgen/mmgen-node-tools
  9. # https://gitlab.com/mmgen/mmgen https://gitlab.com/mmgen/mmgen-node-tools
  10. """
  11. mmnode-ticker: Display price information for cryptocurrency and other assets
  12. """
  13. opts_data = {
  14. 'sets': [
  15. ('wide', True, 'percent_change', True),
  16. ('wide', True, 'name_labels', True),
  17. ('wide', True, 'thousands_comma', True),
  18. ('wide', True, 'update_time', True),
  19. ],
  20. 'text': {
  21. 'desc': 'Display prices for cryptocurrency and other assets',
  22. 'usage': '[opts] [TRADE_SPECIFIER]',
  23. 'options': """
  24. -h, --help Print this help message
  25. --, --longhelp Print help message for long options (common options)
  26. -A, --adjust=P Adjust prices by percentage ‘P’. In ‘trading’ mode,
  27. spot and adjusted prices are shown in separate columns.
  28. -b, --btc Fetch and display data for Bitcoin only
  29. -c, --add-columns=LIST Add columns for asset specifiers in LIST (comma-
  30. separated, see ASSET SPECIFIERS below). Can also be
  31. used to supply a USD exchange rate for missing assets.
  32. -C, --cached-data Use cached data from previous network query instead of
  33. live data from server
  34. -D, --cachedir=D Read and write cached JSON data to directory ‘D’
  35. instead of ‘~/{dfl_cachedir}’
  36. -d, --download=D Retrieve data ‘D’ from source, save to file and exit
  37. (valid options: {ds})
  38. -e, --add-precision=N Add ‘N’ digits of precision to columns
  39. -E, --elapsed Show elapsed time in UPDATED column (see --update-time)
  40. -F, --portfolio Display portfolio data
  41. -l, --list-ids List IDs of all available assets
  42. -n, --name-labels Label rows with asset names rather than symbols
  43. -p, --percent-change Add percentage change columns
  44. -P, --pager Pipe the output to a pager
  45. -r, --add-rows=LIST Add rows for asset specifiers in LIST (comma-separated,
  46. see ASSET SPECIFIERS below). Can also be used to supply
  47. a USD exchange rate for missing assets.
  48. -t, --testing Print command to be executed to stdout and exit
  49. -T, --thousands-comma Use comma as a thousands separator
  50. -u, --update-time Include UPDATED (last update time) column
  51. -v, --verbose Be more verbose
  52. -w, --wide Display all optional columns (equivalent to -punT)
  53. -x, --proxy=P Connect via proxy ‘P’. Set to the empty string to
  54. completely disable or ‘none’ to allow override from
  55. environment. Consult the curl manpage for --proxy usage.
  56. """,
  57. 'notes': """
  58. The script has two display modes: ‘overview’, the default, and ‘trading’, the
  59. latter being enabled when a TRADE_SPECIFIER argument (see below) is supplied
  60. on the command line.
  61. Overview mode displays prices of all configured assets, and optionally the
  62. user’s portfolio, while trading mode displays the price of a given quantity
  63. of an asset in relation to other assets, optionally comparing an offered
  64. price to the spot price.
  65. ASSETS consist of either a symbol (e.g. ‘xmr’) or full ID (see --list-ids)
  66. consisting of symbol plus label (e.g. ‘xmr-monero’). In cases where the
  67. symbol is ambiguous, the full ID must be used. Examples:
  68. chf - specify asset by symbol
  69. chf-swiss-franc-token - same as above, but use full ID instead of symbol
  70. ASSET SPECIFIERS have the following format:
  71. ASSET[:RATE[:RATE_ASSET]]
  72. If the asset referred to by ASSET is not in the source data (see --list-ids),
  73. an arbitrarily chosen label may be used. RATE is the exchange rate of the
  74. asset in relation to RATE_ASSET, if present, otherwise USD. When RATE is
  75. postfixed with the letter ‘r’, its meaning is reversed, i.e. interpreted as
  76. ‘ASSET/RATE_ASSET’ instead of ‘RATE_ASSET/ASSET’. Asset specifier examples:
  77. inr:79.5 - INR is not in the source data, so supply rate of
  78. 79.5 INR to the Dollar (USD/INR)
  79. inr:0.01257r - same as above, but use reverse rate (INR/USD)
  80. inr-indian-rupee:79.5 - same as first example, but add an arbitrary label
  81. omr-omani-rial:2.59r - Omani Rial is pegged to the Dollar at 2.59 USD
  82. bgn-bg-lev:0.5113r:eur - Bulgarian Lev is pegged to the Euro at 0.5113 EUR
  83. A TRADE_SPECIFIER is a single argument in the format:
  84. ASSET:AMOUNT[:TO_ASSET[:TO_AMOUNT]]
  85. Examples:
  86. xmr:17.34 - price of 17.34 XMR in all configured assets
  87. xmr-monero:17.34 - same as above, but with full ID
  88. xmr:17.34:eur - price of 17.34 XMR in EUR only
  89. xmr:17.34:eur:2800 - commission on an offer of 17.34 XMR for 2800 EUR
  90. TO_AMOUNT, if included, is used to calculate the percentage difference or
  91. commission on an offer compared to the spot price.
  92. If either ASSET or TO_ASSET refer to assets not present in the source data,
  93. a USD rate for the missing asset(s) must be supplied via the --add-columns
  94. or --add-rows options.
  95. PROXY NOTE
  96. The remote server used to obtain the price data, {cc.api_host!r}, blocks
  97. Tor behind a Captcha wall, so a Tor proxy cannot be used directly. If you’re
  98. concerned about privacy, connect via a VPN, or better yet, VPN over Tor. Then
  99. set up an HTTP proxy (e.g. Privoxy) on the VPN’ed host and set the ‘proxy’
  100. option in the config file or --proxy on the command line accordingly. Or run
  101. the script directly on the VPN’ed host with ’proxy’ or --proxy set to the
  102. null string.
  103. Alternatively, you may download the JSON source data in a Tor-proxied browser
  104. from ‘{cc.api_url}’, save it as ‘ticker.json’ in your
  105. configured cache directory, and run the script with the --cached-data option.
  106. RATE LIMITING NOTE
  107. To protect user privacy, all filtering and processing of data is performed
  108. client side so that the remote server does not know which assets are being
  109. examined. This means that data for ALL available assets (currently over 4000)
  110. is fetched with each invocation of the script. A rate limit of {cc.ratelimit} seconds
  111. between calls is thus imposed to prevent abuse of the remote server. When the
  112. --btc option is in effect, this limit is reduced to {cc.btc_ratelimit} seconds. To bypass the
  113. rate limit entirely, use --cached-data.
  114. EXAMPLES
  115. # Basic display in ‘overview’ mode:
  116. $ mmnode-ticker
  117. # Display BTC price only:
  118. $ mmnode-ticker --btc
  119. # Wide display, add EUR and OMR columns, OMR/USD rate, extra precision and
  120. # proxy:
  121. $ mmnode-ticker -w -c eur,omr-omani-rial:2.59r -e2 -x http://vpnhost:8118
  122. # Wide display, elapsed update time, add EUR, BGN columns and BGN/EUR rate:
  123. $ mmnode-ticker -wE -c eur,bgn-bulgarian-lev:0.5113r:eur
  124. # Wide display, use cached data from previous network query, show portfolio
  125. # (see above), pipe output to pager, add DOGE row:
  126. $ mmnode-ticker -wCFP -r doge
  127. # Display 17.234 XMR priced in all configured assets (‘trading’ mode):
  128. $ mmnode-ticker xmr:17.234
  129. # Same as above, but add INR price at specified USDINR rate:
  130. $ mmnode-ticker -c inr:79.5 xmr:17.234
  131. # Same as above, but view INR price only at specified rate, adding label:
  132. $ mmnode-ticker -c inr-indian-rupee:79.5 xmr:17.234:inr
  133. # Calculate commission on an offer of 2700 USD for 0.123 BTC, compared to
  134. # current spot price:
  135. $ mmnode-ticker usd:2700:btc:0.123
  136. # Calculate commission on an offer of 200000 INR for 0.1 BTC, compared to
  137. # current spot price, at specified USDINR rate:
  138. $ mmnode-ticker -n -c inr-indian-rupee:79.5 inr:200000:btc:0.1
  139. CONFIGURED ASSETS:
  140. {assets}
  141. Customize output by editing the file
  142. ~/{cfg}
  143. To add a portfolio, edit the file
  144. ~/{pf_cfg}
  145. """
  146. },
  147. 'code': {
  148. 'options': lambda s: s.format(
  149. dfl_cachedir = os.path.relpath(dfl_cachedir,start=homedir),
  150. ds = fmt_dict(DataSource.sources,fmt='equal'),
  151. ),
  152. 'notes': lambda s: s.format(
  153. assets = fmt_list(assets_list_gen(cfg_in),fmt='col',indent=' '),
  154. cfg = os.path.relpath(cfg_in.cfg_file,start=homedir),
  155. pf_cfg = os.path.relpath(cfg_in.portfolio_file,start=homedir),
  156. cc = src_cls['cc'](),
  157. )
  158. }
  159. }
  160. import os
  161. from mmgen.util import fmt_list,fmt_dict
  162. from mmgen.cfg import Config
  163. import mmgen_node_tools.Ticker as tck
  164. tck.gcfg = Config( opts_data=opts_data, do_post_init=True )
  165. tck.make_cfg()
  166. from .Ticker import dfl_cachedir,homedir,DataSource,assets_list_gen,cfg_in,src_cls
  167. tck.gcfg._post_init()
  168. tck.main()