How to use Tweepy ~ Part 2 ~ [Follow, like, etc.]

Introduction

I will summarize how to use tweepy with reference to the official document.

Advance preparation

import tweepy
#Obtained API key
consumer_key = '*********************************'
consumer_secret = '*************************************'
access_token = '*******************************************'
access_token_secret = '****************************************'

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

Follow / follower

Follow, display number of followers

The User type of your account is returned by ʻapi.me ()as shown below. Looking at the returned value, it looks like'followers_count': 112, so you can get the value by doing ʻapi.me (). Followers_count. By looking at this dict type-like User type key, you can get other information as well.

api.me()
#It's long and can be ignored.
>> User(_api=<tweepy.api.API object at 0x7efcf29adc50>, _json={'id': 1147865430297468928, 'id_str': '1147865430297468928', 'name': 'Kita', 'screen_name': 'ki_rii9', 'location': 'Fukuoka, Japan', 'profile_location': None, 'description': 'python/Machine learning/ flask/ Atcoder/Beginner Competitive Pro er Beginner kaggler/Kyushu University B1/ GLEAP #TORYUMON #Chare character', 'url': 'https://t.co/6QXllpbNqu', 'entities': {'url': {'urls': [{'url': 'https://t.co/6QXllpbNqu', 'expanded_url': 'https://qiita.com/kitarikes', 'display_url': 'qiita.com/kitarikes', 'indices': [0, 23]}]}, 'description': {'urls': []}}, 'protected': False, 'followers_count': 112, 'friends_count': 268, 'listed_count': 1, 'created_at': 'Sun Jul 07 13:50:19 +0000 2019', 'favourites_count': 807, 'utc_offset': None, 'time_zone': None, 'geo_enabled': False, 'verified': False, 'statuses_count': 135, 'lang': None, 'status': {'created_at': 'Thu Dec 26 17:50:38 +0000 2019', 'id': 1210256620657102849, 'id_str': '1210256620657102849', 'text': 'Completed because React was added without knowing! !\n You have completed the "React Learning Course IV" course! https://t.co/dAKcZhNNMS #Progate', 'truncated': False, 'entities': {'hashtags': [{'text': 'Progate', 'indices': [79, 87]}], 'symbols': [], 'user_mentions': [], 'urls': [{'url': 'https://t.co/dAKcZhNNMS', 'expanded_url': 'https://prog-8.com/lessons/react/study/4', 'display_url': 'prog-8.com/lessons/react/…', 'indices': [55, 78]}]}, 'source': '<a href="http://twitter.com" rel="nofollow">Twitter Web Client</a>', 'in_reply_to_status_id': None, 'in_reply_to_status_id_str': None, 'in_reply_to_user_id': None, 'in_reply_to_user_id_str': None, 'in_reply_to_screen_name': None, 'geo': None, 'coordinates': None, 'place': None, 'contributors': None, 'is_quote_status': False, 'retweet_count': 0, 'favorite_count': 6, 'favorited': False, 'retweeted': False, 'possibly_sensitive': True, 'lang': 'ja'}, 'contributors_enabled': False, 'is_translator': False, 'is_translation_enabled': False, 'profile_background_color': 'F5F8FA', 'profile_background_image_url': None, 'profile_background_image_url_https': None, 'profile_background_tile': False, 'profile_image_url': 'http://pbs.twimg.com/profile_images/1189529639242223622/YrkeODbb_normal.jpg', 'profile_image_url_https': 'https://pbs.twimg.com/profile_images/1189529639242223622/YrkeODbb_normal.jpg', 'profile_banner_url': 'https://pbs.twimg.com/profile_banners/1147865430297468928/1570725567', 'profile_link_color': '1DA1F2', 'profile_sidebar_border_color': 'C0DEED', 'profile_sidebar_fill_color': 'DDEEF6', 'profile_text_color': '333333', 'profile_use_background_image': True, 'has_extended_profile': True, 'default_profile': True, 'default_profile_image': False, 'can_media_tag': True, 'followed_by': False, 'following': False, 'follow_request_sent': False, 'notifications': False, 'translator_type': 'none', 'suspended': False, 'needs_phone_verification': False}, id=1147865430297468928, id_str='1147865430297468928', name='Kita', screen_name='ki_rii9', location='Fukuoka, Japan', profile_location=None, description='python/Machine learning/ flask/ Atcoder/Beginner Competitive Pro er Beginner kaggler/Kyushu University B1/ GLEAP #TORYUMON #Chare character', url='https://t.co/6QXllpbNqu', entities={'url': {'urls': [{'url': 'https://t.co/6QXllpbNqu', 'expanded_url': 'https://qiita.com/kitarikes', 'display_url': 'qiita.com/kitarikes', 'indices': [0, 23]}]}, 'description': {'urls': []}}, protected=False, followers_count=112, friends_count=268, listed_count=1, created_at=datetime.datetime(2019, 7, 7, 13, 50, 19), favourites_count=807, utc_offset=None, time_zone=None, geo_enabled=False, verified=False, statuses_count=135, lang=None, status=Status(_api=<tweepy.api.API object at 0x7efcf29adc50>, _json={'created_at': 'Thu Dec 26 17:50:38 +0000 2019', 'id': 1210256620657102849, 'id_str': '1210256620657102849', 'text': 'Completed because React was added without knowing! !\n You have completed the "React Learning Course IV" course! https://t.co/dAKcZhNNMS #Progate', 'truncated': False, 'entities': {'hashtags': [{'text': 'Progate', 'indices': [79, 87]}], 'symbols': [], 'user_mentions': [], 'urls': [{'url': 'https://t.co/dAKcZhNNMS', 'expanded_url': 'https://prog-8.com/lessons/react/study/4', 'display_url': 'prog-8.com/lessons/react/…', 'indices': [55, 78]}]}, 'source': '<a href="http://twitter.com" rel="nofollow">Twitter Web Client</a>', 'in_reply_to_status_id': None, 'in_reply_to_status_id_str': None, 'in_reply_to_user_id': None, 'in_reply_to_user_id_str': None, 'in_reply_to_screen_name': None, 'geo': None, 'coordinates': None, 'place': None, 'contributors': None, 'is_quote_status': False, 'retweet_count': 0, 'favorite_count': 6, 'favorited': False, 'retweeted': False, 'possibly_sensitive': True, 'lang': 'ja'}, created_at=datetime.datetime(2019, 12, 26, 17, 50, 38), id=1210256620657102849, id_str='1210256620657102849', text='Completed because React was added without knowing! !\n You have completed the "React Learning Course IV" course! https://t.co/dAKcZhNNMS #Progate', truncated=False, entities={'hashtags': [{'text': 'Progate', 'indices': [79, 87]}], 'symbols': [], 'user_mentions': [], 'urls': [{'url': 'https://t.co/dAKcZhNNMS', 'expanded_url': 'https://prog-8.com/lessons/react/study/4', 'display_url': 'prog-8.com/lessons/react/…', 'indices': [55, 78]}]}, source='Twitter Web Client', source_url='http://twitter.com', in_reply_to_status_id=None, in_reply_to_status_id_str=None, in_reply_to_user_id=None, in_reply_to_user_id_str=None, in_reply_to_screen_name=None, geo=None, coordinates=None, place=None, contributors=None, is_quote_status=False, retweet_count=0, favorite_count=6, favorited=False, retweeted=False, possibly_sensitive=True, lang='ja'), contributors_enabled=False, is_translator=False, is_translation_enabled=False, profile_background_color='F5F8FA', profile_background_image_url=None, profile_background_image_url_https=None, profile_background_tile=False, profile_image_url='http://pbs.twimg.com/profile_images/1189529639242223622/YrkeODbb_normal.jpg', profile_image_url_https='https://pbs.twimg.com/profile_images/1189529639242223622/YrkeODbb_normal.jpg', profile_banner_url='https://pbs.twimg.com/profile_banners/1147865430297468928/1570725567', profile_link_color='1DA1F2', profile_sidebar_border_color='C0DEED', profile_sidebar_fill_color='DDEEF6', profile_text_color='333333', profile_use_background_image=True, has_extended_profile=True, default_profile=True, default_profile_image=False, can_media_tag=True, followed_by=False, following=False, follow_request_sent=False, notifications=False, translator_type='none', suspended=False, needs_phone_verification=False)

#Example
api.me().followers_count
#Check the number of followers
>> 112

api.me().friends_count
#Check the number of followers
>> 268

api.me().description
#Profile comment display
>> 'python/Machine learning/ flask/ Atcoder/Beginner Competitive Pro er Beginner kaggler/Kyushu University B1/ GLEAP #TORYUMON #Chare character'

Follow, get user information of followers

You can get information about the users you are following with ʻapi.friends (). By default, you can get 20 items, but you can specify the maximum value by setting count = 100` as an argument.

len(api.friends())
#Get a list of user types of users you are following
>> 20

len(api.friends(count=100))
>> 100

Since this returns a list type, you can perform loop processing using the for statement as it is.

for user in api.friends(count=100):
  if user.followed_by :
    #Show only users who are following back
    print(user.name)

len([user.name for user in api.friends(count=100) if user.followed_by])

>> 40

It's uncomfortable to display the name with user.name, so just the number with len () ...... ~~ (40/100) ~~

Follow the specified user

You can follow it with ʻapi.create_friendship ()`. It can be specified by id (a unique id that can be obtained by User type .id) or screen_name (@ or less) as an argument. In the example below, we will follow the official account of qiita.

api.create_friendship(screen_name='Qiita')

Similarly, you can unfollow with ʻapi.destroy_friendship (id / screen_name / user_id), and you can see the follow relationship between accounts with ʻapi.show_friendship (source_id / source_screen_name, target_id / target_screen_name). (For details, see Documentation)

How nice!

View Liked Tweets

You can see the tweets you liked with ʻapi.favorites ()`. By default, the likes information of your account is returned, and as before, specify the user with id and screen_name, and specify the maximum number to get with count.

#Example
api.favorites(screen_name='ki_rii9', count=50)
#Caution:This is my account. Thank you for following me.(?)

Like to tweet!

Like ʻapi.create_favorite (id)`! I can do it. As a test, I'll like the tweet at the top of the timeline.

api.create_favorite(id=api.home_timeline()[0].id)

It can also be canceled with ʻapi.destroy_favorite (id)`.

in conclusion

Like to follow and tweet using API! Can now be done. Next time (if there is next time ...) I will try to increase followers using automatic likes and followers.

Recommended Posts

How to use Tweepy ~ Part 2 ~ [Follow, like, etc.]
How to use Tweepy ~ Part 1 ~ [Getting Tweet]
How to use cybozu.com developer network (Part 2)
How to use xml.etree.ElementTree
How to use Python-shell
How to use tf.data
How to use Seaboan
How to use image-match
How to use shogun
How to use Pandas 2
How to use Virtualenv
How to use numpy.vectorize
How to use pytest_report_header
How to use partial
How to use Bio.Phylo
How to use x-means
How to use WikiExtractor.py
How to use IPython
How to use virtualenv
How to use Matplotlib
How to use iptables
How to use numpy
How to use TokyoTechFes2015
How to use venv
How to use dictionary {}
How to use Pyenv
How to use list []
How to use python-kabusapi
How to use OptParse
How to use return
How to use dotenv
How to use pyenv-virtualenv
How to use Go.mod
How to use imutils
How to use import
How to use Qt Designer
How to use search sorted
[gensim] How to use Doc2Vec
python3: How to use bottle (2)
Understand how to use django-filter
How to use the generator
[Python] How to use list 1
How to use FastAPI ③ OpenAPI
How to use Python argparse
How to use IPython Notebook
How to use Pandas Rolling
[Note] How to use virtualenv
How to use redis-py Dictionaries
Python: How to use pydub
[Python] How to use checkio
[Go] How to use "... (3 periods)"
How to use Django's GeoIp2
[Python] How to use input ()
How to use the decorator
[Introduction] How to use open3d
How to use Python lambda
How to use Jupyter Notebook
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Google Colaboratory