フォローイング/フォロワーの作ったリストをリストにする。めちゃくちゃ時間がかかる。
This file contains 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
import tweepy | |
from progressbar import ProgressBar | |
from requests_oauthlib import OAuth1Session | |
import argparse | |
from time import sleep | |
import json | |
argparser = argparse.ArgumentParser() | |
argparser.add_argument("api_key") | |
argparser.add_argument("api_secret") | |
argparser.add_argument("access_token") | |
argparser.add_argument("access_secret") | |
argparser.add_argument("-o","--output_file",default="output.markdown") | |
args = argparser.parse_args() | |
def output_append(text): | |
f = open(args.output_file,'a') | |
f.write(text) | |
f.close() | |
auth = tweepy.OAuthHandler(args.api_key, args.api_secret) | |
auth.set_access_token(args.access_token, args.access_secret) | |
api = tweepy.API(auth_handler=auth, wait_on_rate_limit=True) | |
user_ids = set() | |
my = api.me() | |
print('Retrieving @'+ my.screen_name +'’s friends ids...') | |
for friend in tweepy.Cursor(api.friends_ids).items(): | |
user_ids.add(friend) | |
print ('Retrieving @'+ my.screen_name +'’s followers ids...') | |
for follower in tweepy.Cursor(api.followers_ids).items(): | |
user_ids.add(follower) | |
print(str(len(user_ids)) + ' users\nRetrieving... ') | |
progbar = ProgressBar(max_value=len(user_ids)) | |
session = OAuth1Session(args.api_key, args.api_secret, args.access_token, args.access_secret) | |
api_url = 'https://api.twitter.com/1.1/lists/ownerships.json' | |
output_append("#フォロワー/フォローイングの作成したリスト一覧\n") | |
for i, user in enumerate(user_ids): | |
progbar.update(i+1) | |
params = {'user_id': str(user)} | |
req = session.get(api_url, params=params) | |
if req.status_code == 200: | |
jsn = json.loads(req.text) | |
output_append('##%s\n' % api.get_user(user).name) | |
for item in jsn['lists']: | |
output_append('###[%s](https://twitter.com%s)\n%d人が追加されています\n\n%s\n\n' % (item['name'], item['uri'], item['member_count'], item['description'])) | |
else: | |
print('Error: %d' % req.status_code) | |
sleep(90) | |
progbar.finish() |
This file contains 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
oauthlib==1.1.2 | |
progressbar2==3.6.2 | |
requests==2.10.0 | |
requests-oauthlib==0.6.1 | |
six==1.10.0 | |
tweepy==3.5.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment