<commit_before># vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2013, <NAME>
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import cgi
import json
import logging
import string
import random
import urllib

from django.conf import settings
from django.contrib.auth.models import User
from django.contrib import messages
from django.db import IntegrityError

from horizon.common.models import ExternalProfile
from keystoneclient.v2_0 import client as keystone_client
from openstack_auth.backend import KeystoneBackend
from weibo import APIClient


LOG = logging.getLogger(__name__)


class ExternalBackend:
    def _admin_client(self):
        return  keystone_client.Client(username=settings.ADMIN_USER,
                                      password=settings.ADMIN_PASSWORD,
                                      tenant_name=settings.ADMIN_TENANT,
                                      auth_url=settings.OPENSTACK_KEYSTONE_URL)

    def _get_sina_profile(self, token=None, request=None):
        redirect_uri = request.build_absolute_uri('authentication_callback')
        sina_client = APIClient(app_key=settings.SINA_APP_ID,
                                app_secret=settings.SINA_APP_SECRET,
                                redirect_uri=redirect_uri)

        # Get a legit access token
        access_token_dict = sina_client.request_access_token(token)
        access_token = access_token_dict.access_token
        sina_client.set_access_token(access_token,
                                     access_token_dict.expires_in)

        # Read the user's profile information
        try:
            sina_profile = sina_client.account.profile.basic.get()
            sina_id = str(sina_profile['id'])
            sina_email = sina_profile['email']
        except Exception as e:
            LOG.warn("SinaAPIClient Error: %s", e)
            messages.error(request, 'You SinaID is not authorized to login.')
            return None

        # Validate the user
        valid = False
        # Get the user's bilateral friends.
        uid = settings.SINA_GROUP_ID
        page = 1
        all_friends = []
        while True:
            try:
                friends = sina_client.friendships.friends.bilateral.get(
                                                                    uid=uid,
                                                                    page=page)
            except Exception as e:
                # no more friends.
                LOG.debug('Error: %s', e)
                break<commit_msg>fix infinite loop error<commit_after># vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2013, <NAME>
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import cgi
import json
import logging
import string
import random
import urllib

from django.conf import settings
from django.contrib.auth.models import User
from django.contrib import messages
from django.db import IntegrityError

from horizon.common.models import ExternalProfile
from keystoneclient.v2_0 import client as keystone_client
from openstack_auth.backend import KeystoneBackend
from weibo import APIClient


LOG = logging.getLogger(__name__)


class ExternalBackend:
    def _admin_client(self):
        return  keystone_client.Client(username=settings.ADMIN_USER,
                                      password=<PASSWORD>,
                                      tenant_name=settings.ADMIN_TENANT,
                                      auth_url=settings.OPENSTACK_KEYSTONE_URL)

    def _get_sina_profile(self, token=None, request=None):
        redirect_uri = request.build_absolute_uri('authentication_callback')
        sina_client = APIClient(app_key=settings.SINA_APP_ID,
                                app_secret=settings.SINA_APP_SECRET,
                                redirect_uri=redirect_uri)

        # Get a legit access token
        access_token_dict = sina_client.request_access_token(token)
        access_token = access_token_dict.access_token
        sina_client.set_access_token(access_token,
                                     access_token_dict.expires_in)

        # Read the user's profile information
        try:
            sina_profile = sina_client.account.profile.basic.get()
            sina_id = str(sina_profile['id'])
            sina_email = sina_profile['email']
        except Exception as e:
            LOG.warn("SinaAPIClient Error: %s", e)
            messages.error(request, 'You SinaID is not authorized to login.')
            return None

        # Validate the user
        valid = False
        # Get the user's bilateral friends.
        uid = settings.SINA_GROUP_ID
        page = 1
        all_friends = []
        while True:
            try:
                friends = sina_client.friendships.friends.bilateral.get(
                                                                    uid=uid,
                                                                    page=page)
                # no more friends.
                if friends['total_number'] == 0:
                    break
            except Exception as e:
                LOG.debug('Error: %s', e)
                break
{'fraction_non_alphanumeric': 0.061792300226463925, 'fraction_numerical': 0.0046910384988676805, 'mean_word_length': 2.0014563106796115, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2727682', 'n_tokens_mistral': 1618, 'n_tokens_neox': 1566, 'n_words': 460}