<commit_before># Universal Subtitles, universalsubtitles.org
#
# Copyright (C) 2011 Participatory Culture Foundation
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see
# http://www.gnu.org/licenses/agpl-3.0.html.

import json
import urllib

import requests
from django.conf import settings
from django.contrib.sites.models import Site
from django.core.urlresolvers import reverse
from django.shortcuts import get_object_or_404, redirect, render_to_response

from accountlinker.models import ThirdPartyAccount
from teams.models import Team
from videos.models import VIDEO_TYPE_YOUTUBE
from videos.types.youtube import YouTubeApiBridge

import logging
import sentry_logger # Magical import to make sure Sentry's error recording happens.
logger = logging.getLogger("authbelt.views")


def _generate_youtube_oauth_request_link(state_str=None):
    state_str = state_str or ""
    base =  "https://accounts.google.com/o/oauth2/auth?"
    state = state_str
    
    params = {
        "client_id": settings.YOUTUBE_CLIENT_ID,
        "redirect_uri": "http://%s%s" % (
            Site.objects.get_current().domain,
            reverse("accountlinker:youtube-oauth-callback")),
        "scope": "https://gdata.youtube.com",
        "state": state,
        "response_type": "code",
        "approval_prompt": "force",
        "access_type": "offline",
        
    }
    return "%s%s" % (base, urllib.urlencode(params))

def youtube_oauth_callback(request):
    """
    Stores the oauth tokes. We identify which team this belongs to
    since we've passed the pk on the state param for the authorize request
    """
    import atom
    code = request.GET.get("code", None)
    if code is None:
        raise Exception("No code in youtube oauth callback")
    state = request.GET.get("state", None)
    if state is None:
        raise Exception("No state in youtube oauth callback")
    values = state.split("-")
    team_pk = values[0]
    if len(values) > 1:
        project_pk = values[1]
    team = Team.objects.get(pk=team_pk)
    
    base =  "https://accounts.google.com/o/oauth2/token"
    state = team.pk
    
    params = {
        "client_id": settings.YOUTUBE_CLIENT_ID,
        "client_secret": settings.YOUTUBE_CLIENT_SECRET,
        "redirect_uri": "http://%s%s" % (
            Site.objects.get_current().domain,
            reverse("accountlinker:youtube-oauth-callback")),
        "code": code,
        "grant_type": "authorization_code",
        
    }
    
    response = requests.post(base, data=params, headers={
        "Content-Type": "application/x-www-form-urlencoded"
    })
    if response.status_code != 200:
        logger.error("Error on requesting Youtube Oauth token", extra={
                    "data": {
                        "sent_params": params,
                        "original_request": request,
                    },
                })
                    
    content = json.loads(response.content)
    bridge = YouTubeApiBridge() 
    client, service = bridge.authorize(content['access_token'], content['refresh_token'])
    feed = client.GetUserFeed(username='default')
    author = [x for x in feed.get_elements() if type(x) == atom.data.Author][0]
    <commit_msg>Youtube oauth url uses universal url, thus can use https on non local environments<commit_after># Universal Subtitles, universalsubtitles.org
#
# Copyright (C) 2011 Participatory Culture Foundation
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see
# http://www.gnu.org/licenses/agpl-3.0.html.

import json
import urllib

import requests
from django.conf import settings
from django.contrib.sites.models import Site
from django.core.urlresolvers import reverse
from django.shortcuts import get_object_or_404, redirect, render_to_response

from accountlinker.models import ThirdPartyAccount
from localeurl.utils import universal_url

from teams.models import Team
from videos.models import VIDEO_TYPE_YOUTUBE
from videos.types.youtube import YouTubeApiBridge

import logging
import sentry_logger # Magical import to make sure Sentry's error recording happens.
logger = logging.getLogger("authbelt.views")


def _generate_youtube_oauth_request_link(state_str=None):
    state_str = state_str or ""
    base =  "https://accounts.google.com/o/oauth2/auth?"
    state = state_str
    
    params = {
        "client_id": settings.YOUTUBE_CLIENT_ID,
        "redirect_uri": 
            universal_url("accountlinker:youtube-oauth-callback"),
        "scope": "https://gdata.youtube.com",
        "state": state,
        "response_type": "code",
        "approval_prompt": "force",
        "access_type": "offline",
        
    }
    return "%s%s" % (base, urllib.urlencode(params))

def youtube_oauth_callback(request):
    """
    Stores the oauth tokes. We identify which team this belongs to
    since we've passed the pk on the state param for the authorize request
    """
    import atom
    code = request.GET.get("code", None)
    if code is None:
        raise Exception("No code in youtube oauth callback")
    state = request.GET.get("state", None)
    if state is None:
        raise Exception("No state in youtube oauth callback")
    values = state.split("-")
    team_pk = values[0]
    if len(values) > 1:
        project_pk = values[1]
    team = Team.objects.get(pk=team_pk)
    
    base =  "https://accounts.google.com/o/oauth2/token"
    state = team.pk
    
    params = {
        "client_id": settings.YOUTUBE_CLIENT_ID,
        "client_secret": settings.YOUTUBE_CLIENT_SECRET,
        "redirect_uri": universal_url("accountlinker:youtube-oauth-callback"),
        "code": code,
        "grant_type": "authorization_code",
        
    }
    
    response = requests.post(base, data=params, headers={
        "Content-Type": "application/x-www-form-urlencoded"
    })
    if response.status_code != 200:
        logger.error("Error on requesting Youtube Oauth token", extra={
                    "data": {
                        "sent_params": params,
                        "original_request": request,
                    },
                })
                    
    content = json.loads(response.content)
    bridge = YouTubeApiBridge() 
    client, service = bridge.authorize(content['access_token'], content['refresh_token'])
    feed = client.GetUserFeed(username='default')
    author = [x for x in feed.get_elements() if type(x) == atom.data.Author][0]
    
{'fraction_non_alphanumeric': 0.09786500464129426, 'fraction_numerical': 0.005039119480175043, 'mean_word_length': 3.01383714741884, 'pattern_counts': {'":': 32, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 6, 'lorem ipsum': 0, 'www.': 4, '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': '2237663', 'n_tokens_mistral': 2212, 'n_tokens_neox': 2139, 'n_words': 655}