-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
executable file
·175 lines (132 loc) · 4.72 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env python3
# Wishlist Scrapper
# A program to look out for the current prices onto your wishlist
# Author:
# FelipeCRamos
# Useful libraries
import datetime as dt # For generating today's info (log generation)
import sys # Argv manipulating
import os
import re # Regex things
import threading # Threads everywhere
import argparse # cool arg parser
# Custom project libraries
import args
import product as pd
import logs
data = dict()
repeatData = dict()
product_data = []
sorted_data = dict()
products = []
THREAD_ENABLE = True
VERBOSE_ENABLE = False
CURRENT_VERSION = "0.2.4"
TAX = 1.0
def get_status(product):
if product.get_error():
return "E"
elif product.get_indisponible():
return "I"
elif product.get_discount():
return "D"
else:
return " "
def fetchNext(product):
global products
try:
curr_name = product.link.split('/')[-1]
if VERBOSE_ENABLE:
print("Fetching... \t{}".format(product.link))
try:
product.fetch()
product_data.append(product)
except Exception as e:
print(e)
except:
print("ERROR")
class CreateThread(threading.Thread):
'''
Thread system, each thread will be responsible for one Product
'''
def start(self, product):
self.product = product
super().start()
def run(self):
# Make the fetch on the next product
fetchNext(self.product)
def main(filepath, folderpath):
# Tries to open the file for links extraction
try:
links_file = open(filepath, 'r')
except Exception as e:
print(e) # Display error msg
exit()
# Separate links into a link list
links = [ link for link in links_file.read().split('\n') if link != '' and link[0] != '#' ]
# Check if the file isn't empty
if( len(links) == 0 ):
print("ERROR: No links found on the file!")
exit()
# Create Product list
global products
products = [ pd.Product(link) for link in links ]
print("Fetching products...")
for prod in products:
if THREAD_ENABLE:
CreateThread().start(prod)
else:
fetchNext(prod)
# Wait until all threads are done
while threading.active_count() != 1:
continue
print("~")
product_data.sort(key = lambda x: x.price, reverse=True)
for product in product_data:
print(f"[{get_status(product)}] R$ {product.price:8.2f}\t{product.title[0:60-(16 + 6)]} [...]")
print("-" * 60)
sum_prices = sum([p.price for p in product_data])
print(f"R$ {sum_prices:8.2f}\tTOTAL (a vista)")
print(f"R$ {sum_prices * TAX:8.2f}\tTOTAL (parcelado +{(TAX-1)*100}%)")
# Sort data by price
global sorted_data
total_sum = 0
for item, price in sorted(data.items(), key=lambda x: x[1], reverse=True):
total_sum += price
no_occur = repeatData[item] if item in repeatData else 1
itemName = "({}x) - {}".format(no_occur, item)
sorted_data[itemName] = price
print("R$ {:8.2f}\t({}x) {}".format(price, no_occur, item[0:70]))
total_sum = round(total_sum, 2)
sorted_data['TOTAL'] = total_sum
# print('-'*86)
# print("R$ {:8.2f}\t{}".format(total_sum, 'TOTAL'))
# print('-' * 90)
if(folderpath != None):
# print(folderpath)
# output_filepath = folderpath + ("/" if folderpath[-1] != '/' else "") + \
# filepath.split('.')[0].split("/")[-1]
output_filepath = folderpath
print("Output filepath: {}".format(output_filepath))
logs.write2json(output_filepath, sorted_data)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Fetch prices of a given wishlist")
parser.add_argument('-l', '--list', required=True, help="List of products (one link per line)", metavar=('list.txt'))
parser.add_argument('-o', '--output', required=False, help="Output fetched prices", metavar=('output_file.json'))
parser.add_argument('-T', '--no-threading', required=False, help="Disable parallel fetches", action='store_true')
parser.add_argument('-v', '--verbose', required=False, help="Enable verbosity", action='store_true')
parser.add_argument('--tax', required=False, help='The % of tax that you want to simulate (like +13%)', metavar=13.0, type=int)
args = parser.parse_args()
if args.verbose:
print("[WARNING] Verbose active!")
VERBOSE_ENABLE = True
if args.no_threading:
print("[WARNING] No threads!")
THREAD_ENABLE = False
if args.tax != None:
TAX += args.tax / 100
try:
main(args.list, args.output)
except Exception as inst:
print('ERROR:', inst)
exit()