-
Notifications
You must be signed in to change notification settings - Fork 0
/
Base_Scraper.py
88 lines (75 loc) · 3.79 KB
/
Base_Scraper.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
from abc import ABC, abstractmethod
from GreekLang import GreekLang
class Base_Scraper(ABC):
def __init__(self):
self.selected_products = []
self.all_products = []
def check_response_status(self, response):
if (response.status_code != 200):
exit("Page Unreachable")
@abstractmethod
def categories_are_available(data):
"Returns True if there are available categories to choose from and False if there aren't"
pass
def select_items(self, products_number):
"Selects the items from the same category, according to user input, stores them in a list and returns them"
print() # empty line
print("-----------------------------")
product_numbers_string = input("Select product(s) number(s) of the product(s) you want to choose. If products are more than one seperate them with commas: ")
product_numbers_list = []
commas = product_numbers_string.count(",")
product_numbers_are_incorrect = True
while (product_numbers_are_incorrect):
if (commas == 0):
if (int(product_numbers_string) not in range(1, products_number + 1)):
product_numbers_string = input("Wrong number, enter again: ")
commas = product_numbers_string.count(",")
else:
product_numbers_list.append(int(product_numbers_string))
product_numbers_are_incorrect = False
else:
product_numbers_string = product_numbers_string.split(",")
if (any(int(product_number) not in range(1, products_number + 1) for product_number in
product_numbers_string)):
product_numbers_string = input("One or more numbers are incorrect, enter again: ")
commas = product_numbers_string.count(",")
else:
product_numbers_list = [int(num) for num in product_numbers_string]
product_numbers_are_incorrect = False
print()
print("-----------------------------")
# save the selected products
selected_products = [self.all_products[product_number - 1] for product_number in product_numbers_list]
return selected_products
@abstractmethod
def process_items(self, pages, response):
"Processes all products from all the available pages and stores them in a list"
pass
@abstractmethod
def select_products(self):
"Selects some products according to user input and adds them to a list"
pass
def lowest_price(self):
"Finds the cheapest product and shows its information"
self.select_products()
self.selected_products.sort(key=lambda product: product["price"])
cheapest_product = self.selected_products[0]
review_score = "Not Available"
if (cheapest_product["review_score"] != 0):
review_score = str(cheapest_product["review_score"])
print("Cheapest product details:" + "\n" +
"- Name: " + cheapest_product["name"] + "\n" +
"- Price: " + str(cheapest_product["price"]) + " €" + "\n" +
"- Review score: " + review_score + '\n' +
"- Link: " + cheapest_product["link"])
print("-----------------------------")
def convert_language(self,search_phrase):
"Converts the language of the search phrase to form Greek Greeklish"
language_modifier = GreekLang()
if language_modifier.is_english(search_phrase):
return search_phrase
elif language_modifier.is_greek(search_phrase):
search_phrase = language_modifier.toGreeklish(search_phrase)
return search_phrase
else:
exit("Wrong Language Input")