Skip to content

Instantly share code, notes, and snippets.

@kei-sato
Last active August 29, 2015 14:24

Revisions

  1. Kei Sato renamed this gist Jul 13, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. Kei Sato revised this gist Jul 13, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion usdex.py
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    #!/usr/bin/env python
    #coding:utf-8

    # convert usd to any currency, or any currency to usd
    # Exchange usd to any currency, or any currency to usd
    # help: python convert_usd.py -h

    import os.path
  3. Kei Sato renamed this gist Jul 13, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. Kei Sato revised this gist Jul 13, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions convert_usd.py
    100644 → 100755
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    #!/usr/bin/env python
    #coding:utf-8

    # convert usd to any currency, or any currency to usd
  5. Kei Sato revised this gist Jul 13, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions convert_usd.py
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    #coding:utf-8

    # convert usd to any currency, or vise versa
    # help: python usd.py -h
    # convert usd to any currency, or any currency to usd
    # help: python convert_usd.py -h

    import os.path
    from datetime import date, datetime
  6. Kei Sato revised this gist Jul 13, 2015. 1 changed file with 10 additions and 10 deletions.
    20 changes: 10 additions & 10 deletions convert_usd.py
    Original file line number Diff line number Diff line change
    @@ -23,16 +23,6 @@ def hasCache():
    return False


    def fetchRates():
    # fetch rate list from remote
    response = urllib.urlopen(URL)
    body = response.read()

    writeCache(body)

    return body


    def readCache():
    with open(FPATH, 'r') as f:
    body = f.read()
    @@ -44,6 +34,16 @@ def writeCache(body):
    f.write(body)


    def fetchRates():
    # fetch rate list from remote
    response = urllib.urlopen(URL)
    body = response.read()

    writeCache(body)

    return body


    if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='convert usd to any currency.')
    parser.add_argument('-p', '--price', nargs='?', type=float, help='price', default=1.0)
  7. kei-sato revised this gist Jul 13, 2015. 1 changed file with 0 additions and 3 deletions.
    3 changes: 0 additions & 3 deletions convert_usd.py
    Original file line number Diff line number Diff line change
    @@ -57,9 +57,6 @@ def writeCache(body):
    reverse = args.reverse
    verbosity = args.verbosity

    if verbosity:
    print "verbosity:", verbosity

    if hasCache():
    body = readCache()
    else:
  8. kei-sato created this gist Jul 13, 2015.
    82 changes: 82 additions & 0 deletions convert_usd.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,82 @@
    #coding:utf-8

    # convert usd to any currency, or vise versa
    # help: python usd.py -h

    import os.path
    from datetime import date, datetime
    import argparse
    import urllib
    import json

    URL = "http://api.aoikujira.com/kawase/json/usd"
    FPATH = "/tmp/exchange"


    def hasCache():
    if os.path.isfile(FPATH):
    d = date.today()
    today = datetime.combine(d, datetime.min.time())
    mtime = datetime.fromtimestamp(os.path.getmtime(FPATH))
    if mtime > today:
    return True
    return False


    def fetchRates():
    # fetch rate list from remote
    response = urllib.urlopen(URL)
    body = response.read()

    writeCache(body)

    return body


    def readCache():
    with open(FPATH, 'r') as f:
    body = f.read()
    return body


    def writeCache(body):
    with open(FPATH, 'w') as f:
    f.write(body)


    if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='convert usd to any currency.')
    parser.add_argument('-p', '--price', nargs='?', type=float, help='price', default=1.0)
    parser.add_argument('-c', '--currency', nargs='?', help='currency', default=u'JPY')
    parser.add_argument('-r', '--reverse', action='store_true', help='reverse the direction')
    parser.add_argument('-v', '--verbosity', action='count', help='increase output verbosity')

    args = parser.parse_args()
    price = args.price
    currency = args.currency
    reverse = args.reverse
    verbosity = args.verbosity

    if verbosity:
    print "verbosity:", verbosity

    if hasCache():
    body = readCache()
    else:
    body = fetchRates()

    data = json.loads(body)
    rate = float(data[currency])

    if reverse:
    if verbosity >= 1:
    print "{0}({2}) => {1}(USD)".format(1, 1 / rate, currency)
    print "{0}({2}) => {1}(USD)".format(price, price / rate, currency)
    else:
    print price / rate
    else:
    if verbosity >= 1:
    print "{0}(USD) => {1}({2})".format(1, rate, currency)
    print "{0}(USD) => {1}({2})".format(price, price * rate, currency)
    else:
    print price * rate