-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
303 lines (225 loc) · 11 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#-*- coding: UTF-8 -*-
"""
e-commerce-crawler CLI
Berkay KOÇAK - 2020 June
CLI Web page crawler with scraper for e-commerce vendors.
Written with Python 3.7.3
Additional libraries:
*Beautiful Soup 4.9^
*PyInquirer
*PyFiglet
*termcolor 1.1.0
"""
import asyncio
from src import crawler, utils, scraper
from pprint import pprint
from PyInquirer import style_from_dict, Token, prompt, Separator
from pyfiglet import Figlet
from termcolor import colored
#Scraper = sc
#Crawler = cw
style1 = style_from_dict({
Token.Separator: '#cc5454',
Token.QuestionMark: '#673ab7 bold',
Token.Selected: '#cc5454', # default
Token.Pointer: '#673ab7 bold',
Token.Instruction: '', # default
Token.Answer: '#f44336 bold',
Token.Question: '',
})
style2 = style_from_dict({
Token.Separator: '#33FFEC',
Token.QuestionMark: '#673ab7 bold',
Token.Selected: '#33FFEC', # default
Token.Pointer: '#AF601A bold',
Token.Instruction: '#EC7063', # defaults
Token.Answer: '#AF601A bold',
Token.Question: '#EC7063',
})
style3 = style_from_dict({
Token.Separator: '#FDDB5F',
Token.QuestionMark: '#673ab7 bold',
Token.Selected: '#DEFD5F', # default
Token.Pointer: '#AF601A bold',
Token.Instruction: '#83FD5F', # defaults
Token.Answer: '#DEFD5F bold',
Token.Question: '#FDDB5F',
})
templateScList = {
'type': 'list',
'message': 'What do you want scraper to work with ? \nWith new assets after the crawling operation, with currently available assets or both... ',
'name': 'operationList',
'choices': ["New Assets","Current Assets","Both"]
}
templateScQuestion = {
'type': 'confirm',
'message': 'Do yo want to include scraping operations for files?',
'name': 'scrapingOpt',
'default': True,
}
templateSameVendorQuestion = {
'type': 'confirm',
'message': 'Do you want to use same vendors at scraping operation for current files ? ',
'name': 'sameVendors',
'default': True,
}
templateCwSelection = [
{
'type': 'checkbox',
'message': 'Choose vendors for crawling',
'name': 'cw_vendors',
'choices': [
Separator(' = Available Crawling Vendors = ')
],
'validate': lambda answer: 'You must choose at least one topping.' \
if len(answer) == 0 else True
}
]
templateScSelection = [
{
'type': 'checkbox',
'message': 'Choose vendors for scraping',
'name': 'sc_vendors',
'choices': [
Separator(' = Available Scraping Vendors = ')
],
'validate': lambda answer: 'You must choose at least one topping.' \
if len(answer) == 0 else True
},
]
templateProductSelection = [
{
'type': 'checkbox',
'message': 'Choose products for scraping from assets.',
'name': 'sc_selectedProducts',
'choices': [
Separator(' = Available Scraping Products = ')
],
'validate': lambda answer: 'You must choose at least one topping.' \
if len(answer) == 0 else True
},
]
templateProductInput = [
{
'type': 'input',
'message': """
Enter some products to crawl in web pages
\n*If name of the product consists of multiple words, use \"-\" symbol between words as seperator
\n*Put comma between multiple products and dont use spaces: """
,
'name': 'products',
'validate': lambda answer: 'You must choose at least one topping.' \
if len(answer) == 0 else True
},
]
templateProductExclude = [
{
'type': 'input',
'message': "Enter words to exclude. Put comma between multiple words\n* Words you enter must be case and special character sensitive!",
'name': 'excludedProducts'
},
]
def main():
f = Figlet(font='cyberlarge')
print(f.renderText(' - CRAWLER - '))
print(f.renderText(' * By Berkay * '))
utils.instructions()
cw_vendorSelection = utils.menu_add_vendors(templateCwSelection)
sc_vendorSelection = utils.menu_add_vendors(templateScSelection)
sc_vendors = []
sc_productSelection = []
try:
useScraper = prompt(templateScQuestion, style=style1)
if (useScraper["scrapingOpt"]):
sc_list = prompt(templateScList, style=style1)
cw_vendors = prompt(cw_vendorSelection, style=style1)
if(len(cw_vendors['cw_vendors']) != 0):
tempVendors = cw_vendors['cw_vendors']
templateSameVendorQuestion['message'] = templateSameVendorQuestion['message'] + " --> "+str(tempVendors)
if(("Both") or ("Current Assets") in sc_list['operationList']):
sameVendors = prompt(templateSameVendorQuestion, style = style1)
if (sameVendors['sameVendors']):
sc_vendors = cw_vendors['cw_vendors']
else:
sc_vendors = prompt(sc_vendorSelection, style=style1)
sc_vendors = sc_vendors["sc_vendors"]
print("Selected Vendors for Cw: " + str(cw_vendors['cw_vendors']))
print("Selected Vendors for Sc: " + str(sc_vendors))
else: pass
elif (not useScraper["scrapingOpt"]): pass
else: exit(0)
else: pass
asyncio.run(utils.timeout(1))
if (utils.file_integrity() and useScraper["scrapingOpt"]):
if "Both" in sc_list['operationList']:
if ("None" in cw_vendors['cw_vendors'] and "None" in sc_vendors ):
print(colored('No vendor selected for both operations, no need to stay in run-time then.', 'red'))
exit(0)
elif "None" in cw_vendors['cw_vendors']:
print(colored('Only scraper will operate', 'red'))
utils.vendor_folder_mapping()
utils.product_folder_mapping(sc_vendors)
sc_productSelection = utils.menu_add_products(templateProductSelection)
sc_selectedProducts = prompt(sc_productSelection, style=style2)
asyncio.run(scraper.scraper_init(sc_vendors,sc_selectedProducts['sc_selectedProducts']))
elif "None" in sc_vendors:
print(colored('Only crawler will operate', 'red'))
cw_selectedProducts = prompt(templateProductInput, style=style3)
cw_productsArr = str(cw_selectedProducts).split(",")
cw_excludedProducts = prompt(templateProductExclude, style=style1)
cw_excludedArr = str(cw_excludedProducts).split(",")
asyncio.run(crawler.init_crawler(cw_productsArr,cw_vendors['cw_vendors'],cw_excludedArr))
else:
print(colored('First, scraper will for for current assets,\nThen, crawler will get desired products content\nAt last, scraper will work for new assets', 'red'))
utils.vendor_folder_mapping()
utils.product_folder_mapping(sc_vendors)
cw_selectedProducts = prompt(templateProductInput, style=style2)
cw_productsArr = str(cw_selectedProducts["products"]).split(",")
cw_excludedProducts = prompt(templateProductExclude, style=style1)
cw_excludedArr = str(cw_excludedProducts).split(",")
print(cw_productsArr)
sc_productSelection = utils.menu_add_products(templateProductSelection)
sc_selectedProducts = prompt(sc_productSelection, style=style2)
print(colored(' -- Scraper works for current assets -- ', 'cyan'))
asyncio.run(utils.timeout(1))
asyncio.run(scraper.scraper_init (sc_vendors,sc_selectedProducts['sc_selectedProducts']) )
print(colored(' -- Crawler Starts -- ', 'cyan'))
asyncio.run(utils.timeout(1))
asyncio.run(crawler.init_crawler(cw_productsArr,cw_vendors['cw_vendors'],cw_excludedArr))
utils.vendor_folder_mapping()
utils.product_folder_mapping(sc_vendors)
print(colored(' -- Scraper works for new assets -- ', 'cyan'))
sc_productSelection = utils.menu_add_products(templateProductSelection)
sc_selectedProducts = prompt(sc_productSelection, style=style2)
asyncio.run(utils.timeout(1))
asyncio.run(scraper.scraper_init (cw_vendors['cw_vendors'],sc_selectedProducts['sc_selectedProducts']) )
elif "Current Assets" in sc_list['operationList']:
print(colored('Only scraper will operate', 'red'))
utils.vendor_folder_mapping()
utils.product_folder_mapping(sc_vendors)
sc_productSelection = utils.menu_add_products(templateProductSelection)
sc_selectedProducts = prompt(sc_productSelection, style=style2)
asyncio.run(scraper.scraper_init(sc_vendors,sc_selectedProducts['sc_selectedProducts']))
else:
print(colored('Scraper will work only for incoming new assets after crawling ends.', 'red'))
cw_selectedProducts = prompt(templateProductInput, style=style3)
cw_productsArr = str(cw_selectedProducts["products"]).split(",")
cw_excludedProducts = prompt(templateProductExclude, style=style1)
cw_excludedArr = str(cw_excludedProducts).split(",")
print(cw_productsArr)
asyncio.run(crawler.init_crawler(cw_productsArr,cw_vendors['cw_vendors'],cw_excludedArr))
utils.vendor_folder_mapping()
utils.product_folder_mapping(sc_vendors)
asyncio.run(scraper.scraper_init(cw_vendors['cw_vendors'],cw_productsArr))
else:
print(colored('Only crawler will operate', 'red'))
cw_vendors = prompt(cw_vendorSelection, style=style1)
cw_selectedProducts = prompt(templateProductInput, style=style3)
cw_productsArr = str(cw_selectedProducts["products"]).split(",")
cw_excludedProducts = prompt(templateProductExclude, style=style1)
cw_excludedArr = str(cw_excludedProducts['excludedProducts']).split(",")
asyncio.run(crawler.init_crawler(cw_productsArr,cw_vendors['cw_vendors'],cw_excludedArr))
except Exception as identifier:
print("ERROR IN MAIN : "+str(identifier))
main()
exit(0)