From a179b64c7df061b7319da1ebcef54d9beb0835a5 Mon Sep 17 00:00:00 2001 From: alexwholland Date: Sat, 7 Jan 2023 16:25:18 -0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Added=20a=20new=20location=20to=20d?= =?UTF-8?q?isplay=20url=20results?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../scraper/templates/scraper/index.html | 1 + .../scraper/templates/scraper/result.html | 12 ++++++++++ marketscrape/scraper/views.py | 23 +++++++++++++++++-- 3 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 marketscrape/scraper/templates/scraper/result.html diff --git a/marketscrape/scraper/templates/scraper/index.html b/marketscrape/scraper/templates/scraper/index.html index c90bf84..b3538d6 100644 --- a/marketscrape/scraper/templates/scraper/index.html +++ b/marketscrape/scraper/templates/scraper/index.html @@ -8,6 +8,7 @@ {% csrf_token %} {{ form }} + diff --git a/marketscrape/scraper/templates/scraper/result.html b/marketscrape/scraper/templates/scraper/result.html new file mode 100644 index 0000000..27aef61 --- /dev/null +++ b/marketscrape/scraper/templates/scraper/result.html @@ -0,0 +1,12 @@ +{% extends 'scraper/base.html' %} + +{% block content %} +
+
+

Marketscrape

+

The shortened_url is: {{ shortened_url }}

+

The mobile_url is: {{ mobile_url }}

+

The market_id is: {{ market_id }}

+
+
+{% endblock content %} \ No newline at end of file diff --git a/marketscrape/scraper/views.py b/marketscrape/scraper/views.py index 7e5470b..0a6f433 100644 --- a/marketscrape/scraper/views.py +++ b/marketscrape/scraper/views.py @@ -1,11 +1,30 @@ from django.shortcuts import render from django.views import View from .forms import MarketForm +import re class Index(View): def get(self, request): form = MarketForm() return render(request, 'scraper/index.html', {'form': form}) - def post(self, requeset): - pass \ No newline at end of file + def post(self, request): + form = MarketForm(request.POST) + + if form.is_valid(): + input_url = form.cleaned_data['input_url'] + + # Shorten the URL listing to the title of the listing + shortened_url = re.search(r".*[0-9]", input_url).group(0) + # Use the shortened URL and convert it to mobile, to get the price of the listing + mobile_url = shortened_url.replace("www", "m") + # Find the ID of the product + market_id = (re.search(r"\/item\/([0-9]*)", input_url)).group(1) + + context = { + 'shortened_url': shortened_url, + 'mobile_url': mobile_url, + 'market_id': market_id, + } + + return render(request, 'scraper/result.html', context)