<commit_before># Create your views here.

from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect, HttpResponse, HttpResponseNotModified, JsonResponse, HttpResponsePermanentRedirect
from django.db.models import Q, F
from django.contrib.auth.decorators import login_required
from django.utils import timezone
from django.views.decorators.csrf import csrf_exempt
from django.conf import settings
from feeds.utils import update_feeds, import_feed, get_proxy
from django.urls import reverse
from django.contrib import messages
from django.core.exceptions import PermissionDenied
from django.utils.cache import patch_response_headers

import datetime 
import hashlib
import logging
import sys
import traceback
import uuid
import email

import os

import feedparser

#from xml.dom import minidom

from .models import *

import time
import datetime

from bs4 import BeautifulSoup
from urllib.parse import urljoin
import requests


 
def index(request):

    vals = {}
    
    if "feed" in request.GET:
        vals["feed"] = request.GET["feed"]

    vals["popular"] = Source.objects.exclude(image_url=None).order_by("?")[:6]
    
    return render(request, "index.html",vals)

    

def help(request):

    return render(request, "help.html",{} )

    
def robots(request):
    
    r = HttpResponse("""    
User-agent: *
Disallow: /admin/
Disallow: /feed/
Disallow: /static/
    """)
    
    r["Content-Type"] = "text/plain"
    
    return r
    
def favicon(request):
    return HttpResponsePermanentRedirect('/static/images/recast-small.png')



def feed(request,key):

    """ This is the actual RSS feed """

    
    right_now = timezone.now()

    al = AccessLog(raw_id = key,return_code = 410,ip_address = request.META["REMOTE_ADDR"],user_agent=request.META["HTTP_USER_AGENT"])
    al.save()
    try:
        sub = Subscription.objects.get(key=key)
    except:
        return HttpResponse(status=410)  # let's assume that a feed that doesn't exist has been deleted
                                         # I mean it could be mistype, but most likely not.
          
    al.subscription = sub
    al.return_code = 500
    al.save()
    
    sub.last_accessed = right_now

    
    browser_etag = ""
    if "HTTP_IF_NONE_MATCH" in request.META:
        browser_etag = request.META["HTTP_IF_NONE_MATCH"]
        
    final_post = []


    if not sub.complete:
    
        # Check that we shouldn't be adding the next episode

    
        # hmm this will catch us up to the original schedule even for very slow pollers
        # would it be better to just send one episode new max ? 
        # doesn't affect me personally as Overcast has a hyper-agressive server side poller :)
        roll_date = sub.last_sent_date + datetime.timedelta(days=sub.frequency)
        while sub.last_sent < sub.source.max_index and roll_date < right_now:
            sub.last_sent_date = roll_date
            sub.last_sent = sub.last_sent + 1 
            roll_date = roll_date + datetime.timedelta(days=sub.frequency)


        if sub.last_sent == sub.source.max_index:
            sub.complete = True

        last_sent = sub.last_sent
    else: 
      
        # sub has finished
        # wait two days then send feed closed message for 5 days.
        # then send GONE
        last_sent = sub.last_sent
        if (right_now - sub.last_sent_date).days > 2:
            if (right_now - sub.last_sent_date).days < 7:
                last_sent += 1
                final_post = [
                    {
                        "title": "Recast is complete",
                        "recast_link":  "/",
                        "author": "Recast",
                        "created_for_subscription": email.Utils.formatdate(float(sub.last_sent_date.strftime('%s')))  , 
                        "body":  "This Recast has come to an end.  If you have not already done so, you can get a link to the original source podcast feed from the settings link above.  You should subscribe to the original to continue listening to further episodes.  We hope you enjoyed using Recast.",
                        "id": "fin!",
                        "enclosure_set" : {"all": [ {"recast_link":  "/static/audio/end.mp3",
                                                    "length": 94875,
                                                    "type": "audio/mpeg"  } ] },
                        "image_url": "http://" + request.META["HTTP_HOST"] + "/static/images/recast-large.png" ,
                        "sub" : sub                       
                    }
                ]
            else:
                # this sub is dead and gone and waiting for deletion
                sub.save()
                al.return_code = 410    
                al.save()

                return HttpResponse(status=410)
            
        

    sub.save()
    
    return_etag = '"%d-%d"' % (sub.id, last_sent)
    
    if return_etag == browser_etag:
        al.return_code = 304 
        al.save()       
        return HttpResponseNotModified()         
    
    vals = {}
    vals["subscription"] = sub<commit_msg>cache 410 Gones for a week<commit_after># Create your views here.

from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect, HttpResponse, HttpResponseNotModified, JsonResponse, HttpResponsePermanentRedirect
from django.db.models import Q, F
from django.contrib.auth.decorators import login_required
from django.utils import timezone
from django.views.decorators.csrf import csrf_exempt
from django.conf import settings
from feeds.utils import update_feeds, import_feed, get_proxy
from django.urls import reverse
from django.contrib import messages
from django.core.exceptions import PermissionDenied
from django.utils.cache import patch_response_headers

import datetime 
import hashlib
import logging
import sys
import traceback
import uuid
import email

import os

import feedparser

#from xml.dom import minidom

from .models import *

import time
import datetime

from bs4 import BeautifulSoup
from urllib.parse import urljoin
import requests


 
def index(request):

    vals = {}
    
    if "feed" in request.GET:
        vals["feed"] = request.GET["feed"]

    vals["popular"] = Source.objects.exclude(image_url=None).order_by("?")[:6]
    
    return render(request, "index.html",vals)

    

def help(request):

    return render(request, "help.html",{} )

    
def robots(request):
    
    r = HttpResponse("""    
User-agent: *
Disallow: /admin/
Disallow: /feed/
Disallow: /static/
    """)
    
    r["Content-Type"] = "text/plain"
    
    return r
    
def favicon(request):
    return HttpResponsePermanentRedirect('/static/images/recast-small.png')



def feed(request,key):

    """ This is the actual RSS feed """

    
    right_now = timezone.now()

    al = AccessLog(raw_id = key,return_code = 410,ip_address = request.META["REMOTE_ADDR"],user_agent=request.META["HTTP_USER_AGENT"])
    al.save()
    try:
        sub = Subscription.objects.get(key=key)
    except:

        # give cloudflare something to work with
        patch_response_headers(r, cache_timeout=(60 * 60 * 24 * 7))  # A week


        r = HttpResponse("And like that, he's gone." status=410)  # let's assume that a feed that doesn't exist has been deleted
        return r                               # I mean it could be mistype, but most likely not.
          
    al.subscription = sub
    al.return_code = 500
    al.save()
    
    sub.last_accessed = right_now

    
    browser_etag = ""
    if "HTTP_IF_NONE_MATCH" in request.META:
        browser_etag = request.META["HTTP_IF_NONE_MATCH"]
        
    final_post = []


    if not sub.complete:
    
        # Check that we shouldn't be adding the next episode

    
        # hmm this will catch us up to the original schedule even for very slow pollers
        # would it be better to just send one episode new max ? 
        # doesn't affect me personally as Overcast has a hyper-agressive server side poller :)
        roll_date = sub.last_sent_date + datetime.timedelta(days=sub.frequency)
        while sub.last_sent < sub.source.max_index and roll_date < right_now:
            sub.last_sent_date = roll_date
            sub.last_sent = sub.last_sent + 1 
            roll_date = roll_date + datetime.timedelta(days=sub.frequency)


        if sub.last_sent == sub.source.max_index:
            sub.complete = True

        last_sent = sub.last_sent
    else: 
      
        # sub has finished
        # wait two days then send feed closed message for 5 days.
        # then send GONE
        last_sent = sub.last_sent
        if (right_now - sub.last_sent_date).days > 2:
            if (right_now - sub.last_sent_date).days < 7:
                last_sent += 1
                final_post = [
                    {
                        "title": "Recast is complete",
                        "recast_link":  "/",
                        "author": "Recast",
                        "created_for_subscription": email.Utils.formatdate(float(sub.last_sent_date.strftime('%s')))  , 
                        "body":  "This Recast has come to an end.  If you have not already done so, you can get a link to the original source podcast feed from the settings link above.  You should subscribe to the original to continue listening to further episodes.  We hope you enjoyed using Recast.",
                        "id": "fin!",
                        "enclosure_set" : {"all": [ {"recast_link":  "/static/audio/end.mp3",
                                                    "length": 94875,
                                                    "type": "audio/mpeg"  } ] },
                        "image_url": "http://" + request.META["HTTP_HOST"] + "/static/images/recast-large.png" ,
                        "sub" : sub                       
                    }
                ]
            else:
                # this sub is dead and gone and waiting for deletion
                sub.save()
                al.return_code = 410    
                al.save()



                # give cloudflare something to work with
                patch_response_headers(r, cache_timeout=(60 * 60 * 24 * 7))  # A week


                r = HttpResponse("And like that, he's gone." status=410)  
                return r                               

            
        

    sub.save()
    
    return_etag = '"%d-%d"' % (sub.id, last_sent)
    
    if return_etag == browser_etag:
        al.return_code = 304 
        al.save()       
        return HttpResponseNotModified()         
    
    vals = {}
    vals["subscription"] = sub
{'fraction_non_alphanumeric': 0.08875128998968008, 'fraction_numerical': 0.007974481658692184, 'mean_word_length': 1.982652490207051, 'pattern_counts': {'":': 22, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2581667', 'n_tokens_mistral': 3148, 'n_tokens_neox': 2924, 'n_words': 860}