<commit_before>import urlparse
from utils import log as logging
import oauth2 as oauth
from django.contrib.sites.models import Site
from django.db import IntegrityError
from django.http import HttpResponse, HttpResponseRedirect
from django.conf import settings
from django.core.urlresolvers import reverse
from django.template import RequestContext
from django.contrib.auth import login as login_user
from django.shortcuts import render_to_response
from apps.reader.forms import SignupForm
from apps.reader.models import UserSubscription
from apps.feed_import.models import OAuthToken, OPMLImporter, GoogleReaderImporter
from utils import json_functions as json
from utils.user_functions import ajax_login_required


@ajax_login_required
def opml_upload(request):
    xml_opml = None
    message = "OK"
    code = 1
    payload = {}
    
    if request.method == 'POST':
        if 'file' in request.FILES:
            logging.info(" ---> [%s] OPML Upload" % request.user)
            file = request.FILES['file']
            xml_opml = file.read()
            
            opml_importer = OPMLImporter(xml_opml, request.user)
            folders = opml_importer.process()

            feeds = UserSubscription.objects.filter(user=request.user).values()
            payload = dict(folders=folders, feeds=feeds)
            
            request.session['import_from_google_reader'] = False
        else:
            message = "Attach an .opml file."
            code = -1
            
    data = json.encode(dict(message=message, code=code, payload=payload))
    return HttpResponse(data, mimetype='text/plain')

        
def reader_authorize(request):
    logging.info(" ---> [%s] ~BC~FW~SBAuthorize Google Reader import - %s" % (
        request.user,
        request.session.session_key,
        request.META['REMOTE_ADDR'],
    ))
    oauth_key = settings.OAUTH_KEY
    oauth_secret = settings.OAUTH_SECRET
    scope = "http://www.google.com/reader/api"
    request_token_url = ("https://www.google.com/accounts/OAuthGetRequestToken?"
                         "scope=%s&oauth_callback=http://%s%s") % (
                            scope,
                            Site.objects.get_current().domain,
                            reverse('google-reader-callback'),
                         )
    authorize_url = 'https://www.google.com/accounts/OAuthAuthorizeToken'
    
    # Grab request token from Google's OAuth
    consumer = oauth.Consumer(oauth_key, oauth_secret)
    client = oauth.Client(consumer)
    resp, content = client.request(request_token_url, "GET")
    request_token = dict(urlparse.parse_qsl(content))
    
    # Save request token and delete old tokens
    auth_token_dict = dict(request_token=request_token['oauth_token'], <commit_msg>Google Reader no longer needs session info.<commit_after>import urlparse
from utils import log as logging
import oauth2 as oauth
from django.contrib.sites.models import Site
from django.db import IntegrityError
from django.http import HttpResponse, HttpResponseRedirect
from django.conf import settings
from django.core.urlresolvers import reverse
from django.template import RequestContext
from django.contrib.auth import login as login_user
from django.shortcuts import render_to_response
from apps.reader.forms import SignupForm
from apps.reader.models import UserSubscription
from apps.feed_import.models import OAuthToken, OPMLImporter, GoogleReaderImporter
from utils import json_functions as json
from utils.user_functions import ajax_login_required


@ajax_login_required
def opml_upload(request):
    xml_opml = None
    message = "OK"
    code = 1
    payload = {}
    
    if request.method == 'POST':
        if 'file' in request.FILES:
            logging.info(" ---> [%s] OPML Upload" % request.user)
            file = request.FILES['file']
            xml_opml = file.read()
            
            opml_importer = OPMLImporter(xml_opml, request.user)
            folders = opml_importer.process()

            feeds = UserSubscription.objects.filter(user=request.user).values()
            payload = dict(folders=folders, feeds=feeds)
            
            request.session['import_from_google_reader'] = False
        else:
            message = "Attach an .opml file."
            code = -1
            
    data = json.encode(dict(message=message, code=code, payload=payload))
    return HttpResponse(data, mimetype='text/plain')

        
def reader_authorize(request):
    logging.info(" ---> [%s] ~BC~FW~SBAuthorize Google Reader import - %s" % (
        request.user,
        request.META['REMOTE_ADDR'],
    ))
    oauth_key = settings.OAUTH_KEY
    oauth_secret = settings.OAUTH_SECRET
    scope = "http://www.google.com/reader/api"
    request_token_url = ("https://www.google.com/accounts/OAuthGetRequestToken?"
                         "scope=%s&oauth_callback=http://%s%s") % (
                            scope,
                            Site.objects.get_current().domain,
                            reverse('google-reader-callback'),
                         )
    authorize_url = 'https://www.google.com/accounts/OAuthAuthorizeToken'
    
    # Grab request token from Google's OAuth
    consumer = oauth.Consumer(oauth_key, oauth_secret)
    client = oauth.Client(consumer)
    resp, content = client.request(request_token_url, "GET")
    request_token = dict(urlparse.parse_qsl(content))
    
    # Save request token and delete old tokens
    auth_token_dict = dict(request_token=request_token['oauth_token'], 
{'fraction_non_alphanumeric': 0.09435674106332789, 'fraction_numerical': 0.0010887316276537834, 'mean_word_length': 2.736949152542373, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 4, 'lorem ipsum': 0, 'www.': 6, 'xml': 6}, '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': '6405098', 'n_tokens_mistral': 1581, 'n_tokens_neox': 1557, 'n_words': 357}