11from requests_oauthlib import OAuth1Session
22from prettytable import PrettyTable
33from json import loads , JSONDecodeError
4- from colorama import init ,deinit
4+ from colorama import init , deinit
55import ruamel .yaml
66
77path_to_inventory = "config/inventory.yaml"
@@ -14,10 +14,12 @@ def create_session(url, app_token, app_secret, access_token, access_token_secret
1414
1515 return OAuth1Session (app_token , client_secret = app_secret , resource_owner_key = access_token , resource_owner_secret = access_token_secret , realm = url )
1616
17+
1718def print_header ():
18- print ("+------------------------------------------------------------------------------------------------------------------------------------------------------+" )
19- print ("+ MTG Inventory Tool +" )
20- print ("+------------------------------------------------------------------------------------------------------------------------------------------------------+" )
19+ print ("+-------------------------------------------------------------------------------------------------------------------------------------------------------+" )
20+ print ("+ MTG Inventory Tool +" )
21+ print ("+-------------------------------------------------------------------------------------------------------------------------------------------------------+" )
22+
2123
2224def get_lowest_price (url , session , country , language , api ):
2325 """Fetches the lowest price for a specific country and the given product language"""
@@ -72,6 +74,8 @@ def calculate_product_value(stock_data):
7274def print_summary (data , config , country ):
7375 """Prints a country specific summary as formated Pretty Table"""
7476
77+ if country == None :
78+ country = "International"
7579 total_expense = 0
7680 total_stock = 0
7781 total_value = 0
@@ -122,17 +126,29 @@ def load_yaml(path):
122126 yaml_content = yaml .load (fpi )
123127 return yaml_content
124128
125-
126- def main ():
129+ def initialize ():
127130 init ()
128131 config = load_yaml (path_to_config )
129132 inventory = load_yaml (path_to_inventory )
130133 api = load_yaml (path_to_api )
131134 print_header ()
135+ return config , inventory , api
132136
137+ def finalize ():
138+ deinit ()
139+ print ("\n Press any key to exit." )
140+ input ()
141+ exit (0 )
142+
143+ def main ():
144+ config , inventory , api = initialize ()
133145 for country_number , country_name in config ["countries" ].items ():
134146 data_country = dict ()
135147 for name , stock_data in inventory .items ():
148+ if (country_name == "ALL" ):
149+ country_name = None
150+ elif (country_name == 0 ):
151+ finalize ()
136152 url = '{}/articles/{}' .format (config ["url" ]
137153 ["base_url" ], stock_data ["article_id" ])
138154 session = create_session (url , config ["keys" ]["app_token" ], config ["keys" ]["app_secret" ],
@@ -144,21 +160,8 @@ def main():
144160 data_country [name ] = stock_data
145161 print_summary (data_country , config , country_name )
146162 print ("" )
147- data_international = dict ()
148- for name , stock_data in inventory .items ():
149- url = '{}/articles/{}' .format (config ["url" ]
150- ["base_url" ], stock_data ["article_id" ])
151- session = create_session (url , config ["keys" ]["app_token" ], config ["keys" ]["app_secret" ],
152- config ["keys" ]["access_token" ], config ["keys" ]["access_token_secret" ])
153- stock_data ["lowest_price" ] = get_lowest_price (
154- url , session , None , stock_data ["language" ], api )
155- stock_data ["expense" ], stock_data ["current_total_product_value" ], stock_data ["absolute_gain" ], stock_data ["percentage_gain" ] = calculate_product_value (
156- stock_data )
157- data_international [name ] = stock_data
158- print_summary (data_international , config , "International" )
159- deinit ()
160- print ("\n Press any key to exit." )
161- input ()
163+ finalize ()
164+
162165
163166
164167if __name__ == "__main__" :
0 commit comments