Last active
December 20, 2015 09:28
-
-
Save yassu/560b35aed2a1e81fc3f5 to your computer and use it in GitHub Desktop.
友達とのtwitterでの絡み具合を調べる
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"aiya": ["aiya", "あいや", "@aiya_000", "@public_ai000ya"], | |
"カワズ": ["カワズ", "@MrBoilingFrog"], | |
"ピヨ": ["@cyclic_select", "ピヨ"] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# 友達とのtwitterでの絡み具合を調べる | |
from requests_oauthlib import OAuth1Session | |
import json | |
from pprint import pprint | |
import private | |
# get response | |
CK = private.consumer_key | |
CS = private.consumer_secret | |
AT = private.access_token | |
AS = private.access_token_secret | |
# タイムライン取得用のURL | |
url = "https://api.twitter.com/1.1/statuses/user_timeline.json" | |
# レスポンスの取得 | |
params = {'count': 200} | |
twitter = OAuth1Session(CK, CS, AT, AS) | |
req = twitter.get(url, params=params) | |
# レスポンスが200以外ならばエラーコードを出力して終了 | |
if req.status_code != 200: | |
print("Error: %d" % req.status_code) | |
# 友達のデータの取得 | |
friends = json.load(open("collect_texts.json")) | |
friend_counts = {friend: 0 for friend in friends} | |
# 友達関連のツイート数を数える | |
timeline = json.loads(req.text) | |
for tweet in timeline: | |
try: | |
writer = tweet["retweeted_status"]["user"]["screen_name"] | |
except KeyError: | |
writer = tweet["user"]["screen_name"] | |
for friend, friend_texts in friends.items(): | |
for friend_text in friend_texts: | |
if friend_text in tweet["text"]: | |
friend_counts[friend] += 1 | |
break | |
# パーセンテージに変換して出力 | |
for friend in friends: | |
percent = friend_counts[friend] / len(timeline) * 100 | |
print("{}: {}%".format(friend, percent)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
aiya: 6.5% | |
カワズ: 21.0% | |
ピヨ: 2.5% |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment