Skip to content

Commit 0f064af

Browse files
cleaned up main loop and made the display of international prices configureable in config.yaml
1 parent 405e381 commit 0f064af

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

config/config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ url:
1010
countries:
1111
country_1: AT
1212
country_2: D
13+
country_3: ALL
1314

1415
colors:
1516
red : "\e[0;31;40m"

mkm_inventory_tool.py

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from requests_oauthlib import OAuth1Session
22
from prettytable import PrettyTable
33
from json import loads, JSONDecodeError
4-
from colorama import init,deinit
4+
from colorama import init, deinit
55
import ruamel.yaml
66

77
path_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+
1718
def print_header():
18-
print("+------------------------------------------------------------------------------------------------------------------------------------------------------+")
19-
print("+ MTG Inventory Tool +")
20-
print("+------------------------------------------------------------------------------------------------------------------------------------------------------+")
19+
print("+-------------------------------------------------------------------------------------------------------------------------------------------------------+")
20+
print("+ MTG Inventory Tool +")
21+
print("+-------------------------------------------------------------------------------------------------------------------------------------------------------+")
22+
2123

2224
def 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):
7274
def 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

164167
if __name__ == "__main__":

0 commit comments

Comments
 (0)