2015年12月18日 星期五

[python] python 推薦 twitter sdk api lib


使用 twitter rest api 有許多的 client lib tools
在 python 上,推薦使用  Python Twitter Tools

就如同他自己的介紹,

The Minimalist Twitter API for Python is a Python API for Twitter, everyone's favorite Web 2.0 Facebook-style status updater for people on the go.

Python Twitter Tools (command-line client and IRC bot)
http://mike.verdone.ca/twitter/
sixohsix/twitter
https://github.com/sixohsix/twitter

安裝

pip install twitter


使用

操作上可以直接觀看 github 上 Readme 的例子,主要的核心就是 twitter 這個 class。
https://github.com/sixohsix/twitter

方便與直覺之處就是,你大概直接看官方的api docs,就可以了解有什麼參數可以直接使用。

這是一種滿直覺的封裝方式,沒有太多自己特製的封裝,wrap一層後可以直接看官方手冊。


The Twitter class

The minimalist yet fully featured Twitter API class.
Get RESTful data by accessing members of this class. The result is decoded python objects (lists and dicts).
The Twitter API is documented at:
Examples:
from twitter import *

t = Twitter(
    auth=OAuth(token, token_key, con_secret, con_secret_key))

# Get your "home" timeline
t.statuses.home_timeline()

# Get a particular friend's timeline
t.statuses.user_timeline(screen_name="billybob")

# to pass in GET/POST parameters, such as `count`
t.statuses.home_timeline(count=5)

# to pass in the GET/POST parameter `id` you need to use `_id`
t.statuses.oembed(_id=1234567890)

# Update your status
t.statuses.update(
    status="Using @sixohsix's sweet Python Twitter Tools.")

# Send a direct message
t.direct_messages.new(
    user="billybob",
    text="I think yer swell!")

# Get the members of tamtar's list "Things That Are Rad"
t.lists.members(owner_screen_name="tamtar", slug="things-that-are-rad")

# An *optional* `_timeout` parameter can also be used for API
# calls which take much more time than normal or twitter stops
# responding for some reason:
t.users.lookup(
    screen_name=','.join(A_LIST_OF_100_SCREEN_NAMES), _timeout=1)

# Overriding Method: GET/POST
# you should not need to use this method as this library properly
# detects whether GET or POST should be used, Nevertheless
# to force a particular method, use `_method`
t.statuses.oembed(_id=1234567890, _method='GET')

# Send images along with your tweets:
# - first just read images from the web or from files the regular way:
with open("example.png", "rb") as imagefile:
    imagedata = imagefile.read()
# - then upload medias one by one on Twitter's dedicated server
#   and collect each one's id:
t_up = Twitter(domain='upload.twitter.com',
    auth=OAuth(token, token_key, con_secret, con_secret_key))
id_img1 = t_up.media.upload(media=imagedata)["media_id_string"]
id_img2 = t_up.media.upload(media=imagedata)["media_id_string"]
# - finally send your tweet with the list of media ids:
t.statuses.update(status="PTT ★", media_ids=",".join([id_img1, id_img2]))

# Or send a tweet with an image (or set a logo/banner similarily)
# using the old deprecated method that will probably disappear some day
params = {"media[]": imagedata, "status": "PTT ★"}
# Or for an image encoded as base64:
params = {"media[]": base64_image, "status": "PTT ★", "_base64": True}
t.statuses.update_with_media(**params)
Searching Twitter:
# Search for the latest tweets about #pycon
t.search.tweets(q="#pycon")

沒有留言:

張貼留言