<commit_before>'''
Created on Nov 2, 2012

@author: hoekstra
'''

import requests
from oauth_hook import OAuthHook
from time import time
from urlparse import parse_qs
from urllib import unquote
import json
import random
import string

OAuthHook.consumer_key = 'K9qG70PgROIg8CpZGJlGRg'
OAuthHook.consumer_secret = '0JdZcz5pz0HwyWbeiwsviA'



def get_auth_url(request):
    """Uses the consumer key and secret to get a token and token secret for requesting authorization from Figshare
    
    Returns the authorization URL, or raises an exception if the response contains an error.
    """
    figshare_oauth_hook = OAuthHook(header_auth=True)
    
    response = requests.post('http://api.figshare.com/v1/pbl/oauth/request_token', hooks={'pre_request': figshare_oauth_hook})
    
    qs = parse_qs(response.text)
    
    if 'error' in qs:
        raise Exception(qs['error'])
    else :     
        oauth_request_token = qs['oauth_token'][0]
        oauth_request_token_secret = qs['oauth_token_secret'][0]
        oauth_request_auth_url = unquote(qs['oauth_request_auth_url'][0])
    
        request.session['oauth_request_token'] = oauth_request_token
        request.session['oauth_request_token_secret'] = oauth_request_token_secret
        
        return oauth_request_auth_url

def validate_oauth_verifier(request, oauth_verifier):
    """Retrieves an oauth access token and secret from Figshare using the oauth_verifier (pin) provided by the user. 
    
    Adds the oauth token and secret to the session, or raises an exception if the response is empty or contains an error.
    """
    oauth_request_token = request.session.get('oauth_request_token')
    oauth_request_token_secret = request.session.get('oauth_request_token_secret')
    
    figshare_oauth_hook = OAuthHook(oauth_request_token, oauth_request_token_secret, header_auth=True)
    response = requests.post('http://api.figshare.com/v1/pbl/oauth/access_token', {'oauth_verifier': oauth_verifier}, hooks={'pre_request': figshare_oauth_hook})
    response = parse_qs(response.content)
    
    if response == {} :
        raise Exception('Authorization failed')
    elif 'error' in response :
        raise Exception(response['error'])
    else :
        oauth_token = response['oauth_token'][0]
        oauth_token_secret = response['oauth_token_secret'][0]
        xoauth_figshare_id = response['xoauth_figshare_id'][0]
    
        request.session['oauth_token'] = oauth_token
        request.session['oauth_token_secret'] = oauth_token_secret
        request.session['xoauth_figshare_id'] = xoauth_figshare_id
    
    return


def get_articles(request):
    """Uses the oauth token and secret to obtain all articles of the authenticated user from Figshare
    
    Adds the results to the session under 'items', or raises an exception if the response contains an error.
    """
    oauth_token = request.session.get('oauth_token')
    oauth_token_secret = request.session.get('oauth_token_secret')

    oauth_hook = OAuthHook(oauth_token, oauth_token_secret, header_auth=True)

    client = requests.session(hooks={'pre_request': oauth_hook})

    response = client.get('http://api.figshare.com/v1/my_data/articles')

    if 'error' in response :
        raise Exception(response['error'])
    else :
        results = json.loads(response.content)
        
        request.session['items'] = results['items']
    
    return




<commit_msg>Fixed bug with response content<commit_after>'''
Created on Nov 2, 2012

@author: hoekstra
'''

import requests
from oauth_hook import OAuthHook
from time import time
from urlparse import parse_qs
from urllib import unquote
import json
import random
import string

OAuthHook.consumer_key = 'K9qG70PgROIg8CpZGJlGRg'
OAuthHook.consumer_secret = '0JdZcz5pz0HwyWbeiwsviA'



def get_auth_url(request):
    """Uses the consumer key and secret to get a token and token secret for requesting authorization from Figshare
    
    Returns the authorization URL, or raises an exception if the response contains an error.
    """
    figshare_oauth_hook = OAuthHook(header_auth=True)
    
    response = requests.post('http://api.figshare.com/v1/pbl/oauth/request_token', hooks={'pre_request': figshare_oauth_hook})
    
    qs = parse_qs(response.content)
    
    if 'error' in qs:
        raise Exception(qs['error'])
    else :     
        oauth_request_token = qs['oauth_token'][0]
        oauth_request_token_secret = qs['oauth_token_secret'][0]
        oauth_request_auth_url = unquote(qs['oauth_request_auth_url'][0])
    
        request.session['oauth_request_token'] = oauth_request_token
        request.session['oauth_request_token_secret'] = oauth_request_token_secret
        
        return oauth_request_auth_url

def validate_oauth_verifier(request, oauth_verifier):
    """Retrieves an oauth access token and secret from Figshare using the oauth_verifier (pin) provided by the user. 
    
    Adds the oauth token and secret to the session, or raises an exception if the response is empty or contains an error.
    """
    oauth_request_token = request.session.get('oauth_request_token')
    oauth_request_token_secret = request.session.get('oauth_request_token_secret')
    
    figshare_oauth_hook = OAuthHook(oauth_request_token, oauth_request_token_secret, header_auth=True)
    response = requests.post('http://api.figshare.com/v1/pbl/oauth/access_token', {'oauth_verifier': oauth_verifier}, hooks={'pre_request': figshare_oauth_hook})
    response_content = parse_qs(response.content)
    
    if response_content == {} :
        raise Exception('Authorization failed')
    elif 'error' in response_content :
        raise Exception(response_content['error'])
    else :
        oauth_token = response_content['oauth_token'][0]
        oauth_token_secret = response_content['oauth_token_secret'][0]
        xoauth_figshare_id = response_content['xoauth_figshare_id'][0]
    
        request.session['oauth_token'] = oauth_token
        request.session['oauth_token_secret'] = oauth_token_secret
        request.session['xoauth_figshare_id'] = xoauth_figshare_id
    
    return


def get_articles(request):
    """Uses the oauth token and secret to obtain all articles of the authenticated user from Figshare
    
    Adds the results to the session under 'items', or raises an exception if the response contains an error.
    """
    oauth_token = request.session.get('oauth_token')
    oauth_token_secret = request.session.get('oauth_token_secret')

    oauth_hook = OAuthHook(oauth_token, oauth_token_secret, header_auth=True)

    client = requests.session(hooks={'pre_request': oauth_hook})

    response = client.get('http://api.figshare.com/v1/my_data/articles')

    if 'error' in response.content :
        raise Exception(response['error'])
    else :
        results = json.loads(response.content)
        
        request.session['items'] = results['items']
    
    return





{'fraction_non_alphanumeric': 0.09054151624548737, 'fraction_numerical': 0.006064981949458484, 'mean_word_length': 3.919034090909091, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2445282', 'n_tokens_mistral': 2183, 'n_tokens_neox': 2151, 'n_words': 486}