44"""
55
66import argparse
7- from couchdb import Server
7+ from ibmcloudant import CouchDbSessionAuthenticator , cloudant_v1
88import datetime
99import json
1010import requests
@@ -37,10 +37,16 @@ def get_rate(self, currency):
3737 return 1 / self .rates [currency ]
3838
3939
40- def get_current (db , item ):
41- rows = db .view ("entire_document/by_date" , descending = True , limit = 1 ).rows
40+ def get_current (db_conn , item ):
41+ rows = db_conn .post_view (
42+ db = "pricing_exchange_rates" ,
43+ ddoc = "entire_document" ,
44+ view = "by_date" ,
45+ descending = True ,
46+ limit = 1
47+ ).get_result ()["rows" ]
4248 if len (rows ) != 0 :
43- value = rows [0 ]. value
49+ value = rows [0 ][ " value" ]
4450 return value [item ]
4551 return None
4652
@@ -77,20 +83,18 @@ def main(config, fixer_io_config, push_to_server=False):
7783 with open (config ) as settings_file :
7884 server_settings = yaml .load (settings_file , Loader = yaml .SafeLoader )
7985
80- url_string = 'https://{}:{}@{}' .format (
81- server_settings ['statusdb' ].get ('username' ),
82- server_settings ['statusdb' ].get ('password' ),
83- server_settings ['statusdb' ].get ('url' )
84- )
85- couch = Server (url_string )
86-
87- db = couch ['pricing_exchange_rates' ]
86+ couch = cloudant_v1 .CloudantV1 (
87+ authenticator = CouchDbSessionAuthenticator (
88+ server_settings ["statusdb" ].get ("username" ), server_settings ["statusdb" ].get ("password" )
89+ )
90+ )
91+ couch .set_service_url (server_settings ["statusdb" ].get ("url" ))
8892
8993 # Check that new is not too strange compared to current.
9094 # This is a safety measure so that we have lower risk of having erroneus
9195 # exchange rates in the db.
92- current_usd = get_current (db , 'USD_in_SEK' )
93- current_eur = get_current (db , 'EUR_in_SEK' )
96+ current_usd = get_current (couch , 'USD_in_SEK' )
97+ current_eur = get_current (couch , 'EUR_in_SEK' )
9498
9599 check_financial_crisis (current_usd , usd_to_sek , 'USD' )
96100 check_financial_crisis (current_eur , eur_to_sek , 'EUR' )
@@ -105,7 +109,10 @@ def main(config, fixer_io_config, push_to_server=False):
105109 raise Exception ("Super stable currencies? Stale api would be my guess." )
106110
107111 if push_to_server :
108- db .save (doc )
112+ couch .post_document (
113+ db = "pricing_exchange_rates" ,
114+ document = doc
115+ )
109116 else :
110117 print (doc )
111118
0 commit comments