hrss/web/views.py

148 lines
5.3 KiB
Python
Raw Normal View History

2018-10-24 23:57:41 +02:00
from django.shortcuts import render, redirect
from django.http import HttpResponse
from .utils import *
from .models import Feed
from django.db.models import ObjectDoesNotExist
2018-10-29 21:53:15 +01:00
from bs4 import BeautifulSoup
import logging
2018-12-19 13:18:15 +01:00
from urllib.parse import quote_plus, unquote_plus
2018-10-24 23:57:41 +02:00
# Create your views here.
logger = logging.getLogger(__name__)
2018-10-24 23:57:41 +02:00
2018-12-19 13:18:15 +01:00
def iframe(request, encodedurl):
2018-10-24 23:57:41 +02:00
try:
2018-12-19 13:18:15 +01:00
url = unquote_plus(encodedurl)
req = get_url(url)
2018-10-29 21:53:15 +01:00
html = req.content
bs = False
content_type = req.headers["Content-Type"] if "Content-Type" in req.headers else False
if not content_type or content_type.startswith("text/html"):
logger.debug("No content-type or content-type ~= '^text/html'")
2018-10-29 21:53:15 +01:00
bs = BeautifulSoup(html, 'html.parser')
base_scheme = url.split("://")[0]
base_url = url.split("//")[-1].split("/")[0].split('?')[0]
logger.debug("URL: "+base_scheme+"://"+base_url)
2018-10-29 21:53:15 +01:00
# fixes
# fix click links
all_a = bs.find_all("a")
for a in all_a:
a["href"] = "javascript:void(0)"
# fix absolute CSS
all_links = bs.find_all("link", {"href": True})
for link in all_links:
if link["href"].startswith("/"):
link["href"] = "/iframe/" + base_scheme + "://" + base_url + link["href"]
# test: remove js
all_scripts = bs.find_all("script")
for script in all_scripts:
script.extract()
2018-10-29 21:53:15 +01:00
# fix absolute javascript
all_scripts = bs.find_all("script", {"src": True})
for script in all_scripts:
if script.get("src").startswith("/"):
script["src"] = "/iframe/" + base_scheme + "://" + base_url + script["src"]
# fix absolute images
all_imgs = bs.find_all("img", {"src": True})
for img in all_imgs:
if img.get("src").startswith("/"):
img["src"] = "/iframe/" + base_scheme + "://" + base_url + img["src"]
final_html = str(bs) if bs else html
html = final_html
2018-10-24 23:57:41 +02:00
except Exception as e:
logger.debug(e)
return HttpResponse("An error has occured", content_type=500)
2018-10-29 21:53:15 +01:00
return HttpResponse(html, content_type=content_type)
2018-10-24 23:57:41 +02:00
2018-12-19 13:18:15 +01:00
2018-10-24 23:57:41 +02:00
def dummy(request):
return HttpResponse("toto")
2018-12-19 13:18:15 +01:00
2018-10-24 23:57:41 +02:00
def homepage(request):
if request.method == 'POST':
if "url" in request.POST and request.POST["url"]:
url = request.POST["url"]
if is_valid_url(url):
2018-12-19 13:18:15 +01:00
return redirect("setup", encodedurl=quote_plus(url))
2018-10-24 23:57:41 +02:00
else:
return render(request, 'homepage.html', {"url": url, "error": url+" is not a valid URL."})
return render(request, 'homepage.html')
2018-12-19 13:18:15 +01:00
def setup(request, encodedurl):
decoded_url = unquote_plus(encodedurl)
if is_valid_url(decoded_url):
return render(request, 'setup.html', {"encodedurl": encodedurl, "url": decoded_url})
2018-10-24 23:57:41 +02:00
else:
2018-11-28 23:13:12 +01:00
return redirect("homepage")
2018-10-24 23:57:41 +02:00
def newfeed(request):
if request.method == 'POST':
if not "url" in request.POST or not request.POST["url"] or not "element" in request.POST or not request.POST["element"] or not "title" in request.POST or not request.POST["title"] or not "content" in request.POST or not request.POST["content"]:
2018-10-24 23:57:41 +02:00
return HttpResponse("Error, missing required element")
url = request.POST["url"]
element = request.POST["element"]
title = request.POST["title"]
content = request.POST["content"]
date = request.POST["date"]
author = request.POST["author"]
link = request.POST["link"]
a = find_longest_common_string(element, title)
title = title[len(a)+3:].strip()
a = find_longest_common_string(element, content)
content = content[len(a)+3:].strip()
a = find_longest_common_string(element, date)
date = date[len(a)+3:].strip()
a = find_longest_common_string(element, author)
author = author[len(a)+3:].strip()
a = find_longest_common_string(element, link)
link = link[len(a)+3:].strip()
feed = Feed(url=url, element=element, title=title, content=content, date=date, author=author, link=link)
feed.save()
2018-11-28 23:13:12 +01:00
return redirect("feeds")
2018-10-24 23:57:41 +02:00
else:
2018-11-28 23:13:12 +01:00
return redirect("homepage")
2018-10-24 23:57:41 +02:00
def feeds(request):
feeds = Feed.objects.all()
return render(request, "feeds.html", {"feeds": feeds})
def feed_delete(request, id):
try:
2018-10-29 22:32:54 +01:00
# demo website: disable deleting feeds
if not request.get_host() == "hrss.hipstercat.fr:443":
Feed.objects.get(pk=id).delete()
logger.info("Removed feed ID "+id)
2018-11-28 23:13:12 +01:00
return redirect("feeds")
2018-10-29 22:32:54 +01:00
else:
return HttpResponse("Deleting is disabled on demo website.", status=403)
except ObjectDoesNotExist:
2018-11-28 23:13:12 +01:00
return redirect("feeds")
2018-10-24 23:57:41 +02:00
def rss(request, uurl):
try:
feed = Feed.objects.get(uurl=uurl)
fetched = fetch_feed(feed)
if not fetched or not fetched["items"]:
return HttpResponse("Error: feed is empty. Did you set up 'element' field correctly?", status=422)
return render(request, "feed.xml", {"feed": feed, "rss": fetched}, content_type="application/rss+xml")
2018-10-24 23:57:41 +02:00
except ObjectDoesNotExist:
return HttpResponse("Error: feed is unknown", status=404)