Python+Twitter API①

 
 
[code lang=”python”]
import json, config #標準のjsonモジュールとconfig.pyの読み込み
from requests_oauthlib import OAuth1Session #OAuthのライブラリの読み込み

CK = config.CONSUMER_KEY
CS = config.CONSUMER_SECRET
AT = config.ACCESS_TOKEN
ATS = config.ACCESS_TOKEN_SECRET

twitter = OAuth1Session(CK, CS, AT, ATS) #認証処理
[/code]

 

リツイート

[code lang=”python”]

tweetid = "リツイート対象のID"

url = ‘https://api.twitter.com/1.1/statuses/retweet/’ + tweetid + ‘.json’

param = {‘id’:tweetid}

twitter.post(url, params = param)
[/code]

お気に入り

[code lang=”python”]
url = ‘https://api.twitter.com/1.1/favorites/create.json’

tid = "対象のID"

param = {‘id’:tid}

twitter.post(url, params = param)
[/code]

特定ユーザーのタイムラインを取得し、表示する

[code lang=”python”]
import json, config #標準のjsonモジュールとconfig.pyの読み込み
from requests_oauthlib import OAuth1Session #OAuthのライブラリの読み込み

CK = config.CONSUMER_KEY
CS = config.CONSUMER_SECRET
AT = config.ACCESS_TOKEN
ATS = config.ACCESS_TOKEN_SECRET
twitter = OAuth1Session(CK, CS, AT, ATS) #認証処理

url = ‘https://api.twitter.com/1.1/statuses/user_timeline.json’

tid = "1073949940764762115"

param = {‘screen_name’:’watanabe_naomi’,
‘count’:’5′}

req = twitter.get(url, params = param)

if req.status_code == 200:
timeline = json.loads(req.text)
for tweet in timeline:
print(tweet[‘user’][‘name’]+’::’+tweet[‘text’])
print(tweet[‘created_at’])
print(‘—————————————————-‘)
else:
print("ERROR: %d" % req.status_code)
[/code]

キーワードで検索し表示する

[code lang=”python”]
url = "https://api.twitter.com/1.1/search/tweets.json"

keyword = ‘鬱状態’

params = {‘q’ : keyword, ‘count’ : 5}

req = twitter.get(url, params = params)

if req.status_code == 200:
search_timeline = json.loads(req.text)
for tweet in search_timeline[‘statuses’]:
print(tweet[‘user’][‘name’] + ‘::’ + tweet[‘text’])
print(tweet[‘created_at’])
print(‘—————————————————-‘)
else:
print("ERROR: %d" % req.status_code)
[/code]

トレンドワードの取得

[code lang=”python”]
url = "https://api.twitter.com/1.1/trends/place.json"

params = {‘id’:1118370} #対象地域のID

req = twitter.get(url, params = params)

tl = json.loads(req.text)

print(tl)

#トレンドワードに関する情報がJSONファイルで取得できているのが分かると思います
#必要な情報を抽出すればよいと思います

[/code]

特例のツイートをリツイートしたユーザーの情報を取得

[code lang=”python”]
twitter = OAuth1Session(CK, CS, AT, ATS) #認証処

tweetid = "1093882892214624262"

url = "https://api.twitter.com/1.1/statuses/retweets/" + tweetid + ".json"

params = {"id":tweetid,"count":10} #10件取得

req = twitter.get(url, params = params)

tl = json.loads(req.text) #辞書へ返還

for result in tl:
print(result["id"]) #ユーザーIDを取得

bbb = open(‘aaa2.json’,’w’)
json.dump(tl, bbb) #JSONファイルに書き込み

[/code]

リツイートを取り消す

[code lang=”python”]
twitter = OAuth1Session(CK, CS, AT, ATS) #認証処理

tweetid = "12341234xxxxxxx" #リツイートの対象ツイートのID

url = "https://api.twitter.com/1.1/statuses/unretweet/" + tweetid + ".json"

params = {"id":tweetid}

res = twitter.post(url, params = params)
[/code]

検索し、JSONに保存

[code lang=”python”]
twitter = OAuth1Session(CK, CS, AT, ATS) #認証処理

url = "https://api.twitter.com/1.1/search/tweets.json"

keyword = ‘テレビ’

count = 10

lang = ‘ja’

type = ‘mixed’ #デフォルトはmixed

params = {‘q’ : keyword, ‘count’ : count,’lang’:lang ,’result_type’:type}

req = twitter.get(url, params = params)

if req.status_code == 200:
search_timeline = json.loads(req.text)
for tweet in search_timeline[‘statuses’]:
print(tweet[‘user’][‘name’] + ‘::’ + tweet[‘text’])
print(tweet[‘created_at’])
print(tweet["id"])
print(‘—————————————————-‘)
else:
print("ERROR: %d" % req.status_code)

tl = json.loads(req.text)

contents = open(‘filename.json’,’w’)
json.dump(tl, contents)
[/code]

[code lang=”python”]

[/code]

[code lang=”python”]

[/code]

 
 
 
 
 
 

tweepy, twitter api

Posted by iser