hrss/web/views.py

128 lines
4.4 KiB
Python

from django.shortcuts import render, redirect
from django.http import HttpResponse
from .utils import *
from .models import Feed
from django.db.models import ObjectDoesNotExist
from bs4 import BeautifulSoup
# Create your views here.
def iframe(request, url):
content_type = False
try:
req = requests.get(url)
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"):
bs = BeautifulSoup(html, 'html.parser')
base_scheme = url.split("://")[0]
base_url = url.split("//")[-1].split("/")[0].split('?')[0]
# 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"]
# 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
except Exception as e:
html = str(e)
return HttpResponse(html, content_type=content_type)
def dummy(request):
return HttpResponse("toto")
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):
return redirect("/setup/"+url)
else:
return render(request, 'homepage.html', {"url": url, "error": url+" is not a valid URL."})
return render(request, 'homepage.html')
def setup(request, url):
if is_valid_url(url):
return render(request, 'setup.html', {"url": url})
else:
return redirect("/")
def newfeed(request):
if request.method == 'POST':
if not "url" in request.POST or not "element" in request.POST or not "title" in request.POST or not "content" in request.POST:
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()
return redirect("/feeds")
else:
return redirect("/")
def feeds(request):
feeds = Feed.objects.all()
return render(request, "feeds.html", {"feeds": feeds})
def feed_delete(request, id):
try:
# demo website: disable deleting feeds
if not request.get_host() == "hrss.hipstercat.fr:443":
Feed.objects.get(pk=id).delete()
return redirect("/feeds")
else:
return HttpResponse("Deleting is disabled on demo website.", status=403)
except ObjectDoesNotExist:
return redirect("/feeds")
def rss(request, uurl):
try:
feed = Feed.objects.get(uurl=uurl)
return render(request, "feed.xml", {"feed": feed, "rss": fetch_feed(feed)}, content_type="application/rss+xml")
except ObjectDoesNotExist:
return HttpResponse("nope")