From babacec09ada6fa0a437a6b042df9a0c88a474c7 Mon Sep 17 00:00:00 2001 From: James Yun Date: Sun, 5 Jul 2020 17:37:45 -0400 Subject: [PATCH] https://pypi.org/project/robinhood-api/ --- .gitignore | 3 +++ robinhood/__init__.py | 1 + robinhood.py => robinhood/robinhood.py | 22 ++++++++++++---------- setup.py | 25 +++++++++++++++++++++++++ 4 files changed, 41 insertions(+), 10 deletions(-) create mode 100644 robinhood/__init__.py rename robinhood.py => robinhood/robinhood.py (90%) create mode 100644 setup.py diff --git a/.gitignore b/.gitignore index 2cdf872..9a4bcd5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,8 @@ .idea __pycache__ *.pyc +build +dist +robinhood_api.egg-info tokens.json test.py \ No newline at end of file diff --git a/robinhood/__init__.py b/robinhood/__init__.py new file mode 100644 index 0000000..ceb2500 --- /dev/null +++ b/robinhood/__init__.py @@ -0,0 +1 @@ +from .robinhood import * diff --git a/robinhood.py b/robinhood/robinhood.py similarity index 90% rename from robinhood.py rename to robinhood/robinhood.py index d597389..560cc1b 100644 --- a/robinhood.py +++ b/robinhood/robinhood.py @@ -17,7 +17,7 @@ def oauth(payload): if 'access_token' in response: # save bearer_token and write to tokens.json session.headers['Authorization'] = 'Bearer ' + response['access_token'] - with open('tokens.json', 'w') as file: + with open('../tokens.json', 'w') as file: file.write(json.dumps({ 'bearer_token': response['access_token'], 'refresh_token': response['refresh_token'], @@ -32,14 +32,17 @@ def login(username=None, password=None, device_token='c77a7142-cc14-4bc0-a0ea-bd global session # check if bearer token exists and is valid - with open('tokens.json') as file: - tokens = json.loads(file.read()) - if 'bearer_token' in tokens: - session.headers['Authorization'] = 'Bearer ' + tokens['bearer_token'] - if user(): - return - else: - del session.headers['Authorization'] + with open('tokens.json', 'w+') as file: + try: + tokens = json.loads(file.read()) + if 'bearer_token' in tokens: + session.headers['Authorization'] = 'Bearer ' + tokens['bearer_token'] + if user(): + return + else: + del session.headers['Authorization'] + except json.decoder.JSONDecodeError: + pass if username is None: username = input('Enter email or username: ') @@ -55,7 +58,6 @@ def login(username=None, password=None, device_token='c77a7142-cc14-4bc0-a0ea-bd } r = oauth(payload) - print('login: ') if r.status_code == 400 or r.status_code == 401: # 400: incorrect credentials or unfamiliar device token print(r.text) diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..3738ebf --- /dev/null +++ b/setup.py @@ -0,0 +1,25 @@ +import setuptools + +with open("README.md", "r") as fh: + long_description = fh.read() + +setuptools.setup( + name="robinhood-api", + version="0.0.2", + author="James Yun", + author_email="jameswyun99@gmail.com", + description="the unofficial Robinhood API", + long_description=long_description, + long_description_content_type="text/markdown", + url="https://github.com/james-yun/robinhood", + packages=setuptools.find_packages(), + classifiers=[ + "Programming Language :: Python :: 3", + "License :: OSI Approved :: MIT License", + "Operating System :: OS Independent", + ], + python_requires='>=3.6', + install_requires=[ + 'requests' + ] +)