-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
132 lines (104 loc) · 4.31 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python3
'''
goodXtausch helps to search for books, games and other items on tauschticket.de based on a goodreads shelf,
amazon or steam wishlist and returns the search result for each item as a table in a html page.
'''
import argparse
from configparser import ConfigParser
import goodXtausch as gXt
import logging
import pandas as pd
from pathlib import Path
import webbrowser
import sys
def main():
# Initiate the parser
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-a', '--amazon', help='Enter (public accessable) amazon wishlist url', type=str)
group.add_argument('-g', '--goodreads', help='Enter export.csv filename', type=str)
group.add_argument('-s', '--steam_id', help='Enter steam id', type=str)
parser.add_argument('-o', '--output', help='Specify output file', type=str, default="output.html")
args = parser.parse_args()
result_f = base_path / args.output
if [args.amazon, args.goodreads, args.steam_id].count(None) == 1:
logger.warning("Bitte genau ein Argument für Amazon, Goodreads oder Steam übergeben.")
sys.exit(1)
if (args.amazon):
mode = "a"
ama_wlist = args.amazon
try:
df = pd.DataFrame(data=gXt.get_amazon_items(ama_wlist, user_agent_f), columns=["title"])
except Exception as e:
logger.warning(f"Fehler beim Abrufen der Amazon-Wunschliste: {e}")
sys.exit(1)
elif(args.goodreads):
mode = "g"
goodreads_shelf_export_f = base_path / args.goodreads
if not goodreads_shelf_export_f.exists():
logger.warning(f"Datei {goodreads_shelf_export_f} nicht gefunden.")
sys.exit(1)
try:
df = pd.DataFrame(data=gXt.goodreads(goodreads_shelf_export_f))
except Exception as e:
logger.warning(f"Fehler beim Verarbeiten der Goodreads-Exportdatei: {e}")
sys.exit(1)
elif(args.steam_id):
mode = "s"
steam_id = args.steam_id
try:
df = pd.DataFrame(data=gXt.steam(steam_id, user_agent_f), columns=["title"])
except Exception as e:
logger.warning(f"Fehler beim Abrufen der Steam-Wunschliste: {e}")
sys.exit(1)
else:
logger.warning("Missing arguments, please run main.py -h for help")
sys.exit(1)
# search tauschticket for the desired items
tausch_df = df.apply(lambda row: gXt.get_tausch(row, mode, time_delay, logger), axis=1)
combined_df = pd.concat([df, tausch_df], axis=1)
# Sort the combined DataFrame by 'results' (descending) and then by 'category' (descending)
sorted_df = combined_df.sort_values(by=['results', 'category'], ascending=[False, False])
# return an html table
gXt.get_html(sorted_df, result_f)
# Opens result in your webbrowser
webbrowser.open(f"file://{result_f}")
if __name__ == "__main__":
base_path = Path(__file__).parent.absolute()
# Config.ini initialisieren
config_path = base_path / 'config.ini'
config_f = ConfigParser()
if not config_path.exists():
print(f"Datei {config_f} nicht gefunden.")
sys.exit(1)
config_f.read(config_path)
# Setup Logging
log_file = config_f.get('Output', 'logging')
# Konvertiere log_file in ein Path-Objekt
log_file_path = Path(log_file)
if not log_file_path.exists():
print(f"Datei {log_file_path} nicht gefunden.")
sys.exit(1)
logger = logging.getLogger('logger')
logger.setLevel(logging.DEBUG)
# create log file handler
fh = logging.FileHandler(log_file_path)
fh.setLevel(logging.DEBUG)
logger.addHandler(fh)
# create stream handler (console output)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
logger.addHandler(ch)
# set format
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
fh.setFormatter(formatter)
# initialize
logger.info(f"Logger initialized. Log file {log_file} is being saved to {log_file_path}")
# Global settings
time_delay = 1
user_agent_f = base_path / 'random_user_agent.csv'
if not user_agent_f.exists():
logger.warning(f"Datei {user_agent_f} nicht gefunden.")
sys.exit(1)
main()