<commit_before>from django.contrib.sites.models import RequestSite
from django.shortcuts import render
from django.conf import settings
from django.db.models import Q

from funfactory.urlresolvers import reverse

from airmozilla.main.models import Channel, Event
from airmozilla.main.views import is_contributor
from airmozilla.base.utils import (
    paginate
)


def categories_feed(request):
    context = {}

    privacy_filter = {}
    privacy_exclude = {}
    if request.user.is_active:
        if is_contributor(request.user):
            # feed_privacy = 'contributors'
            privacy_exclude = {'privacy': Event.PRIVACY_COMPANY}
        # else:
            # feed_privacy = 'company'
    else:
        privacy_filter = {'privacy': Event.PRIVACY_PUBLIC}
        # feed_privacy = 'public'
    events = Event.objects.filter(status=Event.STATUS_SCHEDULED)
    live_events = Event.objects.live()
    if privacy_filter:
        events = events.filter(**privacy_filter)
        live_events = live_events.filter(**privacy_filter)
    elif privacy_exclude:
        events = events.exclude(**privacy_exclude)
        live_events = live_events.exclude(**privacy_exclude)

    channels = get_channels(events)
    context['channels'] = channels

    context['live_events'] = live_events

    prefix = request.is_secure() and 'https' or 'http'
    root_url = '%s://%s' % (prefix, RequestSite(request).domain)

    def abs_url_maker(viewname, *args, **kwargs):
        return root_url + reverse(viewname, args=args, kwargs=kwargs)

    context['abs_url'] = abs_url_maker
    context['get_media_info'] = get_media_info

    response = render(request, 'roku/categories.xml', context)
    response['Content-Type'] = 'text/xml'
    return response


def get_channels(events, parent=None):
    channels = []
    channels_qs = Channel.objects.all()
    if parent is None:
        channels_qs = channels_qs.filter(parent__isnull=True)
    else:
        channels_qs = channels_qs.filter(parent=parent)
    for channel in channels_qs:
        event_count = events.filter(channels=channel).count()
        subchannel_count = Channel.objects.filter(parent=channel).count()
        if event_count or subchannel_count:
            # channel.subchannels = get_channels(events, parent=channel)
            channels.append(channel)

    def sorter(x, y):
        if x.slug == settings.DEFAULT_CHANNEL_SLUG:
            return -2
        return cmp(x.name.lower(), y.name.lower())
    channels.sort(sorter)
    return channels


def get_media_info(event):
    if event.template and 'vid.ly' in event.template.name.lower():
        tag = event.template_environment['tag']
        return {
            # 'url': 'http://vid.ly/%s?content=video&format=webm' % tag,
            # 'format': 'webm'
            # NOTE that it's deliberately set to the HTTP URL. Not HTTPS :(
            'url': 'http://vid.ly/%s?content=video&format=mp4' % tag,
            'format': 'mp4'
        }
    elif event.template and 'hls' in event.template.name.lower():
        try:
            file = event.template_environment['file']
            return {
                # it's important to use HTTP here :(
                'url': (
                    'http://wowza1.cdn.mozilla.net/live/ngrp:%s_all'
                    '/playlist.m3u8' % file
                ),
                'format': 'hls',
            }
        except KeyError:
            pass

    return None


def event_feed(request, id):
    # return a feed containing exactly only one event
    context = {}
    events = Event.objects.filter(id=id)
    context['events'] = events

    context['get_media_info'] = get_media_info

    response = render(request, 'roku/channel.xml', context)
    response['Content-Type'] = 'text/xml'
    return response
<commit_msg>correct wowza CDN URL for HLS streams<commit_after>from django.contrib.sites.models import RequestSite
from django.shortcuts import render
from django.conf import settings
from django.db.models import Q

from funfactory.urlresolvers import reverse

from airmozilla.main.models import Channel, Event
from airmozilla.main.views import is_contributor
from airmozilla.base.utils import (
    paginate
)


def categories_feed(request):
    context = {}

    privacy_filter = {}
    privacy_exclude = {}
    if request.user.is_active:
        if is_contributor(request.user):
            # feed_privacy = 'contributors'
            privacy_exclude = {'privacy': Event.PRIVACY_COMPANY}
        # else:
            # feed_privacy = 'company'
    else:
        privacy_filter = {'privacy': Event.PRIVACY_PUBLIC}
        # feed_privacy = 'public'
    events = Event.objects.filter(status=Event.STATUS_SCHEDULED)
    live_events = Event.objects.live()
    if privacy_filter:
        events = events.filter(**privacy_filter)
        live_events = live_events.filter(**privacy_filter)
    elif privacy_exclude:
        events = events.exclude(**privacy_exclude)
        live_events = live_events.exclude(**privacy_exclude)

    channels = get_channels(events)
    context['channels'] = channels

    context['live_events'] = live_events

    prefix = request.is_secure() and 'https' or 'http'
    root_url = '%s://%s' % (prefix, RequestSite(request).domain)

    def abs_url_maker(viewname, *args, **kwargs):
        return root_url + reverse(viewname, args=args, kwargs=kwargs)

    context['abs_url'] = abs_url_maker
    context['get_media_info'] = get_media_info

    response = render(request, 'roku/categories.xml', context)
    response['Content-Type'] = 'text/xml'
    return response


def get_channels(events, parent=None):
    channels = []
    channels_qs = Channel.objects.all()
    if parent is None:
        channels_qs = channels_qs.filter(parent__isnull=True)
    else:
        channels_qs = channels_qs.filter(parent=parent)
    for channel in channels_qs:
        event_count = events.filter(channels=channel).count()
        subchannel_count = Channel.objects.filter(parent=channel).count()
        if event_count or subchannel_count:
            # channel.subchannels = get_channels(events, parent=channel)
            channels.append(channel)

    def sorter(x, y):
        if x.slug == settings.DEFAULT_CHANNEL_SLUG:
            return -2
        return cmp(x.name.lower(), y.name.lower())
    channels.sort(sorter)
    return channels


def get_media_info(event):
    if event.template and 'vid.ly' in event.template.name.lower():
        tag = event.template_environment['tag']
        return {
            # 'url': 'http://vid.ly/%s?content=video&format=webm' % tag,
            # 'format': 'webm'
            # NOTE that it's deliberately set to the HTTP URL. Not HTTPS :(
            'url': 'http://vid.ly/%s?content=video&format=mp4' % tag,
            'format': 'mp4'
        }
    elif event.template and 'hls' in event.template.name.lower():
        try:
            file = event.template_environment['file']
            return {
                # it's important to use HTTP here :(
                'url': (
                    'http://wowza1.cdn.mozilla.net/Edgecast/ngrp:%s_all'
                    '/playlist.m3u8' % file
                ),
                'format': 'hls',
            }
        except KeyError:
            pass

    return None


def event_feed(request, id):
    # return a feed containing exactly only one event
    context = {}
    events = Event.objects.filter(id=id)
    context['events'] = events

    context['get_media_info'] = get_media_info

    response = render(request, 'roku/channel.xml', context)
    response['Content-Type'] = 'text/xml'
    return response

{'fraction_non_alphanumeric': 0.11292445380363253, 'fraction_numerical': 0.0015793629902605948, 'mean_word_length': 2.6727887868535523, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, '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': '2065023', 'n_tokens_mistral': 2300, 'n_tokens_neox': 2222, 'n_words': 484}