File: units_cur

package info (click to toggle)
units 2.26-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,844 kB
  • sloc: ansic: 6,851; python: 969; sh: 903; yacc: 605; makefile: 491; perl: 435
file content (918 lines) | stat: -rwxr-xr-x 29,811 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
#!/usr/bin/python
#
# units_cur for units, a program for updated currency exchange rates
#
# Copyright (C) 2017-2018, 2022, 2026
# Free Software Foundation, Inc
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#    
#
# This program was written by Adrian Mariano (adrianm@gnu.org)
#

version = '5.1'

# Version 5.0:
#
# Rewrite to support multiple different data sources due to disappearance
# of the Yahoo feed.  Includes support for base currency selection. 
#
# Version 4.3: 20 July 2018
#
# Validate rate data from server
#
# Version 4.2: 18 April 2018
#
# Handle case of empty/malformed entry returned from the server
#
# Version 4.1: 30 October 2017
#
# Fixed to include USD in the list of currency codes.  
#
# Version 4: 2 October 2017 
#
# Complete rewrite to use Yahoo YQL API due to removal of TimeGenie RSS feed.
# Switched to requests library using JSON.  One program now runs under
# Python 2 or Python 3.  Thanks to Ray Hamel for some help with this update.  

# Normal imports
import requests
import json
from argparse import ArgumentParser
from collections import OrderedDict
from datetime import date
from os import linesep
from os.path import getmtime
from sys import exit, stderr, stdout
import io, re

datestr = date.today().isoformat()

output_dir = ''

currency_file = output_dir + 'currency.units'
cpi_file = output_dir + 'cpi.units'
metals_file = output_dir + 'metal_prices.units'
crypto_file = output_dir + 'crypto.units'

# valid metals 

validmetals = ['silver','gold','platinum']

PRIMITIVE = '!        # Base unit, the primitive unit of currency'

# This exchange rate table lists the currency ISO 4217 codes, their
# long text names, and any fixed definitions.  If the definition is
# empty then units_cur will query the server for a value.

rate_index = 1
currency = OrderedDict([
    ('ATS', ['austriaschilling',  '1|13.7603 euro']),
    ('BEF', ['belgiumfranc',      '1|40.3399 euro']),
    ('CYP', ['cypruspound',       '1|0.585274 euro']),
    ('EEK', ['estoniakroon',      '1|15.6466 euro # Equal to 1|8 germanymark']),
    ('FIM', ['finlandmarkka',     '1|5.94573 euro']),
    ('FRF', ['francefranc',       '1|6.55957 euro']),
    ('DEM', ['germanymark',       '1|1.95583 euro']),
    ('GRD', ['greecedrachma',     '1|340.75 euro']),
    ('IEP', ['irelandpunt',       '1|0.787564 euro']),
    ('ITL', ['italylira',         '1|1936.27 euro']),
    ('LVL', ['latvialats',        '1|0.702804 euro']),
    ('LTL', ['lithuanialitas',    '1|3.4528 euro']),
    ('LUF', ['luxembourgfranc',   '1|40.3399 euro']),
    ('MTL', ['maltalira',         '1|0.4293 euro']),
    ('SKK', ['slovakiakoruna',    '1|30.1260 euro']),
    ('SIT', ['sloveniatolar',     '1|239.640 euro']),
    ('ESP', ['spainpeseta',       '1|166.386 euro']),
    ('NLG', ['netherlandsguilder','1|2.20371 euro']),
    ('PTE', ['portugalescudo',    '1|200.482 euro']),
    ('CVE', ['capeverdeescudo',   '1|110.265 euro']),
    ('BGN', ['bulgarialev',       '1|1.9558 euro']),
    ('BAM', ['bosniaconvertiblemark','germanymark']),
    ('KMF', ['comorosfranc',      '1|491.96775 euro']),
    ('XOF', ['westafricafranc',   '1|655.957 euro']),
    ('XPF', ['cfpfranc',          '1|119.33 euro']),
    ('XAF', ['centralafricacfafranc','1|655.957 euro']),
    ('AED', ['uaedirham','']),
    ('AFN', ['afghanistanafghani','']),
    ('ALL', ['albanialek','']),
    ('AMD', ['armeniadram','']),
    ('ANG', ['antillesguilder','']),
    ('AOA', ['angolakwanza','']),
    ('ARS', ['argentinapeso','']),
    ('AUD', ['australiadollar','']),
    ('AWG', ['arubaflorin','']),
    ('AZN', ['azerbaijanmanat','']),
    ('BAM', ['bosniaconvertiblemark','']),
    ('BBD', ['barbadosdollar','']),
    ('BDT', ['bangladeshtaka','']),
    ('BGN', ['bulgarialev','']),
    ('BHD', ['bahraindinar','']),
    ('BIF', ['burundifranc','']),
    ('BMD', ['bermudadollar','']),
    ('BND', ['bruneidollar','']),
    ('BOB', ['boliviaboliviano','']),
    ('BRL', ['brazilreal','']),
    ('BSD', ['bahamasdollar','']),
    ('BTN', ['bhutanngultrum','']),
    ('BWP', ['botswanapula','']),
    ('BYN', ['belarusruble','']),
    ('BYR', ['oldbelarusruble','1|10000 BYN']),  
    ('BZD', ['belizedollar','']),
    ('CAD', ['canadadollar','']),
    ('CDF', ['drcfranccongolais','']),
    ('CHF', ['swissfranc','']),
    ('CLP', ['chilepeso','']),
    ('CNY', ['chinayuan','']),
    ('COP', ['colombiapeso','']),
    ('CRC', ['costaricacolon','']),
    ('CUP', ['cubapeso','']),
    ('CVE', ['capeverdeescudo','']),
    ('CZK', ['czechiakoruna','']),
    ('DJF', ['djiboutifranc','']),
    ('DKK', ['denmarkkrone','']),
    ('DOP', ['dominicanrepublicpeso','']),
    ('DZD', ['algeriadinar','']),
    ('EGP', ['egyptpound','']),
    ('ERN', ['eritreanakfa','']),
    ('ETB', ['ethiopiabirr','']),
    ('EUR', ['euro','']),
    ('FJD', ['fijidollar','']),
    ('FKP', ['falklandislandspound','']),
    ('GBP', ['ukpound','']),
    ('GEL', ['georgialari','']),
    ('GHS', ['ghanacedi','']),
    ('GIP', ['gibraltarpound','']),
    ('GMD', ['gambiadalasi','']),
    ('GNF', ['guineafranc','']),
    ('GTQ', ['guatemalaquetzal','']),
    ('GYD', ['guyanadollar','']),
    ('HKD', ['hongkongdollar','']),
    ('HNL', ['honduraslempira','']),
    ('HRK', ['croatiakuna','']),
    ('HTG', ['haitigourde','']),
    ('HUF', ['hungaryforint','']),
    ('IDR', ['indonesiarupiah','']),
    ('ILS', ['israelnewshekel','']),
    ('INR', ['indiarupee','']),
    ('IQD', ['iraqdinar','']),
    ('IRR', ['iranrial','']),
    ('ISK', ['icelandkrona','']),
    ('JMD', ['jamaicadollar','']),
    ('JOD', ['jordandinar','']),
    ('JPY', ['japanyen','']),
    ('KES', ['kenyaschilling','']),
    ('KGS', ['kyrgyzstansom','']),
    ('KHR', ['cambodiariel','']),
    ('KMF', ['comorosfranc','']),
    ('KPW', ['northkoreawon','']),
    ('KRW', ['southkoreawon','']),
    ('KWD', ['kuwaitdinar','']),
    ('KYD', ['caymanislandsdollar','']),
    ('KZT', ['kazakhstantenge','']),
    ('LAK', ['laokip','']),
    ('LBP', ['lebanonpound','']),
    ('LKR', ['srilankarupee','']),
    ('LRD', ['liberiadollar','']),
    ('LSL', ['lesotholoti','']),
    ('LYD', ['libyadinar','']),
    ('MAD', ['moroccodirham','']),
    ('MDL', ['moldovaleu','']),
    ('MGA', ['madagascarariary','']),
    ('MKD', ['macedoniadenar','']),
    ('MMK', ['myanmarkyat','']),
    ('MNT', ['mongoliatugrik','']),  
    ('MOP', ['macaupataca','']),
    ('MRO', ['mauritaniaoldouguiya','1|10 MRU']),
    ('MRU', ['mauritaniaouguiya', '']), 
    ('MUR', ['mauritiusrupee','']),
    ('MVR', ['maldiverufiyaa','']),
    ('MWK', ['malawikwacha','']),
    ('MXN', ['mexicopeso','']),
    ('MYR', ['malaysiaringgit','']),
    ('MZN', ['mozambiquemetical','']),
    ('NAD', ['namibiadollar','']),
    ('NGN', ['nigerianaira','']),
    ('NIO', ['nicaraguacordobaoro','']),
    ('NOK', ['norwaykrone','']),
    ('NPR', ['nepalrupee','']),
    ('NZD', ['newzealanddollar','']),
    ('OMR', ['omanrial','']),
    ('PAB', ['panamabalboa','']),
    ('PEN', ['perunuevosol','']),
    ('PGK', ['papuanewguineakina','']),
    ('PHP', ['philippinepeso','']),
    ('PKR', ['pakistanrupee','']),
    ('PLN', ['polandzloty','']),
    ('PYG', ['paraguayguarani','']),
    ('QAR', ['qatarrial','']),
    ('RON', ['romanianewlei','']),
    ('RSD', ['serbiadinar','']),
    ('RUB', ['russiaruble','']),
    ('RWF', ['rwandafranc','']),
    ('SAR', ['saudiarabiariyal','']),
    ('SBD', ['solomonislandsdollar','']),
    ('SCR', ['seychellesrupee','']),
    ('SDG', ['sudanpound','']),
    ('SEK', ['swedenkrona','']),
    ('SGD', ['singaporedollar','']),
    ('SHP', ['sainthelenapound','']),
#    ('SLL', ['sierraleoneoldleone','1|1000 SLE']),
#    ('SLE', ['sierraleoneleone','']),
    ('SOS', ['somaliaschilling','']),
    ('SRD', ['surinamedollar','']),
    ('SSP', ['southsudanpound','']),
    ('STD', ['saotome&principeolddobra','']),
    ('STN', ['saotome&principedobra','']),
#    ('SVC', ['elsalvadorcolon','']),  # eliminated in 2001
    ('SYP', ['syriapound','']),
    ('SZL', ['swazilandlilangeni','']),
    ('THB', ['thailandbaht','']),
    ('TJS', ['tajikistansomoni','']),
    ('TMT', ['turkmenistanmanat','']),
    ('TND', ['tunisiadinar','']),
    ('TOP', ["tongapa'anga",'']),
    ('TRY', ['turkeylira','']),
    ('TTD', ['trinidadandtobagodollar','']),
    ('TWD', ['taiwandollar','']),
    ('TZS', ['tanzaniashilling','']),
    ('UAH', ['ukrainehryvnia','']),
    ('UGX', ['ugandaschilling','']),
    ('USD', ['US$', '']),
    ('UYU', ['uruguaypeso','']),
    ('UZS', ['uzbekistansum','']),
    ('VEF', ['venezuelabolivarfuerte','']),
    ('VES', ['venezuelabolivarsoberano','']),
    ('VND', ['vietnamdong','']),
    ('VUV', ['vanuatuvatu','']),
    ('WST', ['samoatala','']),
    ('XAF', ['centralafricacfafranc','']),
    ('XCD', ['eastcaribbeandollar','']),
    ('XDR', ['specialdrawingrights','']),
    ('YER', ['yemenrial','']),
    ('ZAR', ['southafricarand','']),
    ('ZMW', ['zambiakwacha','']),
    ('ZWL', ['zimbabwedollar','']),
    ('FOK', ['faroeislandskróna','DKK']),
    ('GGP', ['guernseypound', 'GBP']),
    ('IMP', ['isleofmanpound','GBP']),
    ('JEP', ['jerseypound','GBP']),
    ('KID', ['kiribatidollar','AUD']),
    ('TVD', ['tuvaludollar','AUD']),
])


def validfloat(x):
  try:
    float(x)
    return True
  except ValueError:
    return False


def format_aligned(numbers, suffix=""):
    splitnums = []
    for n in numbers:
        s = f"{n}"
        if '.' not in s:
            # Integer-like: no dot, no fractional part
            splitnums.append((s, None))
        else:
            left, right = s.split('.')
            right = right.rstrip('0') or None  # None if nothing left
            splitnums.append((left, right))
    max_left = max(len(left) for left, right in splitnums)
    max_right = max((len(right) if right else 0) for left, right in splitnums)
    max_right = len(suffix)+max((len(right) if right else 0) for left, right in splitnums)    
    formatted=[]
    for left, right in splitnums:
        if right is None:
            # No decimal point shown, pad right side with spaces
            formatted.append(f"{left:>{max_left}} {suffix:<{max_right+1}}")
        else:
            formatted.append(f"{left:>{max_left}}.{right} {suffix:<{max_right - len(right)}}")
    return formatted

  
def addrate(verbose,form,code,rate):
  if code not in currency.keys():
    if (verbose):
      stderr.write('Got unknown currency with code {}\n'.format(code))
  else:
    if not currency[code][rate_index]:
      if validfloat(rate):
        currency[code][rate_index] = form.format(rate)
      else:
        stderr.write('Got invalid rate "{}" for currency "{}"\n'.format(
                                  rate, code))
    elif verbose:
      if currency[code][rate_index] != form.format(rate):
        stderr.write('Got value "{}" for currency "{}" but '
                   'it is already defined as {}\n'.format(rate, code,
                                                  currency[code][rate_index]))

def getjson(address,args=None):
  try:
    res = requests.get(address,args)
    res.raise_for_status()
    return(res.json())
  except requests.exceptions.RequestException as e:
    stderr.write('Error connecting to currency server:\n{}.\n'.format(e))
    return None

# Write data, returning false on failure and true on success      

def write_data(filename, data):
  try:
      if filename == '-':
        utf8_stdout = io.TextIOWrapper(stdout.buffer, encoding='utf-8')
        utf8_stdout.write(data)
      else:    
        with open(filename, 'w', encoding='utf8') as of:
            of.write(data)
  except IOError as e:
      stderr.write('Unable to write to output file {}:\n{}\n'.format(filename,e))
      return False
  return True
        
########################################################
#
#  Connect to floatrates for currency update
#

def floatrates(verbose,base,dummy):
  webdata = getjson('https://www.floatrates.com/daily/'+base+'.json')
  if not webdata:
    return None
  for index in webdata:
    entry = webdata[index]
    if 'rate' not in entry or 'code' not in entry:    # Skip empty/bad entries
      if verbose:
        stderr.write('Got bad entry from server: '+str(entry)+'\n')
    else:      
      addrate(verbose,'{} '+base,entry['code'],entry['inverseRate'])
  currency[base][rate_index] = PRIMITIVE
  return('FloatRates ('+base+' base)')

########################################################
#
# Connect to European central bank site
#

def eubankrates(verbose,base,dummy):
  if verbose and base!='EUR':
    stderr.write('European bank uses euro for base currency.  Specified base {} ignored.\n'.format(base))
  import xml.etree.ElementTree as ET
  try:
    res=requests.get('https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml')
    res.raise_for_status()
    data = ET.fromstring(res.content)[2][0]
  except requests.exceptions.RequestException as e:
    stderr.write('Error connecting to currency server:\n{}.\n'.
                 format(e))
    return None
  for entry in data.iter():
    if entry.get('time'):
      continue
    rate = entry.get('rate')
    code = entry.get('currency')
    if not rate or not code:                # Skip empty/bad entries
      if verbose:
        stderr.write('Got bad entry from server, code {} and rate {}\n'.format(code,rate))
    else:      
      addrate(verbose,'1|{} euro', code, rate)
  currency['EUR'][rate_index]=PRIMITIVE
  return('the European Central Bank (euro base)')

def is_number(s):
    try:
        float(s) # Tries converting the string to a float
        return True
    except ValueError:
        return False
      
########################################################
#
# Connect to fixer.io (requires API key)
#
# Free API key does not allow changing base currency
# With free key only euro base is supported, and https is not allowed
#
      
def fixer(verbose,base,key):
  if not key:
    stderr.write('API key required for this source\n')
    exit(1)
  if verbose and base!='EUR':
    stderr.write('Fixer uses euro for base currency.  Specified base {} ignored.\n'.format(base))
  webdata = getjson('http://data.fixer.io/api/latest', {'access_key':key})
  if not webdata:
    return None
  if not webdata['success']:
    stderr.write('Currency server error: '+webdata['error']['info'])
    return None
  for code in webdata['rates']:
    addrate(verbose,'1|{} euro', code, webdata['rates'][code])
  currency['EUR'][rate_index] = PRIMITIVE
  return('Fixer (euro base)')

########################################################
#
# Connect to openexchangerates (requires API key)
#
# Free API key does not allow changing the base currency
#
         
def openexchangerates(verbose,base,key):
  if not key:
    stderr.write('API key required for this source\n')
    return None
  if verbose and base!='USD':
    stderr.write('Open Exchange Rates uses US dollar for base currency.  Specified base {} ignored.\n'.format(base))
  webdata = getjson('https://openexchangerates.org/api/latest.json',
                    {'app_id':key}
                    )
  if not webdata:
    return None
  for code in webdata['rates']:
    addrate(verbose,'1|{} US$', code, webdata['rates'][code])
  currency['USD'][rate_index] = PRIMITIVE
  return('open exchange rates (USD base)')

########################################################
#
# Connect to exchangerate-api.com
#
# The open access endpoint gives updates once per day.
# User can optionally supply a paid account's API key to get newer data and more precision.
# Base currency can be changed with both open and paid versions of the API.
#

def exchangerate_api(verbose,base,key):
  if not key:
    webdata = getjson('https://open.er-api.com/v6/latest/'+base)
    if not webdata:
      return None
    if not webdata['result'] or webdata['result'] != 'success':
      stderr.write('Currency server error: '+webdata['error-type']+'\n')
      return None
    for code, rate in webdata['rates'].items() :
      addrate(verbose,'1|{} '+base,code,rate)
  else:
    webdata = getjson('https://v6.exchangerate-api.com/v6/'+key+'/latest/'+base)
    if not webdata['result'] or webdata['result'] != 'success':
      stderr.write('Currency server error: '+webdata['error-type']+'\n')
      exit(1)
    for code, rate in webdata['conversion_rates'].items() :
      addrate(verbose,'1|{} '+base,code,rate)
  currency[base][rate_index] = PRIMITIVE
  return('exchangerate-api.com ('+base+' base)')


#######################################################
#
# list of valid source names and corresponding functions
#

sources = {       
  'exchangerate_api': exchangerate_api,  
  'floatrates': floatrates,
  'eubank' : eubankrates,
  'fixer' : fixer,
  'openexchangerates': openexchangerates,
}

default_currency = 'exchangerate_api'
default_crypto = 'coingecko'

#######################################################
#
# Argument Processing
#

ap = ArgumentParser(
    description="Update currency information for 'units' "
    "into the specified filenames or the default currency "
    "file name {} and CPI file name {}.  The special "
    "filename '-' will send either or both files to "
    "stdout.  The special filename '.' skips output for that file.".format(currency_file,cpi_file),
)

ap.add_argument(
    'currency_file',
    default=currency_file,
    help='the file to update',
    metavar='currency_file',
    nargs='?',
    type=str,
)


ap.add_argument(
    'crypto_file',
    default=crypto_file,
    help='the file to update',
    metavar='crypto_file',
    nargs='?',
    type=str,
)

ap.add_argument(
    'metals_file',
    default=metals_file,
    help='the file to update',
    metavar='metals_file',
    nargs='?',
    type=str,
)

ap.add_argument(
    'cpi_file',
    default=cpi_file,
    help='the file to update',
    metavar='cpi_file',
    nargs='?',
    type=str,
)


ap.add_argument('-c','--crypto',choices=['coingecko','packetizer'],
                default=default_crypto,
                help='set crypto data source (default: {})'.format(default_crypto),
)

ap.add_argument('-V','--version',
                action='version',
                version='%(prog)s version ' + version,
                help='display units_cur version',
)

ap.add_argument('-v','--verbose',
                action='store_true',
                help='display details when fetching currency data',
)

ap.add_argument('-s','--source',choices=list(sources.keys()),
                default=default_currency, 
                help='set currency data source (default: {})'.format(default_currency),
)

ap.add_argument('-b','--base',default='USD',
                help='set the base currency (when allowed by source).  BASE should be a 3 letter ISO currency code, e.g. USD.  The specified currency will be the primitive currency unit used by units.  Only the floatrates source supports this option.',
)                

ap.add_argument('-k','--key',default='',
                help='set API key for sources that require it'
)

ap.add_argument('--blskey',default='',
                help='set BLS key for fetching CPI data'
)


args = ap.parse_args()  
currency_file = args.currency_file
cpi_file = args.cpi_file
metals_file = args.metals_file
crypto_file = args.crypto_file
verbose = args.verbose
source = args.source
crypto = args.crypto
base = args.base
apikey = args.key
cpikey = args.blskey


def fetch_currency():
  if currency_file=='.':
    return False
  if base not in currency.keys():
    stderr.write('Base currency {} is not a known currency code.\n'.format(base))
    return False

  # Fetch currency data from specified currency source

  sourcename = sources[source](verbose,base,apikey)

  if not sourcename:
    return False
              
  # Delete currencies where we have no rate data
  for code in list(currency.keys()):
    if not currency[code][rate_index]:
      if verbose:
        stderr.write('No data for {}\n'.format(code))
      del currency[code]

  cnames = [currency[code][0] for code in currency.keys()]
  crates = [currency[code][1] for code in currency.keys()]

  codestr = '\n'.join('{:23}{}'.
     format(code, name) for (code,name) in zip(currency.keys(), cnames))

  maxlen = max(len(name) for name in cnames) + 2

  ratestr = '\n'.join(
      '{:{}}{}'.format(name, maxlen, rate) for (name, rate) in zip(cnames, crates)
      )

  currency_text = (
"""# ISO Currency Codes

{codestr}

# Currency exchange rates 

!message Currency exchange rates from {sourcename} on {datestr}

{ratestr}

""".format(codestr=codestr, datestr=datestr, ratestr=ratestr, sourcename=sourcename)
  ).replace('\n', linesep)
  if not write_data(currency_file,currency_text):
    return False
  return True


def fetch_metals():
  if metals_file=='.':
    return False
  
  # Get precious metals data and bitcoin

  metals = getjson('https://services.packetizer.com/spotprices/',{'f':'json'})

  if not metals:
    return None
  
  metallist = ['']*len(validmetals)
  for metal, price in metals.items():
    if metal in validmetals:
      metalindex = validmetals.index(metal)
      if validfloat(price):
        if not metallist[metalindex]:
          metallist[validmetals.index(metal)] = '{:19}{} US$/troyounce'.format(
                                                           metal + 'price', price)
        elif verbose:
          stderr.write('Got value "{}" for metal "{}" but '
                       'it is already defined\n'.format(price,metal))
      else:
        stderr.write('Got invalid rate "{}" for metal "{}"\n'.format(price,metal))
    elif metal != 'date' and verbose:  # Don't print a message for the "date" entry
      stderr.write('Got unknown metal "{}" with value "{}"\n'.format(metal,price))
  metalrates = '\n'.join(metallist)
  metalstr = ("""# Precious metals prices from Packetizer (services.packetizer.com/spotprices)

!message Precious metals prices from {source} on {datestr}

{metalrates}

""".format(datestr=datestr, metalrates=metalrates, source="packetizer.com")
               ).replace('\n', linesep)
  if not write_data(metals_file, metalstr):
    return False
  return True


########################################################
#
# Get crypto (just bitcoin) from packetizer
#

def crypto_packetizer():
  bitcoin = getjson('https://services.packetizer.com/btc/',{'f':'json'})
  if not bitcoin:
    return False

  if validfloat(bitcoin['usd']):
    bitcoinstr = '{:{}}{} US$\n'.format(
                  'bitcoin',20,bitcoin['usd'])
  else:
    stderr.write('Got invalid bitcoin rate "{}"\n', bitcoint['usd'])
    return False
  
  cryptorates="""# From services.packetizer.com/btc

!message Crypto prices from {source} on {datestr}  

BTC                 bitcoin
  
{bitcoinstr}
""".format(bitcoinstr=bitcoinstr,datestr=datestr,source="packetizer.com").replace('\n', linesep)
  return cryptorates



########################################################
#
# Get crypto from coingecko
#

def crypto_coingecko():
    url = "https://api.coingecko.com/api/v3/coins/markets"

    def printnum(n):
        n = float(n)
        if n<1e6:
            return '{}'.format(n)
        elif n<1e9:
            return '{} million'.format(round(n/1e6))
        elif n<1e10:
            return '{} billion'.format(round(n/1e7)/100)
        elif n<1e11:
            return '{} billion'.format(round(n/1e8)/10)
        else:
            return '{} billion'.format(round(n/1e9))
    
    # CoinGecko IDs for important cryptocurrencies
    important_coins = [
        # Top tier
        'bitcoin', 'ethereum', 'tether', 'usd-coin',
        # Major platforms and tokens
        'binancecoin', 'solana', 'ripple', 'cardano', 'dogecoin',
        'polkadot', 'avalanche-2', 'chainlink', 'polygon-ecosystem-token',
        'litecoin', 'bitcoin-cash', 'stellar', 'monero',
        # DeFi
        'uniswap', 'aave', 'maker', 'dai',
        # Layer 2
        'arbitrum', 'optimism',
        # Other notable
        'shiba-inu', 'cosmos', 'algorand', 'hedera-hashgraph',
        'tron', 'ethereum-classic', 'filecoin', 'aptos'
    ]

    # CoinGecko allows comma-separated IDs
    ids_param = ','.join(important_coins)
    
    all_coins = []
    per_page = 250  # CoinGecko allows up to 250 per page
    
    params = {
        'ids': ids_param, 
        'vs_currency': 'usd',
        'order': 'market_cap_desc',
        #'per_page': per_page,
        #'page': 1,
        'sparkline': 'false'
    }
    
    try:
        response = requests.get(url, params=params, timeout=10)
        response.raise_for_status()
        coins = response.json()
        name = []
        value = []
        capitalization = []
        symbol=[]
        
        for coin in coins:
            cname = coin['name'].lower()
            cname = re.sub(r' *\(.*\)', '', cname)
            cname = re.sub(r'[^a-z]', '_', cname)
            name.append(cname)
            symbol.append(coin['symbol'].upper())
            value.append(coin['current_price'])
            capitalization.append(round(float(coin['market_cap'])))
        formatval = format_aligned(value,'US$')
        codestr = '\n'.join('{:23} {}'.format(name,code)
                          for (code,name) in zip(name,symbol))
        ratestr = '\n'.join(
            '{:23} {}    # ${}'.format(
                n,val,printnum(cap)) for (n,val,cap) in zip(name,formatval,capitalization))
    except requests.exceptions.RequestException as e:
        stderr.write("Error fetching data from {url}: {e}\n".format(url=url,e=e))
        return False
    cryptorates="""# From {url}

!message Crypto prices from {source} on {datestr}  

{codestr}

# Selected crypto values with market capitalization

{ratestr}    
""".format(codestr=codestr, source="coingecko.com", datestr=datestr,
           ratestr=ratestr,url=url).replace('\n', linesep)
    return cryptorates
        

cryptosources = {
  'coingecko': crypto_coingecko,
  'packetizer': crypto_packetizer,
}

  
def fetch_crypto():
  if crypto_file=='.':
    return False
  cryptorates = cryptosources[crypto]()

  if not write_data(crypto_file, cryptorates):
    return False
  return True


def fetch_cpi():
  if cpi_file=='.':
    return False
  docpi=False
  if cpi_file=='-':
    docpi=True
  else:
    try:
      filedate = getmtime(cpi_file)
      filedate = date.fromtimestamp(filedate)
      today = date.today()
      dmonth = (today.month-filedate.month) + 12*(today.year-filedate.year)
      if dmonth>1 or (dmonth==1 and today.day>18):
        docpi=True
    except FileNotFoundError:
      docpi=True
  # Skipping CPI but not due to an error
  if not docpi:
    return True
  headers = {'Content-type': 'application/json'}
  yearlist = list(range(date.today().year,1912,-10))
  if yearlist[-1]>1912:
    yearlist.append(1912)
  cpi=[]
  lastcpi = 0
  query = {"seriesid": ['CUUR0000SA0']}
  if cpikey:
    query["registrationkey"]=cpikey
  for endyear in range(len(yearlist)-1):
    query["startyear"]=str(yearlist[endyear+1]+1)
    query["endyear"]=str(yearlist[endyear])
    data = json.dumps(query)
    try:
      p = requests.post('https://api.bls.gov/publicAPI/v2/timeseries/data/', data=data, headers=headers)
      json_data = json.loads(p.text)
    except requests.exceptions.RequestException as e:
      stderr.write('Error connecting to cpi server:\n{}.\n'.format(e))
      return False
    if json_data['status']=="REQUEST_NOT_PROCESSED":
      stderr.write("Unable to update CPI data: Exceeded daily threshold for BLS requests\n")
      return False
    for series in json_data['Results']['series']:
        for item in series['data']:
            if not ('M01' <= item['period'] <= 'M12'):
                continue
            year = int(item['year']) + int(item['period'][1:])/12
            value = item['value']
            if is_number(value):
              cpi.append(' '*10 + "{} {} \\".format(year,value))
              if lastcpi==0:
                lastcpi=value
                lastyear=year
            firstcpi=value
            firstyear=year
  cpi.reverse()
  cpistr = '\n'.join(cpi)

  cpi_text = ("""!message Consumer price index data from US BLS, {datestr}

UScpi[1] noerror \\
{cpistr}

UScpi_now          {lastcpi}
UScpi_lastdate     {lastyear}

USdollars_in(date) units=[1;$] domain=[{firstyear},{lastyear}] \\
                               range=[1,{maxinfl}] \\
                               US$ UScpi_now / UScpi(date) ;\\
                               ~UScpi(US$ UScpi_now / USdollars_in)
USinflation_since(date) units=[1;1] domain=[{firstyear},{lastyear}] \\
                               range=[1,{maxinfl}] \\
                                UScpi_now / UScpi(date) ;\\
                               ~UScpi(UScpi_now / USinflation_since)

""".format(datestr=datestr,cpistr=cpistr,maxinfl=float(lastcpi)/float(firstcpi),lastcpi=lastcpi,
           firstyear=firstyear,lastyear=lastyear)
  ).replace('\n', linesep)
  write_data(cpi_file,cpi_text)

  
##################################################

if not fetch_currency():
  stderr.write('Currency file not written\n')
if not fetch_crypto():
  stderr.write('Crypto file not written\n')
if not fetch_metals():
  stderr.write('Precious metals file not written\n')
fetch_cpi()