<commit_before># Copyright (c) 2008-2009 Participatory Culture Foundation
# See LICENSE for details.

import urlparse
from datetime import datetime, timedelta

from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist
from django.contrib.auth.models import User
from django.contrib.syndication import feeds, views
from django.utils import feedgenerator
from django.views.decorators.cache import cache_page
from django.http import Http404

from channelguide.api import utils as api_utils
from channelguide import util

def https_add_domain(domain, url):
    """
    The built-in add_domain doesn't support https:// URLs, so we fake it.
    """
    if url and url.startswith('https://'):
        return url
    return _old_add_domain(domain, url)


_old_add_domain = feeds.add_domain
feeds.add_domain = https_add_domain


class MiroFeedGenerator(feedgenerator.DefaultFeed):
    thumbnail_url = "http://s3.getmiro.com/img/home-logo-revised.png"

    def write_items(self, handler):
        handler.startElement(u"image", {})
        handler.addQuickElement(u"url", self.thumbnail_url)
        handler.addQuickElement(u"title", self.feed['title'])
        handler.addQuickElement(u"link", self.feed['link'])
        handler.endElement(u"image")
        feedgenerator.DefaultFeed.write_items(self, handler)

    def add_item_elements(self, handler, item):
        feedgenerator.DefaultFeed.add_item_elements(self, handler, item)
        handler.addQuickElement('thumbnail', item['thumbnail'])


class ChannelsFeed(feeds.Feed):
    title_template = "feeds/channel_title.html"
    description_template = "feeds/channel_description.html"
    feed_type = MiroFeedGenerator

    def item_extra_kwargs(self, item):
        return {
            'thumbnail': util.make_absolute_url(item.thumb_url_200_134())
            }

    def item_guid(self, item):
        if item.newest:
            return item.newest.guid or item.newest.url

    def item_pubdate(self, item):
        if item.newest:
            return item.newest.date

    def item_enclosure_url(self, item):
        results = item.items.filter(
            date__isnull=False,
            date__lt=self.get_date_for_item(item))
        if results.count():
            item.newest = results.order_by('-date')[0]

            # We shouldn't produce a redirect-enclosure for anything that
            # ends in .swf or comes from youtube (since youtube also
            # sometimes does its own redirects, so just .swf doesn't always
            # work) because that will make it so that miro doesn't know
            # that it should do flash scraping (needs to see actual item
            # url to do flash scraping)
            split_url = urlparse.urlsplit(item.newest.url)
            if (split_url[2].endswith('.swf')
                    or split_url[1] in ('www.youtube.com', 'youtube.com')):
                return item.newest.url
            else:
                return item.get_absolute_url() + '/latest'

        else:
            item.newest = None<commit_msg>don't use None as a query value<commit_after># Copyright (c) 2008-2009 Participatory Culture Foundation
# See LICENSE for details.

import urlparse
from datetime import datetime, timedelta

from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist
from django.contrib.auth.models import User
from django.contrib.syndication import feeds, views
from django.utils import feedgenerator
from django.views.decorators.cache import cache_page
from django.http import Http404

from channelguide.api import utils as api_utils
from channelguide import util

def https_add_domain(domain, url):
    """
    The built-in add_domain doesn't support https:// URLs, so we fake it.
    """
    if url and url.startswith('https://'):
        return url
    return _old_add_domain(domain, url)


_old_add_domain = feeds.add_domain
feeds.add_domain = https_add_domain


class MiroFeedGenerator(feedgenerator.DefaultFeed):
    thumbnail_url = "http://s3.getmiro.com/img/home-logo-revised.png"

    def write_items(self, handler):
        handler.startElement(u"image", {})
        handler.addQuickElement(u"url", self.thumbnail_url)
        handler.addQuickElement(u"title", self.feed['title'])
        handler.addQuickElement(u"link", self.feed['link'])
        handler.endElement(u"image")
        feedgenerator.DefaultFeed.write_items(self, handler)

    def add_item_elements(self, handler, item):
        feedgenerator.DefaultFeed.add_item_elements(self, handler, item)
        handler.addQuickElement('thumbnail', item['thumbnail'])


class ChannelsFeed(feeds.Feed):
    title_template = "feeds/channel_title.html"
    description_template = "feeds/channel_description.html"
    feed_type = MiroFeedGenerator

    def item_extra_kwargs(self, item):
        return {
            'thumbnail': util.make_absolute_url(item.thumb_url_200_134())
            }

    def item_guid(self, item):
        if item.newest:
            return item.newest.guid or item.newest.url

    def item_pubdate(self, item):
        if item.newest:
            return item.newest.date

    def item_enclosure_url(self, item):
        max_date = self.get_date_for_item()
        if not max_date:
            item.newest = None
            return
        results = item.items.filter(
            date__isnull=False,
            date__lt=max_date)
        if results.count():
            item.newest = results.order_by('-date')[0]

            # We shouldn't produce a redirect-enclosure for anything that
            # ends in .swf or comes from youtube (since youtube also
            # sometimes does its own redirects, so just .swf doesn't always
            # work) because that will make it so that miro doesn't know
            # that it should do flash scraping (needs to see actual item
            # url to do flash scraping)
            split_url = urlparse.urlsplit(item.newest.url)
            if (split_url[2].endswith('.swf')
                    or split_url[1] in ('www.youtube.com', 'youtube.com')):
                return item.newest.url
            else:
                return item.get_absolute_url() + '/latest'

        else:
            item.newest = None
{'fraction_non_alphanumeric': 0.09193366607631621, 'fraction_numerical': 0.006762196103687007, 'mean_word_length': 2.8110429447852763, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '330039', 'n_tokens_mistral': 1848, 'n_tokens_neox': 1764, 'n_words': 461}