robinhood-unofficial / pyrh

Python Framework to make trades with the unofficial Robinhood API
https://pyrh.readthedocs.io/en/latest/
MIT License
1.79k stars 603 forks source link

urllib.request is a Python 3 feature #15

Closed jingjingwang closed 7 years ago

jingjingwang commented 7 years ago

The doc says it's written in Python 2 but places such as this uses urllib.request.

Jamonek commented 7 years ago

I will look into this tomorrow.

Best,

Jamone

On Jan 21, 2017, at 9:16 PM, jingjingwang notifications@github.com wrote:

The doc says it's written in Python 2 but places such as this uses urllib.request.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

jsmwoolf commented 7 years ago

jingjingwang,

I noticed that the code uses raw_input() instead of input(). raw_input() does not exist in Python 3. That how I came to that conclusion. However, you are correct as I verified urllib.request to be a Python 3 only feature.

It appears that the code is inconsistent with either Python 2 or 3. What version of Python should we stick to? If it's Python 3, then we should definitely remove my comment and make the appropriate changes.

Here are some places in the code that are Python 2:

-        username = raw_input("Username: ")
+        username = input("Username: ")
-        data = urllib.urlencode({"password" : self.password, "username" : self.username})
+        data = urllib.parse.urlencode({"password" : self.password, "username" : self.username})
-            stock = raw_input("Symbol: ");
+            stock = input("Symbol: ");
-        print(data["symbol"] + ": $" + data["last_trade_price"]);
+        print((data["symbol"] + ": $" + data["last_trade_price"]));
-            urllib.unquote(instrument['url']),
+            urllib.parse.unquote(instrument['url']),

I tested this using Mac OS X Python 2.7 and Python 3.5.2.

rudziankou commented 7 years ago

Hi guys,

I also tried to execute this script. I received same error. I used Python2.7 and CentOS7.

[irudziankou@localhost Robinhood]$ python example.py Traceback (most recent call last): File "example.py", line 4, in <module> my_trader = Robinhood(); File "/home/irudziankou/Robinhood/Robinhood.py", line 52, in __init__ self.session.proxies = urllib.request.getproxies() AttributeError: 'module' object has no attribute 'request'

jsmwoolf commented 7 years ago

Okay. I just used the 2to3 tool to convert to Python 3 and remove the commentary about making the conversion. I issued a pull request to make these changes.

maddurup commented 7 years ago

in Version 2.7: I made it urllib.getproxies() instead of urllib.request.getproxies(). And this worked.

rudziankou commented 7 years ago

@maddurup , It's also working for me. Thanks!

idf commented 7 years ago

Same problem related to this issue:

>>> my_trader.print_quote("AAPL")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "Robinhood.py", line 128, in print_quote
    data = self.quote_data(stock)
  File "Robinhood.py", line 102, in quote_data
    res = json.loads((urllib.request.urlopen(url)).read().decode('utf-8'));
AttributeError: 'module' object has no attribute 'request'
jsmwoolf commented 7 years ago

I decided to revert the code to Python 2. A new pull request has been made.

If any of you wish to use Python 3, please read the Pull Request Note.

Jamonek commented 7 years ago

We will maintain 2 branches for Python 2.7 and 3.