Spaces:
Runtime error
Runtime error
| from django.shortcuts import render, redirect | |
| from .utils.HebScraper import HebScraper | |
| from .utils.WallmartScraper import WallmartScraper | |
| from .utils.SephoraScraper import SephoraScraper | |
| from .utils.UltaScraper import UltaScraper | |
| from .utils.TargetScraper import TargetScraper | |
| from django_filters.views import FilterView | |
| from django_tables2.views import SingleTableMixin | |
| from .filters import ProductFilter | |
| from .tables import ProductTable | |
| from .models import Product | |
| import csv | |
| from django.http import HttpResponse | |
| import threading | |
| # Create your views here. | |
| def refreshHeb(request): | |
| hebScraper = HebScraper() | |
| hebScraper.run() | |
| return redirect('home') | |
| def refreshWallmart(request): | |
| wallmartScraper = WallmartScraper() | |
| wallmartScraper.run() | |
| return redirect('home') | |
| def refreshSephora(request): | |
| sephoraScraper = SephoraScraper() | |
| sephoraScraper.run() | |
| return redirect('home') | |
| def refreshUlta(request): | |
| ultaScraper = UltaScraper() | |
| ultaScraper.run() | |
| return redirect('home') | |
| def refreshAll(request): | |
| # create a background task to run all the scrapers in parallel | |
| hebScraper = HebScraper() | |
| wallmartScraper = WallmartScraper() | |
| sephoraScraper = SephoraScraper() | |
| targetScraper = TargetScraper() | |
| ultaScraper = UltaScraper() | |
| hebThread = threading.Thread(target=hebScraper.run) | |
| wallmartThread = threading.Thread(target=wallmartScraper.run) | |
| sephoraThread = threading.Thread(target=sephoraScraper.run) | |
| targetThread = threading.Thread(target=targetScraper.run) | |
| ultaThread = threading.Thread(target=ultaScraper.run) | |
| hebThread.start() | |
| wallmartThread.start() | |
| sephoraThread.start() | |
| targetThread.start() | |
| ultaThread.start() | |
| # redirect to home page and pass a message | |
| return redirect('home') | |
| def refreshTarget(request): | |
| TargetScraper().run() | |
| return redirect('home') | |
| def home(request): | |
| return render(request, 'scraper/home.html') | |
| class ProductListView(SingleTableMixin, FilterView): | |
| table_class = ProductTable | |
| model = Product | |
| template_name = "scraper/home.html" | |
| filterset_fields = { | |
| 'title': ['icontains'], | |
| 'ingredients': ['icontains'], | |
| 'product_id': ['exact'], | |
| 'url': ['exact'], | |
| 'store_name': ['exact'], | |
| } | |
| filtered_class = ProductFilter | |
| def download_all_product_csv(request): | |
| parcels = Product.objects.all() | |
| response = HttpResponse(content_type='text/csv') | |
| response['Content-Disposition'] = 'attachment; filename="parcels.csv"' | |
| writer = csv.writer(response) | |
| writer.writerow(['Product Id', 'Title', 'Ingredients', 'Url', 'Store Name']) | |
| for parcel in parcels: | |
| writer.writerow([parcel.product_id, parcel.title, parcel.ingredients, parcel.url, parcel.store_name]) | |
| return response |