Skip to content

Commit

Permalink
https://pypi.org/project/robinhood-api/
Browse files Browse the repository at this point in the history
  • Loading branch information
james-yun committed Jul 5, 2020
1 parent 622b249 commit babacec
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
.idea
__pycache__
*.pyc
build
dist
robinhood_api.egg-info
tokens.json
test.py
1 change: 1 addition & 0 deletions robinhood/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .robinhood import *
22 changes: 12 additions & 10 deletions robinhood.py → robinhood/robinhood.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand All @@ -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: ')
Expand All @@ -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)
Expand Down
25 changes: 25 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -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'
]
)

0 comments on commit babacec

Please sign in to comment.