Douban is a popular Chinese social cultural site. It integrates Book
reading, movie criticism, city events, and miniblog together.
From my past couple day’s experience, though most Chinese social
websites open their API, but they all have different ‘tricks’ to
successfully send your http post and get commands. I had been confused
on Douban’s photo uploading format for a while, and now I was hoping to
share the points you should be careful when uploading your photo using
urllib and urllib2 libraries from python.
Encode your photo
The format to encode the photo to pass to Douban could be very restrict.
The following format works:
Things you should really keep in mind is that Douban needs a clear
declaration of Content-Type, you need to restrictedly follow the format.
Use mimetypes.types_map would return you a dictionary of types they
have, and you could pass your uploading file’s extension as a key into
mimetypes.types_map.get(YOUR_FILE_EXTENTION), and it should return
you the correct answer for Content-Type.
Also, 0x14026e4be72 is just the boundary variable you could generate
randomly.
Following is the python code:
test_douban class
This version of code is heavily depends on the existed code from
internet. I just amended little bit on the encoding part to make the
format be accepted by Douban.
call test_douban class
Now you should see the photo you just post on Douban’s miniblog. Cheers
!
Comments
comments powered by Disqus