Пример #1
0
 def get_mendeley_session(self):
     mendeley = django_settings.MENDELEY
     token = self.get_mendeley_token()
     mendeley_session = None
     if token:
         mendeley_session = MendeleySession(mendeley, token)
         try:
             mendeley_session.profiles.me
         except TokenExpiredError, e:
             authenticator = MendeleyAuthorizationCodeAuthenticator(
                 mendeley, DefaultStateGenerator.generate_state())
             oauth = OAuth2Session(client=authenticator.client,
                                   redirect_uri=mendeley.redirect_uri,
                                   scope=['all'])
             oauth.compliance_hook['access_token_response'] = [
                 handle_text_response
             ]
             token = oauth.refresh_token(
                 authenticator.token_url,
                 auth=authenticator.auth,
                 refresh_token=token['refresh_token'])
             self.set_mendeley_token(token)
             self.user.save()
             mendeley_session = MendeleySession(mendeley, token)
         except Exception, e:
             pass
Пример #2
0
    def authenticate(self):
        token_url = self.mendeley.host + '/oauth/token'
        auth = HTTPBasicAuth(self.mendeley.client_id,
                             self.mendeley.client_secret)

        token = self.oauth.fetch_token(token_url, auth=auth, scope=['all'])
        return MendeleySession(self.mendeley, token)
Пример #3
0
def mendeley():

    ## load configs from file
    conf = open('config.yml', 'r+w')
    config = yaml.load(conf)

    mendeley = Mendeley(config['clientId'], config['clientSecret'],
                        config['path'])

    ## interactive OAuth flow
    if 'token' not in config:
        auth = mendeley.start_authorization_code_flow()
        state = auth.state
        auth = mendeley.start_authorization_code_flow(state=state)
        print auth.get_login_url()

        ## auth = mendeley.start_implicit_grant_flow()
        # After logging in, the user will be redirected to a URL, auth_response.
        session = auth.authenticate(raw_input())

        print session.token
        config['token'] = session.token

        ## clean file
        conf.write('')
        yaml.dump(config, conf, default_flow_style=False)
        print 'New infos stored'

    ## update access tokens

    ## use new access token
    session = MendeleySession(mendeley, config['token'])

    return session
Пример #4
0
    def authenticate(self):
        oauth = OAuth2Session(client=self.client, scope=['all'])
        oauth.compliance_hook['access_token_response'] = [handle_text_response]

        token = oauth.fetch_token(self.token_url, auth=self.auth, scope=['all'])
        return MendeleySession(self.mendeley,
                               token,
                               client=self.client,
                               refresher=MendeleyClientCredentialsTokenRefresher(self))
Пример #5
0
def get_client_credentials_session():
    config = load_config()
    mendeley = configure_mendeley()

    if config['recordMode'] == 'none':
        token = {'access_token': config['accessToken']}
        return MendeleySession(mendeley, token)
    else:
        return mendeley.start_client_credentials_flow().authenticate()
Пример #6
0
    def authenticate(self, redirect_url):
        token = self.oauth.fetch_token(self.token_url,
                                       authorization_response=redirect_url,
                                       auth=self.auth,
                                       scope=['all'])

        return MendeleySession(self.mendeley,
                               token,
                               client=self.client,
                               refresher=MendeleyAuthorizationCodeTokenRefresher(self))
Пример #7
0
def get_document(request):
    if 'token' not in request.session:
        return redirect('/')

    mendeley_session = MendeleySession(mendeley, request.session['token'])

    document_id = request.GET['document_id']
    doc = mendeley_session.documents.get(document_id)

    return render(request, 'metadata.html', doc=doc)
Пример #8
0
 def get(self, request, *args, **kwargs):
     if 'token' in request.session:
         try:
             self.mendeley = Mendeley(settings.MENDELEY_ID,
                                      settings.MENDELEY_SECRET,
                                      settings.MENDELEY_REDIRECT)
             self.session = MendeleySession(self.mendeley,
                                            request.session['token'])
             return super(MendeleyMixin, self).get(request, *args, **kwargs)
         except TokenExpiredError:
             print('TOKEN_EXPIRED')
             request.session.pop('token')
             return HttpResponseRedirect(reverse('mendeley_oauth'))
     else:
         return HttpResponseRedirect(reverse('mendeley_oauth'))
Пример #9
0
def index():
    keywords = request.cookies.get('keywords')
    if not keywords:
        keywords = DEFAULT_KEYWORDS
    else:
        keywords = unquote(keywords)
    target_date = get_date_str(request.cookies.get('datetoken'))
    column_list = []
    for kw in keywords.split(","):
        src = "twitter" if "tweets" in kw.lower() else "arxiv"
        num_page = 80 if src == "twitter" else NUMBER_EACH_PAGE
        posts = get_posts(src,
                          keywords=kw,
                          since=target_date,
                          start=0,
                          num=num_page)
        column_list.append((src, kw, posts))

    # Mendeley
    auth = mendeley.start_authorization_code_flow()
    if "ma_token" in session and session["ma_token"] is not None:
        ma_session = MendeleySession(mendeley, session['ma_token'])
        try:
            ma_firstname = ma_session.profiles.me.first_name
        except:
            session['ma_token'] = None
            ma_session = None
            ma_firstname = None
    else:
        ma_session = None
        ma_firstname = None

    ma_authorized = ma_session is not None and ma_session.authorized
    return render_template("index.html",
                           columns=column_list,
                           mendeley_login=auth.get_login_url(),
                           ma_session=ma_session,
                           ma_authorized=ma_authorized,
                           ma_firstname=ma_firstname)
Пример #10
0
def save_mendeley():
    import urllib
    if "ma_token" in session and session["ma_token"] is not None:
        ma_session = MendeleySession(mendeley, session['ma_token'])
    else:
        ma_session = None

    ma_authorized = ma_session is not None and ma_session.authorized
    if not ma_authorized:
        return "Please log in into Mendeley."

    pdf_url = request.args.get('url')
    # Retrieve pdf file
    arxiv_id = pdf_url.split("/")[-1].replace(".pdf", "")
    local_pdf = "{}/{}.pdf".format(settings.PDF_PATH, arxiv_id)
    remote_pdf = "http://arxiv.org/pdf/{}.pdf".format(arxiv_id)
    if not os.path.exists(local_pdf):
        urllib.urlretrieve(remote_pdf, local_pdf)

    # Create file
    ma_session.documents.create_from_file(local_pdf)

    return "{} is saved into Mendeley".format(os.path.basename(local_pdf))
Пример #11
0
def get_session_from_cookies():
    return MendeleySession(mendeley, session['token'])
Пример #12
0
 def authenticate(self, redirect_url):
     token = self.oauth.token_from_fragment(redirect_url)
     return MendeleySession(self.mendeley, token, client=self.client)
Пример #13
0
def get_user_session():
    config = load_config()
    mendeley = configure_mendeley()
    token = {'access_token': config['accessToken']}

    return MendeleySession(mendeley, token)