OAuth of Fanfou
Project description
Installation
$ sudo pip install fanfou
Usage
>>> import fanfou
Step 2: Access API
We assume that you’ve got client on Step 1, now you have two styles to access API.
Style 1:
>>> import json
>>>
>>> resp = client.request('/statuses/home_timeline', 'GET') # resp is a HTTPResponse instance
>>> print(resp.code)
>>> data = json.loads(resp.read()) # Python 3: data = json.loads(resp.read().decode('utf8'))
>>> for item in data:
>>> print(item['text'])
>>>
>>> body = {'status': 'update status test 1'}
>>> resp = client.request('/statuses/update', 'POST', body)
>>> print(resp.code)
Style 2:
>>> import json
>>>
>>> fanfou.bound(client) # note the line
>>>
>>> body = {'page': 2, 'count': 20, 'mode': 'lite'}
>>> resp = client.statuses.home_timeline()
>>> data = json.loads(resp.read()) # Python 3: data = json.loads(resp.read().decode('utf8'))
>>> for item in data:
>>> print(item['text'])
>>>
>>> body = {'status': 'update status test 2'}
>>> resp = client.statuses.update(body)
>>> print(resp.code)
If you want to use style 2, you must fanfou.bound(client) before use. They have the same effect, just two different styles.
Just put all you want to request args to a dict (above is body), and then access a API. If you want to upload a photo, please see pack_image. More API details on Fanfou API Apicategory.
What’s new in 0.2.x
>>> fanfou.bound(client)
>>>
>>> resp = client.users.show()
>>> data = resp.json() # equal: data = json.loads(resp.read().decode('utf8'))
In this update, you can get a Python object directly by using resp.json().
More details
pack_image(args, binary=None)
On /account/update_profile_image and /photos/upload you need to upload a image, pack_image can help you work easily.
>>> # update profile image
>>> args = {'image': 'test.jpg', 'mode': 'lite'}
>>> body, headers = fanfou.pack_image(args)
>>> resp = client.account.update_profile_image(body, headers)
>>> # or, resp = client.request('/account/update_profile_image', 'POST', body, headers)
>>> print(resp.code)
>>>
>>> # upload photo
>>> args = {'photo': 'http://static.fanfou.com/img/fanfou.png', 'status': 'upload online photo'}
>>> body, headers = fanfou.pack_image(args)
>>> resp = client.photos.upload(body, headers)
>>> print(resp.code)
Just put the filename in the args, then pack_image it, and then you can access API. The image file can be local or network file, pack_image will auto read it.
Sometimes you want to provide binary bytes instead of filename when you’re writing a webapp, because the data you get from the form is binary. (like m.setq.me)
>>> f = open('test.jpg')
>>> args = {'photo': 'test.jpg', 'status': 'upload local photo'}
>>> body, headers = fanfou.pack_image(args, binary=f.read()) # note the line
>>> f.close()
>>> resp = client.photos.upload(body, headers)
>>> print(resp.code)
print_api(‘plain’)
The following code print all api_access_url that be allowed pass to client.request:
>>> fanfou.print_api('plain')
If you type the line and watch the results carefully, you will find two api_access_url have ‘/:id’, they are:
Because these API need id on it’s access_url, so we get id from body and replace :id, like that:
>>> body = {'id': 'zFbiu4CsJrw'}
>>> resp = client.request('/favorites/create/:id', 'POST', body)
>>> print(resp.url)
You will see resp.url is http://api.fanfou.com/favorites/create/zFbiu4CsJrw.json (Forget to mention that ‘.json’ will add to the access_url).
print_api(‘bound’)
>>> fanfou.print_api('bound')
The line like fanfou.print_api(‘plain’) but it will print all available methods that like client.statuses.home_timeline.
Your IDE (or editor) can autocomplete them after fanfou.bound(client).
auth classes
The __init__ method for auth classes is as follows:
class OAuth (oauth_consumer, oauth_token=None, callback=None, auth_host=None)
class XAuth (oauth_consumer, username, password)
Thanks
Thank Fanfou and thank you for tolerating my poor English.
If you have any questions, I am here @home2.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for fanfou-0.2.5-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1bf9a4a9abdbb891240bea02dd4388b25b1ced1ccca2d5fc3cee8eec193f9883 |
|
MD5 | 2e3b9abf458c36e6cf8e4507144d6c26 |
|
BLAKE2b-256 | e5ac49b54dff384f98aafd41d43b1f01c2a1e28d32c080dd279ac6730aa9a155 |