-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
71 lines (57 loc) · 3 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
import asyncio
from src.storage import StorageExporter
from src.email_sender import EmailSender
from src.crawler import Crawler
from src import parser
# Asynchronous main function
async def main():
# List to store information about all products
all_products = []
# Create an instance of the Crawler class
crawler_instance = Crawler()
# Infinite loop to handle multiple URLs
while True:
# User input for multiple URLs separated by commas
urls = input("Enter the URL(s) separated by commas:")
url_list = [url.strip() for url in urls.split(',')]
# Loop through each URL in the list
for url in url_list:
# Fetch HTML content for the current URL using the Crawler
html_content = await crawler_instance.fetch(url)
print(f"\nProduct Details for URL: {url}\n")
# Check if HTML content is retrieved successfully
if html_content:
# Determine the parser based on the URL format (Trendyol or N11)
if "trendyol.com" in url:
url_parser = parser.TrendyolURLParser(url)
elif "n11.com" in url:
url_parser = parser.N11URLParser(url)
else:
print("Error: Unsupported URL format.")
continue
# Parse the HTML content and get product details
product_list = url_parser.parse(html_content)
all_products.extend(product_list)
# Ask the user if they want to continue entering URLs
user_decision = input("\nDo you want to continue? (y/n): ")
if user_decision.lower() != 'y':
# If user decides to stop, prompt for the recipient's email address
user_email = input("Enter the recipient's email address: ")
break
# Export product information to different file formats (JSON, CSV, Excel)
StorageExporter.export_json(all_products, 'output.json', append=True)
StorageExporter.export_csv(all_products, 'output.csv', append=True)
StorageExporter.export_excel(all_products, 'output.xlsx', append=True)
# Create an instance of the EmailSender class with SMTP details
email_sender = EmailSender(sender_email="[email protected]",
sender_password="flko lpqg kzdn tioq",
smtp_server="smtp.gmail.com",
smtp_port=587)
# Send an Email to notify the user when the crawl process is successfully completed
await email_sender.send_crawl_success_email(receiver_email="[email protected]")
# Send an email with exported data to the user's provided email address
await email_sender.send_exported_data_email(receiver_email=user_email, product_data=all_products,
attachments=['output.json', 'output.csv', 'output.xlsx'])
# Run the asynchronous main function when the script is executed
if __name__ == "__main__":
asyncio.run(main())