get_exchange_rates.py 3.5 KB
Newer Older
Solt Budavári's avatar
Solt Budavári committed
1
#! /usr/bin/python3
2 3
## Download the specified currency exchange rates and
## save the data to the specified file.
Solt Budavári's avatar
Solt Budavári committed
4

Solt Budavári's avatar
Solt Budavári committed
5 6
## Copyright 2019 Solt Budavári

Solt Budavári's avatar
Solt Budavári committed
7 8
## SPDX-License-Identifier: GPL-3.0-or-later

9 10 11 12 13 14 15 16 17 18 19 20
## 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, see <https://www.gnu.org/licenses/>.
Solt Budavári's avatar
Solt Budavári committed
21

22 23 24 25 26 27 28 29 30 31 32
## The following is a sample config file for this script;
## adapt it to your needs.
##
## --- # Api key and settings for Open Exchange Rates currency script
## api_key: 0123456789abcdef
## base_url: https://openexchangerates.org/api/latest.json
## wanted_currencies: [ AUD, CAD, EUR, GBP ]
## target_file: ~/Documents/currency_rates.csv
## ...


33
import csv, json, os.path, requests, time, yaml
Solt Budavári's avatar
Solt Budavári committed
34 35 36 37


def main():
    
38 39 40 41 42
    SETTINGS_FILE = os.path.expanduser( "~/etc/get_exchange_rates.yaml" )
    
    ## Get the needed arguments from the given YAML file
    
    with open( SETTINGS_FILE, "r" ) as sf:
43
        args = yaml.load( sf, Loader = yaml.SafeLoader )
44 45 46 47 48
    
    API_KEY = args[ "api_key" ]
    BASE_URL = args[ "base_url" ]
    WANTED_CURRENCIES = ",".join( args[ "wanted_currencies" ] )
    TARGET_FILE = os.path.expanduser( args[ "target_file" ] )
49
    
Solt Budavári's avatar
Solt Budavári committed
50 51
    ## Download the exchange rate for the desired currencies
    currency_data = download_currency_data( BASE_URL + \
52
                                            "?app_id=" + API_KEY + \
Solt Budavári's avatar
Solt Budavári committed
53 54
                                            "&symbols=" + WANTED_CURRENCIES )
    
55
    ## Assemble the row (a tuple) that we will write to file
56
    row = create_row( currency_data )
Solt Budavári's avatar
Solt Budavári committed
57
    
58 59 60
    ## Write all the exchange data (the row) to file
    write_row_to_file( TARGET_FILE, row )

Solt Budavári's avatar
Solt Budavári committed
61 62 63 64 65 66 67 68 69 70 71 72

def download_currency_data( url ):
    req = requests.get( url )
    
    if req.status_code == requests.codes.ok:
        result = json.loads( req.text )
    else:
        req.raise_for_status()
    
    return result


73 74 75 76 77 78 79 80 81 82
def create_row( data ):
    ## Calculate some needed exchange rates from the downloaded data, and
    ## convert the Unix-time timestamp to human-readable format
    
    eur_to_huf = data[ "rates" ][ "HUF" ] /\
                 data[ "rates" ][ "EUR" ]
    
    gbp_to_huf = data[ "rates" ][ "HUF" ] /\
                 data[ "rates" ][ "GBP" ]
    
83 84 85
    isk_to_huf = data[ "rates" ][ "HUF" ] /\
                 data[ "rates" ][ "ISK" ]
    
86 87 88 89 90 91
    usd_to_huf = data[ "rates" ][ "HUF" ]
    
    raw_time = data[ "timestamp" ]
    nice_time = time.strftime( "%Y.%m.%d. %H:%M:%S",
                               time.localtime( raw_time ) )
    
92
    return tuple( [ raw_time, nice_time, eur_to_huf, gbp_to_huf, isk_to_huf, usd_to_huf ] )
93 94 95 96 97


def write_row_to_file( file, row ):
    with open( file, "a", newline = "\n" ) as csvfile:
        csv_writer = csv.writer( csvfile, \
98
                                 dialect = "unix", \
99 100 101 102 103 104 105 106 107
                                 delimiter = ";", \
                                 quotechar = '"',
                                 quoting = csv.QUOTE_MINIMAL )
        
        csv_writer.writerow( row )
    
    return None


Solt Budavári's avatar
Solt Budavári committed
108 109
if __name__ == "__main__":
    main()