[PYTHON] I want to be healed by Mia Nanasawa's image. In such a case, hit the Twitter API ♪

Introduction

This time, we will deal with ** Twiiter API ** in Web API. If you use the Twiiter API, you will be able to perform the following two points automatically.

① Get posts that meet specific conditions ② Post automatically with your account

This time I will execute ①.

I will automatically acquire the posted images from the Tweets of ** Mia Nanasawa ** who is active as an AV actress and is indebted to me the most.

Get Twitter Keys & Tokens

consumer_key = "???"
consumer_secret = "???"
access_token = "???"
access_token_secret = "???"
#Get each???Substitute in.

Get Mia Nanasawa's Twitter account & number of followers

For details of the code below https://kurozumi.github.io/tweepy/getting_started.html#hello-tweepy It is written in.

def show_user_profile():
  user = api.get_user('mia_nanasawa')
  print(user.screen_name) #Get account name
  print(user.followers_count) #Get followers
データ

From the Twitter image below, you can see that you have correctly obtained ** account name ** and ** number of followers **.

データ

Get Tweet information of Mia Nanasawa (multiple images)

How to use user_timeline https://kurozumi.github.io/tweepy/api.html It is written in.

def show_media_url():
  user_id = "mia_nanasawa"
  statuses = api.user_timeline(id=user_id, count=4)

  count = 1
  for status in statuses:
    for entity in status.extended_entities["media"]:
      img_url = entity["media_url"]
      print(img_url)
    break

Download image

def download_image(url, file_path):
  r = requests.get(url, stream=True)

  if r.status_code == 200:
    with open(file_path, "wb") as f:
      f.write(r.content)

Run

How to use Cursor https://kurozumi.github.io/tweepy/cursor_tutorial.html It is written in.

def main():
  user_id = "mia_nanasawa"
  for page in tweepy.Cursor(api.user_timeline, id=user_id).pages(20):
    for status in page:
      try:
        for media in status.extended_entities["media"]:
          media_id = media["id"]
          img_url = media["media_url"]
          print(media_id)
          print(img_url)

          #In the current directory"Create an "images folder".
          download_image(url=img_url, file_path="./images/{}.jpg ".format(media_id))

      #If an error occurs during the try, an exception will be output and the loop will be executed.
      except Exception as e:
        print(e)
        #There may be an error when tweeting with a video.

if __name__ == "__main__":
    main()
データ

From the above output, you can see that some ** error handling ** has occurred. It is likely that the Tweet contains ** videos ** rather than image files. (By the way, the tweeted image has been acquired correctly.)

result

データ

I was able to confirm that it was saved correctly in the images file.

Now you have an eye candy.

Recommended Posts

I want to be healed by Mia Nanasawa's image. In such a case, hit the Twitter API ♪
Python program is slow! I want to speed up! In such a case ...
I want to see the graph in 3D! I can make such a dream come true.
I want to print in a comprehension
I want to change the color by clicking the scatter point in matplotlib
I want to be cursed by a pretty girl every time I sudo! !!
I want to sort a list in the order of other lists
[LINE Messaging API] I want to send a message from the program to everyone's LINE
I want to create an API that returns a model with a recursive relationship in the Django REST Framework
I want to create a window in Python
I want to create a priority queue that can be updated in Python (2.7)
I want to set a life cycle in the task definition of ECS
I want to see a list of WebDAV files in the Requests module
I thought it would be slow to use a for statement in NumPy, but that wasn't the case.
I want to display the progress in Python!
I want to use a network defined by myself in PPO2 of Stable Baselines
Use Twitter API to reduce the time taken by Twitter (create a highlighting (like) timeline)
I want to easily implement a timeout in python
I want DQN Puniki to hit a home run
100 image processing knocks !! (021-030) I want to take a break ...
I want to transition with a button in flask
I want to write in Python! (2) Let's write a test
I want to randomly sample a file in Python
I want to work with a robot in python.
I want to write in Python! (3) Utilize the mock
I want to use the R dataset in python
I tried to create a RESTful API by connecting the explosive Python framework FastAPI to MySQL.
I made a class to get the analysis result by MeCab in ndarray with python
I want to save the photos sent by LINE to S3
I want to make the Dictionary type in the List unique
I want to align the significant figures in the Numpy array
I want Sphinx to be convenient and used by everyone
I want to be able to run Python in VS Code
I want to make input () a nice complement in python
I want to create a Dockerfile for the time being.
I wrote a script that splits the image in two
I didn't want to write the AWS key in the program
I want to improve efficiency with Python even in the experimental system (5) I want to send a notification at the end of the experiment with the slack API
I want to create a graph with wavy lines omitted in the middle with matplotlib (I want to manipulate the impression)
[Twitter] I want to make the downloaded past tweets (of my account) into a beautiful CSV
I want to save the trouble of inputting when debugging Paiza's skill check example in a local environment such as Jupyter [Python]
An easy way to hit the Amazon Product API in Python
I want to record the execution time and keep a log.
Export the contents acquired by Twitter Streaming API in JSON format
[Linux] I want to know the date when the user logged in
I created a Python library to call the LINE WORKS API
I tried to process the image in "sketch style" with OpenCV
Hit the New Relic API in Python to get the server status
LINEbot development, I want to check the operation in the local environment
I want to create a system to prevent forgetting to tighten the key 1
I want to create a pipfile and reflect it in docker
I tried to process the image in "pencil style" with OpenCV
I want to make the second line the column name in pandas
I tried to cut out a still image from the video
I want to pass the G test in one month Day 1
I want to know the population of each country in the world.
I made a command to display a colorful calendar in the terminal
A note that you want to manually decorate the parameters passed in the Django template form item by item
I want to receive the configuration file and check if the JSON file generated by jinja2 is a valid JSON
I want to load the pytest fixture as a library somewhere else (pytest may not exist in the environment)
I want to use complicated four arithmetic operations in the IF statement of the Django template! → Use a custom template