Exemple #1
0
def purify_html(request):
    """
    过滤掉多余css,修正图片一些属性,替换超链接
    :param request:
    :return:
    """
    html = request.GET.get('html', None)
    base_url = request.GET.get('baseUrl', None)
    css_path = request.GET.get('cssPath', None)
    # 若果domain已经在数据库了就找到,没有就新建
    domain_name = get_top_domain(base_url)
    site_set = Site.objects.filter(domain_name=domain_name)
    if site_set.exists():
        s = site_set[0]
    else:
        s = Site()
    s.domain_name = domain_name
    # 保存二级页面的主要显示区域的css
    if css_path.strip():
        if not s.second:
            second = SecondModifications()
            second.css_path = css_path.strip()
            second.save()
            s.second = second
        else:
            second = s.second
            second.css_path = css_path.strip()
            second.save()
    s.save()
    res = purify(html, base_url)
    if not res:
        return HttpResponse(status=404)
    return HttpResponse(res)
Exemple #2
0
def save(request):
    """
    存储导航项,所选区域的css路径
    :param request:
    :return:
    """
    if request.method == "POST":
        kind = request.POST.get('type', 'contents')
        url = request.POST.get('url', None)
        # 如果抓取二级页面内容
        if kind == 'contents':
            navs_str = request.POST.get('navs', None)
            if navs_str:
                navs = json.loads(navs_str, 'utf-8')
                domain_name = get_top_domain(url)
                site_set = Site.objects.filter(domain_name=domain_name)
                if site_set.exists():
                    s = site_set[0]
                    s.nav_first.clear()
                else:
                    s = Site()
                s.domain_name = domain_name.strip()
                s.save()
                for nav in navs:
                    if nav['name'].strip() and nav['url'].strip():
                        n = NavBlock()
                        n.father_id = 0
                        n.name = nav['name'].strip()
                        n.url = nav['url'].strip()
                        n.save()
                        for second in nav['secondNavs']:
                            sn = NavBlock()
                            sn.father_id = n.id
                            sn.name = second['name'].strip()
                            sn.url = second['url'].strip()
                            sn.save()
                            # 添加二级nav
                            s.nav_first.add(sn)
                        # 添加一级nav
                        s.nav_first.add(n)
                return HttpResponse(status=200)
        # 如果抓取图片
        elif kind == 'imgs':
            url_str = request.POST.get('imgUrl', None)
            img_urls = json.loads(url_str, 'utf-8')
            domain_name = get_top_domain(url)
            site_set = Site.objects.filter(domain_name=domain_name)
            if site_set.exists():
                s = site_set[0]
                if s.sliders:
                    s.sliders.clear()
            else:
                s = Site()
                s.domain_name = domain_name.strip()
                s.save()
            for url in img_urls:
                if url.strip():
                    slider = Slider()
                    slider.url = url.strip()
                    slider.save()
                    s.sliders.add(slider)
            return HttpResponse(status=200)
        elif kind == 'css':
            domain_name = get_top_domain(url)
            css_path = request.POST.get('cssPath', None)
            site_set = Site.objects.filter(domain_name=domain_name)
            if site_set.exists():
                s = site_set[0]
            else:
                s = Site()
            s.domain_name = domain_name
            # 保存二级页面的主要显示区域的css
            if css_path.strip():
                if not s.second:
                    second = SecondModifications()
                    second.css_path = css_path.strip()
                    second.save()
                    s.second = second
                else:
                    second = s.second
                    second.css_path = css_path.strip()
                    second.save()
            s.save()
            return HttpResponse(status=200)
        elif kind == 'status':
            uid = request.POST.get('id', None)
            if uid:
                site_set = Site.objects.filter(id=uid)
                if site_set.exists():
                    site = site_set[0]
                    site.is_active = not site.is_active
                    site.save()
                    return HttpResponse(status=200)
    elif request.method == "DELETE":
        request_delete = QueryDict(request.body)
        kind = request_delete.get('type', 'imgs')
        url = request_delete.get('url', None)
        if kind == 'imgs':
            domain_name = get_top_domain(url)
            site_set = Site.objects.filter(domain_name=domain_name)
            if site_set.exists():
                s = site_set[0]
                s.sliders.clear()
                return HttpResponse(status=204)
            else:
                return HttpResponse(status=404)

    return HttpResponse(status=403)