bibanon / BASC-py4chan

Python wrapper for 4chan API. The BA's vastly improved fork of Edgeworth's original.
http://basc-py4chan.readthedocs.org/en/latest/index.html
Do What The F*ck You Want To Public License
55 stars 13 forks source link

file_md5 string does not work in Python3 - Recommending a Cross Compatible Fix #16

Closed antonizoon closed 9 years ago

antonizoon commented 9 years ago

While writing py8chan, I discovered that the file_md5 property in the Post class does not convert the base64 string correctly in python3. This is because Python3 treats all string literals as unicode, but decode only works on ASCII.

Thus, instead of using .encode() or .decode(), use the included Python libraries base64 and binascii, which abstract away the issues, and make our code work on both Python 2 and 3. More info at StackOverflow.

from base64 import b64decode
from binascii import hexlify

@property
def file_md5(self):
    # Py 2/3 compatible equivalent of:
    #return self._data['md5'].decode('base64')
    # More info: http://stackoverflow.com/a/16033232
    # returns a bytestring
    return b64decode('NOetrLVnES3jUn1x5ZPVAg==')

@property
def file_md5_hex(self):
    return hexlify(self.file_md5).decode('ascii')

Pull request coming soon, but here's a public notice.

antonizoon commented 9 years ago

Now fixed in the following commit, after backporting the File Class from py8chan:

https://github.com/bibanon/BASC-py4chan/commit/b91304ba1ca6450485f386b6cf93c604b2546c41