Esempio n. 1
0
    def __init__(self,
                 consumer_key: str,
                 consumer_secret: str,
                 access_token: Optional[str] = None,
                 access_token_secret: Optional[str] = None):

        self.consumer_key = consumer_key
        self.consumer_secret = consumer_secret
        self.access_token = access_token
        self.access_token_secret = access_token_secret

        self.client = OAuth1Service(
            consumer_key=consumer_key,
            consumer_secret=consumer_secret,
            name='goodreads',
            request_token_url=Goodreads.request_token_url,
            authorize_url=Goodreads.authorize_url,
            access_token_url=Goodreads.access_token_url,
            base_url=Goodreads.base_url)

        if self.access_token and self.access_token_secret:
            self.session = OAuth1Session(
                consumer_key=self.consumer_key,
                consumer_secret=self.consumer_secret,
                access_token=self.access_token,
                access_token_secret=self.access_token_secret)
        else:
            self.session = None

        self._current_user: Optional[User] = None
Esempio n. 2
0
 def session(self):
     if not self._session and self._token and self._secret:
         self._session = OAuth1Session(consumer_key=KEY,
                                       consumer_secret=SECRET,
                                       access_token=self._token,
                                       access_token_secret=self._secret)
     return self._session
Esempio n. 3
0
 def oauth_resume(self):
     """Create session if access token and key are already available"""
     self.session = OAuth1Session(
         consumer_key=self.client_key,
         consumer_secret=self.client_secret,
         access_token=self.access_token,
         access_token_secret=self.access_token_secret)
Esempio n. 4
0
def get_session(auth):
    return OAuth1Session(
        consumer_key=auth["goodreads_developer_key"],
        consumer_secret=auth["goodreads_developer_secret"],
        access_token=auth["goodreads_user_access_token"],
        access_token_secret=auth["goodreads_user_access_secret"],
    )
Esempio n. 5
0
def shelve_book_on_gr():
    """ Creates a post request to Goodreads so that the user-selected book can
    be added to their shelves, both within Readerboard and on GR. """

    acct = get_current_account(session['acct'])
    book_id = request.form.get('book')
    shelf = request.form.get('shelf')

    url = 'https://www.goodreads.com/shelf/add_to_shelf.xml'
    params = {'name': shelf, 'book_id': book_id}

    gr_session = OAuth1Session(
        consumer_key=GR_KEY,
        consumer_secret=GR_SECRET,
        access_token=acct.access_token,
        access_token_secret=acct.access_token_secret,
    )

    shelf_request = gr_session.post(url, data=params)

    # TODO: Create a book/edition/shelfbook object and add them to db.
    # book = get_book_details(book_id, GR_KEY)
    # create_books_editions([book], acct.user.gr_id, GR_KEY)

    flash("A book has been added to your " + shelf + " shelf!")
    return render_template("index.html", acct=acct, search=False)
Esempio n. 6
0
def get_acct_id(acct, KEY, SECRET):
    """ Takes in an account and developer keys and returns a list containing
    the goodreads id and url for the requested user. """

    new_gr_session = OAuth1Session(
        consumer_key=KEY,
        consumer_secret=SECRET,
        access_token=acct.access_token,
        access_token_secret=acct.access_token_secret,
    )

    # get user information through a GR query
    response = new_gr_session.get('https://www.goodreads.com/api/auth_user')
    doc = untangle.parse(response.content)
    response = doc.GoodreadsResponse.user
    gr_id = int(response["id"].encode('utf8'))
    gr_url = response.link.cdata.encode('utf8')
    name = response.name.cdata.encode('utf8')

    # gets rest of user info via a separate GR requests
    user_info = requests.get('https://www.goodreads.com/user/show/' + str(gr_id) + '.xml?key=' + KEY)
    user_doc = untangle.parse(user_info.content)
    user_doc = user_doc.GoodreadsResponse.user
    # gr_user_name = user_doc.user_name.cdata.encode('utf8')
    image_url = user_doc.small_image_url.cdata.encode('utf8')

    return (gr_id, gr_url, name, image_url)
 def get(self):
     session = OAuth1Session(
         consumer_key=CONSUMER_KEY,
         consumer_secret=CONSUMER_SECRET,
         access_token=self.request.cookies.get('access_token'),
         access_token_secret=self.request.cookies.get(
             'access_token_secret'))
     self.response.write(get_friends_count(session))
 def get(self):
     session = OAuth1Session(
         consumer_key=CONSUMER_KEY,
         consumer_secret=CONSUMER_SECRET,
         access_token=self.request.cookies.get('access_token'),
         access_token_secret=self.request.cookies.get(
             'access_token_secret'))
     page_number = self.request.get('page_number')
     self.response.write(get_friends_info_json(session, page_number))
Esempio n. 9
0
def goodreads_session(user_id):
    user = mongo_users.find_one({"user_id": user_id})
    session = OAuth1Session(
        consumer_key=os.environ['GOODREADS_API_KEY'],
        consumer_secret=os.environ['GOODREADS_API_SECRET'],
        access_token=user['access_token'],
        access_token_secret=user['access_token_secret'],
    )

    return session
Esempio n. 10
0
def grExistingSession(token1, token2):

    new_session = OAuth1Session(
        consumer_key=grkey,
        consumer_secret=grsecret,
        access_token=token1,
        access_token_secret=token2,
    )

    return new_session
 def get(self):
     """ It is very important to use access token and secret from client cookie
         as there can be multiple users using this app at the same time """
     session = OAuth1Session(
         consumer_key=CONSUMER_KEY,
         consumer_secret=CONSUMER_SECRET,
         access_token=self.request.cookies.get('access_token'),
         access_token_secret=self.request.cookies.get(
             'access_token_secret'))
     self.response.write(get_user_json(session))
Esempio n. 12
0
def get_goodreads_session(key):
    secret = os.environ.get('GOODREADS_API_SECRET')

    access_token = os.environ.get('GOODREADS_ACCESS_TOKEN')
    access_token_secret = os.environ.get('GOODREADS_ACCESS_TOKEN_SECRET')

    session = OAuth1Session(
        consumer_key=key,
        consumer_secret=secret,
        access_token=access_token,
        access_token_secret=access_token_secret
    )

    return session
Esempio n. 13
0
def initialize_goodreads_session():
    """ Creates goodreads session with access token/secret."""

    # Gets the key, secret, access token, access token secret from shell enviroment.
    app_key = environ["KEY"]
    app_secret = environ["SECRET"]
    access_token = environ['ACCESS_TOKEN']
    access_token_secret = environ['ACCESS_TOKEN_SECRET']

    # Creates the session object.
    session = OAuth1Session(
            consumer_key=app_key,
            consumer_secret=app_secret,
            access_token=access_token,
            access_token_secret=access_token_secret)

    return session
Esempio n. 14
0
def get_user_friends(acct, KEY, SECRET):
    """ Takes in an account and developer keys and returns a list of friends for
    a given user. """  # this isn't true - evaluate what needs to be returned tomorrow.

    new_gr_session = OAuth1Session(
        consumer_key=KEY,
        consumer_secret=SECRET,
        access_token=acct.access_token,
        access_token_secret=acct.access_token_secret
    )

    user_id = str(acct.user.gr_id)
    current_page = 1

    total, friends = get_friends_page(new_gr_session, user_id, current_page)

    # check for no friends first
    if len(friends) == 0:
        flash("No Goodreads friends found.")
        print "No friends!"

    # friends requests return a list of 30 at a time
    # get total number of pages required.
    total_pages = int(math.ceil(total / float(30)))
    # creates new users and adds friendship relationships to db
    add_user_friendships(friends, acct)

    # check for more than 30 friends
    if total_pages > 1:

        current_page = 2
        while current_page <= total_pages:

            print "******YOU HAVE MORE FRIENDS*******"

            # wait 1 second between calls, per GR policy
            time.sleep(1.00)

            # create new query with updated current_page
            total, friends = get_friends_page(new_gr_session, user_id, current_page)
            add_user_friendships(friends, acct)
            current_page += 1

    return None
Esempio n. 15
0
def get_session():
    """
    Creates a session using the developer key and secret,
    and session token and token secret.
    """

    key, secret = get_access_configs()
    if not key:
        raise Exception(
            'No access.ini file found with KEY and SECRET information to authenticate.'
        )

    token, token_secret = get_token_configs()
    if not token:
        # todo: implement authentication logic
        raise NotImplementedError

    new_session = OAuth1Session(key, secret, token, token_secret)
    return (new_session)
Esempio n. 16
0
    def session(self):
        if self._session is not None:
            return self._session

        credentials = Transport.read_credentials()

        if credentials:
            self._session = OAuth1Session(
                consumer_key=self._developer_key,
                consumer_secret=self._developer_secret,
                access_token=credentials['access_token'],
                access_token_secret=credentials['access_token_secret'],
            )
        else:
            self._session = self.gr.get_auth_session(
                self._request_token, self._request_token_secret)
            self._write_credentials()

        return self._session
Esempio n. 17
0
def _create_session(access_token):
	access_token, access_token_secret = decrypt_token(access_token, SECRET)
	session = OAuth1Session(consumer_key=GOODREADS_API_KEY, consumer_secret=GOODREADS_API_SECRET,
		access_token=access_token, access_token_secret=access_token_secret)
	return session
Esempio n. 18
0

def get_user_id():
    return read_file("user-id.credentials.txt")


def get_oath_token():
    return read_file("oath.token.credentials.txt")


def get_oath_token_secret():
    return read_file("oath.token-secret.credentials.txt")


new_session = OAuth1Session(consumer_key=get_dev_key(),
                            consumer_secret=get_dev_secret(),
                            access_token=get_oath_token(),
                            access_token_secret=get_oath_token_secret())

# TODO xxx add a 'find book id' command

# xxx add a book - WORKS!
# book_id 631932 is "The Greedy Python"
data = {'name': 'to-read', 'book_id': 631932}

# # add this to our "to-read" shelf
response = new_session.post('https://www.goodreads.com/shelf/add_to_shelf.xml',
                            data)

if response.status_code != 201:
    raise Exception('Cannot create resource: %s' % response.status_code)
else:
from rauth.service import OAuth1Service, OAuth1Session
import xml.etree.ElementTree as ET

url = 'http://www.goodreads.com'
request_token_url = '%s/oauth/request_token/' % url
authorize_url = '%s/oauth/authorize/' % url
access_token_url = '%s/oauth/access_token/' % url

#check if access tokens have been saved. If yes, generate session using access tokens
if (os.path.isfile('accesstokens.txt')):
    accesstokenFile = open('accesstokens.txt', 'r')
    access_token = accesstokenFile.readline().strip()
    access_token_secret = accesstokenFile.readline().strip()
    session = OAuth1Session(
        consumer_key='CHANGEME',
        consumer_secret='CHANGEME',
        access_token=access_token,
        access_token_secret=access_token_secret,
    )
else:
    goodreads = OAuth1Service(
        consumer_key='CHANGEME',
        consumer_secret='CHANGME',
        name='goodreads',
        request_token_url='http://www.goodreads.com/oauth/request_token',
        authorize_url='http://www.goodreads.com/oauth/authorize',
        access_token_url='http://www.goodreads.com/oauth/access_token',
        base_url='http://www.goodreads.com/')
    # head_auth=True is important here; this doesn't work with oauth2 for some reason
    request_token, request_token_secret = goodreads.get_request_token(
        header_auth=True)
    authorize_url = goodreads.get_authorize_url(request_token)
Esempio n. 20
0
import sys, os
import xml.etree.ElementTree as ET
from rauth.service import OAuth1Service, OAuth1Session
from termcolor import colored

key = os.environ.get('GOODREADS_API_KEY')
secret = os.environ.get('GOODREADS_API_SECRET')

access_token = os.environ.get('GOODREADS_ACCESS_TOKEN')
access_token_secret = os.environ.get('GOODREADS_ACCESS_TOKEN_SECRET')

if access_token and access_token_secret:
    session = OAuth1Session(consumer_key=key,
                            consumer_secret=secret,
                            access_token=access_token,
                            access_token_secret=access_token_secret)
    print colored('Authorization successful; continuing.', 'green')
else:
    # Authorize new user
    goodreads = OAuth1Service(
        consumer_key=key,
        consumer_secret=secret,
        name='goodreads',
        request_token_url='https://www.goodreads.com/oauth/request_token',
        authorize_url='https://www.goodreads.com/oauth/authorize',
        access_token_url='https://www.goodreads.com/oauth/access_token',
        base_url='https://www.goodreads.com')

    request_token, request_token_secret = goodreads.get_request_token(
        header_auth=True)
    authorize_url = goodreads.get_authorize_url(request_token)