Ejemplo n.º 1
0
def authenticate(user_email, user_password):
    try:
        # These values should match the ones supplied when registering your application.
        mendeley = Mendeley(CLIENT_ID, redirect_uri=REDIRECT_URI)

        auth = mendeley.start_implicit_grant_flow()

        # The user needs to visit this URL, and log in to Mendeley2.
        login_url = auth.get_login_url()

        import requests

        res = requests.post(login_url,
                            allow_redirects=False,
                            data={
                                'username': user_email,
                                'password': user_password
                            })

        auth_response = res.headers['Location']

        # After logging in, the user will be redirected to a URL, auth_response.
        global session
        session = auth.authenticate(auth_response)
        global ACCESS_TOKEN
        ACCESS_TOKEN = session.token['access_token']
    except Exception as exc:
        raise exc
Ejemplo n.º 2
0
def trx_abstracts(m):

    TRX = MaltegoTransform()

    doi = m.getProperty("DOI")
    if not doi:
        TRX.addUIMessage(
            'A DOI is needed to perform this search. Please run "Search on Crossref" and, if a DOI is found, try again here'
        )
        return TRX.returnOutput()

    client_id = Tokens.MendeleyID
    client_secret = Tokens.MendeleySecret

    mendeley = Mendeley(client_id, client_secret=client_secret)
    auth = mendeley.start_client_credentials_flow()
    session = auth.authenticate()

    try:
        abstract = session.catalog.by_identifier(doi=doi).abstract
    except:
        TRX.addUIMessage("Cannot find document on Mendeley")
    else:
        new = TRX.addEntity("me.Article", title)
        new.addProperty("abstract", "Abstract", 'loose',
                        abstract.encode('utf-8'))
        TRX.addUIMessage(abstract)

    logging(TRX.returnOutput(), m.Maltegoxml)
    return TRX.returnOutput()
Ejemplo n.º 3
0
    def get_session(self, user):

        # Get social instance
        social = user.social_auth.get(provider=self.name)

        client_id, client_secret = self.get_key_and_secret()
        tokens = {
            'access_token': social.access_token,
            'refresh_token': social.extra_data['refresh_token']
        }

        # start authorization flow
        mendeley = Mendeley(client_id,
                            client_secret=client_secret,
                            redirect_uri=self.redirect_uri)
        auth = mendeley.start_authorization_code_flow()
        refresher = MendeleyAuthorizationCodeTokenRefresher(auth)
        session = MendeleySession(
            mendeley,
            tokens,
            client=auth.client,
            refresher=MendeleyAuthorizationCodeTokenRefresher(auth))

        # test token expiration
        expires_at = social.extra_data['expires_at']
        if (expires_at or 0) < time():  # renew
            refresher.refresh(session)

            # Store new tokens
            for key in session.token.keys():
                social.extra_data[key] = session.token[key]
            social.save(update_fields=('extra_data', ))

        return session
Ejemplo n.º 4
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
Ejemplo n.º 5
0
    def dois2articles(configLocation, doisAndValues):
        # takes the location of the config file for mendeley, and a
        # dictionary with DOIs and their respective values
        # and returns a dictionary with DOIs as keys and
        # abstracts, titles and more in a dict as values

        if os.path.isfile(configLocation):
            with open(configLocation) as f:
                config = yaml.load(f)

        mendeley = Mendeley(config["clientId"], config["clientSecret"])
        session = mendeley.start_client_credentials_flow().authenticate()

        dois = doisAndValues.keys()
        doisAndInfo = {}

        for doi in dois:
            try:
                info = session.catalog.by_identifier(doi=doi, view="stats")

                doisAndInfo[doi] = {
                    "title": info.title,
                    "abstract": info.abstract,
                    "link": info.link,
                    "keywords": info.keywords,
                    "year": info.year
                }
            except Exception as e:
                pass

        return doisAndInfo
    def genCatalog(self):

        config_file = KEY
        config = {}
        with open('config.yml') as f:
            config = yaml.load(f)
        mendeley = Mendeley(config['clientId'], config['clientSecret'])
        session = mendeley.start_client_credentials_flow().authenticate()

        catalog_df = pd.read_csv(INPUT)
        catalog_id_list = catalog_df['id'].tolist()
        c = 0
        authors_list = []
        for catalog_id in catalog_id_list:
            c += 1
            print(c)
            raw_catalog = session.catalog.get(catalog_id, view='all')
            if raw_catalog.authors:
                for author in raw_catalog.authors:
                    first_name = author.first_name
                    last_name = author.last_name
                    name = (first_name, last_name)
                    if name not in authors_list:
                        authors_list.append(name)
                    else:
                        pass
            else:
                pass

        authors_df = pd.DataFrame()
        authors_df['author_name'] = authors_list
        return authors_df
Ejemplo n.º 7
0
def search(request):
    if 'search' in request.GET:
        print(cache)
        try:
            if request.GET['search'] in cache:
                print("ok")
                article_list = Article.objects.all().filter(
                    id__in=cache.get(request.GET['search']))
                return render(request, 'frontend/search.html',
                              {'article_list': article_list})
        except django.core.cache.backends.base.InvalidCacheBackendError:
            pass

        mendeley = Mendeley(settings.MENDELEY_ID, settings.MENDELEY_SECRET)
        auth = mendeley.start_client_credentials_flow().authenticate()
        request.session['token'] = auth.token
        ids = get_data_by_query(auth.token['access_token'],
                                request.GET['search'], 50)

        cache.set(request.GET['search'], ids, timeout=2000)

        article_list = Article.objects.all().filter(id__in=ids)

        return render(request, 'frontend/search.html',
                      {'article_list': article_list})
    return redirect(reverse('home'))
Ejemplo n.º 8
0
def establish_mendeley_session(config: dict) -> MendeleySession:
    """
    Connect to Mendeley's RESTful server
    """
    if len(config) == 0:
        raise ValueError(
            'Configuration Error: Check the configuration yaml file!')
    try:
        mendeley = Mendeley(client_id=config.get('clientId'),
                            client_secret=config.get('clientSecret'),
                            redirect_uri=config.get('redirectURI'))
        authorization = mendeley.start_implicit_grant_flow()
        if authorization is not None:
            loginURL = authorization.get_login_url()
            if loginURL != "" or loginURL is not None:
                authHeader = {
                    "username": config.get("username"),
                    "password": config.get("password")
                }
                request = requests.post(loginURL,
                                        allow_redirects=False,
                                        data=authHeader)
                if request.status_code >= 400:
                    raise ConnectionError(
                        'Error: Cannot connect to Mendeley Database: Status Code: {}'
                        .format(request.status_code))
                session = authorization.authenticate(
                    request.headers['Location'])
                return session
            else:
                raise ValueError('Error: Cannot Retrieve the Login URL')
        else:
            raise ConnectionAbortedError('Error: Unauthorized User!')
    except Exception as exc:
        print('Error: Connecting to the Mendeley Database')
Ejemplo n.º 9
0
def test_should_get_authenticated_session():
    mendeley = Mendeley('id', 'secret', 'https://example.com', state_generator=DummyStateGenerator())
    auth = mendeley.start_implicit_grant_flow()

    session = auth.authenticate('https://example.com#state=state1234&access_token=token5678&token_type=bearer')

    assert session.token['access_token'] == 'token5678'
    assert session.host == 'https://api.mendeley.com'
Ejemplo n.º 10
0
def mendeleyAuth():
    client_id = "8910"
    client_secret = "OxP464bB5roU81GH"
    mendeley = Mendeley(client_id, client_secret)
    auth = mendeley.start_client_credentials_flow()
    session = auth.authenticate()
    
    return session
Ejemplo n.º 11
0
def mendeleyAuth():
    client_id = "8949"
    client_secret = "u38KsziNTLHOD8VB"
    mendeley = Mendeley(client_id, client_secret)
    auth = mendeley.start_client_credentials_flow()
    session = auth.authenticate()
    
    return session
Ejemplo n.º 12
0
def start_mendeley_session(mndly_config):
    """
    Creates a new Mendeley session
    :param mndly_config: Contains information 'client_id', 'secret'
    :return: session
    """
    mendeley = Mendeley(mndly_config['client_id'], mndly_config['secret'])
    auth = mendeley.start_client_credentials_flow()
    return auth.authenticate()
Ejemplo n.º 13
0
def test_should_get_implicit_grant_login_url():
    mendeley = Mendeley('id', 'secret', 'https://example.com', state_generator=DummyStateGenerator())
    auth = mendeley.start_implicit_grant_flow()

    assert auth.get_login_url() == 'https://api.mendeley.com/oauth/authorize?' \
                                   'response_type=token&' \
                                   'client_id=id&' \
                                   'redirect_uri=https%3A%2F%2Fexample.com&' \
                                   'scope=all&' \
                                   'state=state1234'
Ejemplo n.º 14
0
def trx_searchauthor(m):

    TRX = MaltegoTransform()

    authore = m.getProperty("person.fullname")
    client_id = Tokens.MendeleyID
    client_secret = Tokens.MendeleySecret

    mendeley = Mendeley(client_id, client_secret=client_secret)
    auth = mendeley.start_client_credentials_flow()
    session = auth.authenticate()

    page = session.catalog.advanced_search(author=authore).list()

    if len(page.items) < 12:
        limit = len(page.items)
    else:
        limit = m.Slider
#    print m.Slider
#    print limit

    for n in range(limit):
        doc = page.items[n]
        titlee = clean_obsession(doc.title)
        new = TRX.addEntity("me.Article", titlee)
        new.setWeight(100 - n)
        try:
            new.addProperty("abstract", "Abstract", 'strict',
                            clean_obsession(doc.abstract).encode('utf-8'))
            new.addProperty("year", "Year", 'strict', str(doc.year))
            new.addProperty("DOI", "DOI", 'strict', doc.identifiers['doi'])
            authori = [
                doc.authors[n].first_name.encode('utf-8') + ' ' +
                doc.authors[n].last_name.encode('utf-8')
                for n in range(len(doc.authors))
            ]
            new.addProperty("author", "Author", 'strict',
                            clean_obsession('; '.join(authori)))
            if doc.keywords:
                doc_keywords = [
                    doc.keywords[n].encode('utf-8')
                    for n in range(len(doc.keywords))
                ]
                new.addProperty("keywords", "Keywords", 'strict',
                                clean_obsession('; '.join(doc_keywords)))
        except:
            #              pass
            #           print 'o dear'
            TRX.addUIMessage('Article: ' + titlee +
                             '. Not all fields could be downloaded')
#       print titlee
#    print TRX.returnOutput()
#    print 'santa madonna'
    logging(TRX.returnOutput(), m.Maltegoxml)
    return TRX.returnOutput()
Ejemplo n.º 15
0
    def __init__(self, app_id: str, app_secret: str):
        self._app_id = app_id
        self._app_secret = app_secret
        self._initialized = False
        self._mendeley = Mendeley(app_id, app_secret)
        self._session = None
        """:type : MendeleySession """

        log.info(
            "Intialized SDKCrawler with app_id: {app_id} and app_secret: {app_secret}"
            .format(app_id=app_id, app_secret=app_secret))
Ejemplo n.º 16
0
def startSession():
    '''
    Initializes credential flow and returns session object for search
    '''
    credentials = cred.getCred()
    client_id = credentials['client_id']
    client_secret = credentials['client_secret']

    mendeley = Mendeley(client_id, client_secret=client_secret)
    auth = mendeley.start_client_credentials_flow()
    session = auth.authenticate()
    print('Session open command sent')
    return session
Ejemplo n.º 17
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'))
Ejemplo n.º 18
0
 def perform_action(self, request, *args, **kwargs):
     mendeley = Mendeley(settings.MENDELEY_ID, settings.MENDELEY_SECRET,
                         settings.MENDELEY_REDIRECT)
     self.auth = mendeley.start_authorization_code_flow(
         request.session.get('state', ''))
     if 'state' not in request.session:
         request.session['state'] = self.auth.state
         print(request.session['state'])
     try:
         if not settings.MENDELEY_SSL_VERIFY:
             os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
         mendeley_session = self.auth.authenticate(request.get_full_path())
         request.session['token'] = mendeley_session.token
     except Exception as e:
         print(self.auth.get_login_url())
         raise e
Ejemplo n.º 19
0
def _mendeley_data(doi):
    """Returns Mendeley data or 'None', retrieved by doi via Mendeley."""

    config_file_name = gv.resources_path.joinpath('mendeley_user_config.yml').as_posix()

    with open(config_file_name) as f:
        config = yaml.load(f)

    mendeley = Mendeley(config['clientId'], config['clientSecret'])
    session = mendeley.start_client_credentials_flow().authenticate()

    doc = session.catalog.by_identifier(doi=doi, view='bib')

    mendeley_doi = doc.identifiers['doi']
    if doi == mendeley_doi:
        return doc
    else:
        raise DoiNotFoundException()
Ejemplo n.º 20
0
def avgRCSList(queryList):
    client_id = "8910"
    client_secret = "OxP464bB5roU81GH"
    avgRCS =0
    curryear = datetime.now().year #get the current year
    mendeley = Mendeley(client_id=client_id, client_secret=client_secret)
    resultList = []

    auth = mendeley.start_client_credentials_flow()
    session = auth.authenticate()
    queryList_subset = all_subset(queryList)
    for query in queryList_subset:
        x = 0
        iterCount = 0
        paperCount = 0
        avgRCS = 0
        avgyear = 0
        pages = session.catalog.advanced_search(source=query, view="stats")

        for page in pages.iter(page_size=100):
            if iterCount == 1000:
                break
            if curryear - page.year <= 0:
                x += 0
            else:
                x += page.reader_count / (curryear - page.year)
                paperCount += 1
            iterCount += 1
            avgyear += page.year #calculate average year of publications

        avgyear = avgyear / iterCount
        avgRCS = round((x / paperCount),2)
        resultList.append(avgRCS)
        print(str(query) + ": " + str(avgRCS) + " | published: " + str(paperCount) + " | avgyear: " + str(round(avgyear)))

        results = {

            'subset':queryList_subset,
            'marks':resultList,
            'zipped':zip(queryList_subset,resultList),
            
        }

    return results
Ejemplo n.º 21
0
def load_articles_author(request):
    if request.user.is_authenticated:
        if request.method == 'POST':

            author = Authors.objects.get(pk=request.POST['pk'])
            mendeley = Mendeley(settings.MENDELEY_ID, settings.MENDELEY_SECRET)
            auth = mendeley.start_client_credentials_flow().authenticate()
            print(auth.token)

            ids = get_article_from_authors(author.name,
                                           auth.token['access_token'])
            articles = Article.objects.filter(pk__in=ids)

            return JsonResponse({
                'id':
                list(articles.values_list("pk", flat=True)),
                'title':
                list(articles.values_list("title", flat=True))
            })
Ejemplo n.º 22
0
def mendeley_data_by_doi(doi):
    # returns Mendeley data or 'None'

    with open('mendeley_user_config.yml') as f:
        config = yaml.load(f)
    mendeley = Mendeley(config['clientId'], config['clientSecret'])
    session = mendeley.start_client_credentials_flow().authenticate()

    try:
        doc = session.catalog.by_identifier(doi=doi, view='bib')
    except MendeleyException:
        return None

    mendeley_doi = doc.identifiers['doi']
    if doi == mendeley_doi:
        return doc

    # either an error or doi could not be resolved
    return None
Ejemplo n.º 23
0
def get_article_from_data(request):
    if request.user.is_authenticated:
        if request.method == 'POST':
            query = request.POST['query'].split(';')
            mendeley = Mendeley(settings.MENDELEY_ID, settings.MENDELEY_SECRET)
            auth = mendeley.start_client_credentials_flow().authenticate()

            for i in query:
                if query == '': continue
                get_data_by_query(auth.token['access_token'], query=i)

            if request.user.keywords:
                for i in request.user.keywords:
                    get_data_by_query(auth.token['access_token'], query=i)

            if request.user.authors:
                for i in request.user.authors:
                    get_article_from_authors(i, auth.token['access_token'])

            if request.user.tags:
                for i in request.user.tags:
                    get_data_by_query(auth.token['access_token'], query=i)

            return JsonResponse({"success": "Success"})
Ejemplo n.º 24
0
    def __init__(self, authinfo, group_id):
        """ Authenticate the Mendeley client """
        mendeley = Mendeley(
            client_id=authinfo.client_id,
            client_secret=authinfo.client_secret,
            redirect_uri=authinfo.redirect_uri,
        )

        auth = mendeley.start_authorization_code_flow()
        login_url = auth.get_login_url()

        response = requests.post(
            login_url,
            allow_redirects=False,
            data={
                "username": authinfo.user,
                "password": authinfo.password
            },
        )
        redirect_url = response.headers["Location"]
        redirect_url = redirect_url.replace("http://", "https://")

        self.session = auth.authenticate(redirect_url)
        self.group = self.session.groups.get(group_id)
Ejemplo n.º 25
0
import requests
from tqdm import tqdm

from mendeley import Mendeley
from mendeley.session import MendeleySession

with open(os.path.join(os.path.dirname(__file__), 'config.yml')) as f:
    config = yaml.load(f)

REDIRECT_URI = 'http://*****:*****@app.route('/')
def home():
    if 'token' in session:
        return redirect('/listFolders')

    auth = mendeley.start_authorization_code_flow()
    session['state'] = auth.state

    return render_template('home.html', login_url=(auth.get_login_url()))


@app.route('/oauth')
def auth_return():
Ejemplo n.º 26
0
from mendeley import Mendeley
import yaml
import os

# Get the DOI to look up
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("doi", help="Searches the Mendeley catalogue for this DOI")
args = parser.parse_args()

config_file = 'config.yml'

config = {}

if os.path.isfile(config_file):
    with open('config.yml') as f:
        config = yaml.load(f)
else:
    config['clientId'] = os.environ.get('MENDELEY_CLIENT_ID')
    config['clientSecret'] = os.environ.get('MENDELEY_CLIENT_SECRET')

mendeley = Mendeley(config['clientId'], config['clientSecret'])
session = mendeley.start_client_credentials_flow().authenticate()

doi = args.doi

doc = session.catalog.by_identifier(doi=doi, view='stats')
print(dir(doc))
print '"%s" has %s readers.' % (doc.title, doc.reader_count)
Ejemplo n.º 27
0
import os, sys
from mendeley import Mendeley
from mendeley.session import MendeleySession


with open('config','r',encoding='utf8') as f:
    tmp_str = f.read() 
if not tmp_str: tmp_str = 'None'
config = eval(tmp_str)

with open('session','r',encoding='utf8') as f:
    tmp_str = f.read()
if not tmp_str: tmp_str = 'None'
session_state = eval(tmp_str)

mendeley = Mendeley(config['Id'], config['Secret'], config['REDIRECT_URI'])


def get_session():
    auth = mendeley.start_authorization_code_flow()
    os.startfile(auth.get_login_url())
    auth_response = input()
    session = auth.authenticate(auth_response)

    session_state = {'state':auth.state,'auth_response':auth_response,'token':session.token}

    with open('session','w',encoding='utf8') as f:
        f.write(str(session_state))

def get_session_from_state():
    auth = mendeley.start_authorization_code_flow(session_state['state'])
Ejemplo n.º 28
0
 def __init__(self):
     self.mendeley = Mendeley(config.MENDELEY_ID, config.MENDELEY_SECRET)
     self.mendeley_session = self.mendeley.start_client_credentials_flow().authenticate()
Ejemplo n.º 29
0
from mendeley import Mendeley
from mendeley.session import MendeleySession
import oauthlib

app = Flask(__name__, static_url_path='/static')
app.secret_key = settings.SESSION_KEY
app.config['SESSION_TYPE'] = 'filesystem'

NUMBER_EACH_PAGE = 30
DEFAULT_KEYWORDS = "Hot Tweets,Hot Papers,Fresh Papers,reinforcement learning,language"
DATE_TOKEN_SET = set(['1-week', '2-week', '1-month'])

# Mendeley
MENDELEY_REDIRECT = "{}/oauth".format(settings.HOME_URL)
mendeley = Mendeley(settings.MENDELEY_CLIENTID, settings.MENDELEY_SECRET,
                    MENDELEY_REDIRECT)


def get_date_str(token):
    """
    Convert token to date string.
    For example, '1-week' ---> '2017-04-03'.
    """
    today = DT.date.today()
    if token not in DATE_TOKEN_SET:
        token = '2-week'
    if token == '1-week':
        target_date = today - DT.timedelta(days=7)
    elif token == '2-week':
        target_date = today - DT.timedelta(days=14)
    else:
Ejemplo n.º 30
0
LOGOUT_URL = '/signout/'

EMAIL_BACKEND = config('EMAIL_BACKEND',
                       default='django.core.mail.backends.smtp.EmailBackend')
EMAIL_FILE_PATH = PROJECT_DIR.parent.child('maildumps')
EMAIL_HOST = config('EMAIL_HOST')
EMAIL_PORT = config('EMAIL_PORT', cast=int)
EMAIL_HOST_USER = config('EMAIL_HOST_USER')
EMAIL_HOST_PASSWORD = config('EMAIL_HOST_PASSWORD')
EMAIL_USE_TLS = config('EMAIL_USE_TLS', cast=bool)
DEFAULT_FROM_EMAIL = 'Parsifal Team <*****@*****.**>'
EMAIL_SUBJECT_PREFIX = '[Parsifal] '
SERVER_EMAIL = '*****@*****.**'

MENDELEY_ID = config('MENDELEY_ID', default=0, cast=int)
MENDELEY_SECRET = config('MENDELEY_SECRET', default=None)
MENDELEY_REDIRECT_URI = config('MENDELEY_REDIRECT_URI', default=None)
MENDELEY = Mendeley(MENDELEY_ID,
                    client_secret=MENDELEY_SECRET,
                    redirect_uri=MENDELEY_REDIRECT_URI)

DROPBOX_APP_KEY = config('DROPBOX_APP_KEY', default=None)
DROPBOX_SECRET = config('DROPBOX_SECRET', default=None)
DROPBOX_REDIRECT_URI = config('DROPBOX_REDIRECT_URI', default=None)

ELSEVIER_API_KEY = config('ELSEVIER_API_KEY', default=None)

ABSOLUTE_URL_OVERRIDES = {
    'auth.user': lambda u: '/%s/' % u.username,
}