[PYTHON] Get tweets with Google Cloud Function and automatically save images to Google Photos

Introduction

Recently, it's a hassle to download Twitter images one by one while working and bring them to Google Photos. It is a record that if you are an engineer, you can automate it.

I just want an image of an idol who wants to download a Twitter image. It has no other purpose.

Final goal

Get tweets regularly → Download if there is an image → Upload the downloaded image to Google Photos.

environment

Google Cloud Functions https://cloud.google.com/functions/?hl=ja

A service that executes a specific function. It's more like a small program than running a large program. This time, the program has about 120 lines, so I chose this one.

If the price is not good, it will not exceed the permanent free frame (I think that even if you make a lot of functions, it will not exceed it). Since this program causes communication outside the Google API (Tweet acquisition and image download), we are taking measures to reduce the execution frequency. However, since we do not know where to look at network usage, it is difficult to know how much communication is actually occurring even now that we have started operation.

The language set for the function this time is python. The reason is that I have been using it for business recently. There are other languages that can be used, so let's check before starting development (I will not write it here because it may change)

The price list is here.

Google Cloud Scheduler https://cloud.google.com/scheduler/?hl=ja

It's easy to connect with Google Cloud Functions, so I'll leave it to this guy for regular execution. The fee is determined by the number of jobs, but it is a service that is cheap and easy to use even if it exceeds the free frame.

The price list is here.

Before developing

This time we will use GCP, Google API (Photos), Twitter API and various APIs, so we will make various settings.

GCP related

https://cloud.google.com/?hl=ja

This is not particularly troublesome to set up, so it finishes quickly.

Google Photos API https://developers.google.com/photos

You need to enable the API from around the URL above. The activation itself is easy, but you need to authorize using OAuth to call the API. Here Needs to be obtained until the Refresh Token is obtained, so let's do it in advance.

Twitter API https://developer.twitter.com/content/developer-twitter/ja.html

This was the most troublesome. The operation is easy, but since the application explanation in English is required when registering as a developer, the explanation must be written in English for a certain length. I have the impression that the policy when creating an app changes considerably, so please be careful when working.

There was information that Japanese was fine, but as of January 2020, English was mandatory.

Even people who are not very English can break through by translating simple Japanese into Google Translate, so you don't have to think too hard. It's definitely more troublesome than writing in Japanese.

The examination itself was completed in an instant. Maybe it depends on how you choose your options, so it may be different for each person.

Implementation

The program itself is listed on github, so please see that for the completed version. If you create an environment variable or .env, you can execute it locally.

In terms of processing, I use python-twitter to get the tweet of USER_LIST defined in the environment variable, and if there is an image, I just upload it from the download.

Due to the specifications of the Google Photos API, there is no problem even if you give the same image (probably judged by the file name), so if there is an image in the tweet you got, you have uploaded it with the same procedure. It's useless communication, but I do it because I'm not in trouble for the time being. If it is true, it would be better to make the program save the id somewhere and get it from there, but I gave priority to the release because it seems to take a little time.

It is recommended to save it in Firebase because it can be executed locally because it is often done for free using GCP system.

Official is very easy to understand how to use Firebase from python code, so please refer to this. Please refer to the Article for how to test locally.

Cloud SQL doesn't have Always Free, so I gave up this time.

2020/02/01 postscript Due to the specifications of Twitter, if you do not set tweet_mode = "extended", you will get chopped text. Please note that the media will not be set properly for such a Tweet, so you must set Tweet_mode = "extended".

The source of Github has been updated.

Implementation notes

Note that event triggers require arguments for the starting function.

pub/sub => event, context http => request, response

is. I forgot and got hooked a little. Also, since only two files, MAIN.PY and REQUIREMENTS.TXT, can be set, it must be implemented within that range. Writing a complicated program is painful.

If you need a log, print is fine, so let's prepare it.

test

If everything can be done locally, I think it's easier to confirm that it works locally. It seems that it can be done on Cloud Functions, but as mentioned above, it is impossible to encounter a mysterious error.

2020/04/17 postscript The test on Cloud Functions was because the timeout setting was set by default. For programs with long execution times, the test here will result in an error. However, it seems that the execution itself is done, so you can check in the log whether you can run completely.

Google Cloud Functions settings

The memory settings change depending on the program, but with this program, 256MB was enough. Triggers are created in Cloud Pub / Sub. You can set a maximum timeout of 518 seconds, so you can rest assured that you set the maximum for the time being. This time the program took about 200 seconds, so I did it in 60 seconds and timed out.

Google Cloud Scheduler settings

Just set the trigger to pub / sub and the topic to the value set in Google Cloud Functions. The time to execute is written by the cron method. I set it only occasionally, so I don't feel like learning how to write it, but there is a site like this, so let's use it.

If you want to run it right away, press the button and it will run, so make sure it succeeds.

Finally

It's easy to run small programs on the server on a regular basis, so it's great for automating small things. This time, it took about 3 hours to implement and 1 hour to set up, so it was very meaningful to be able to do hobbies, practical benefits and study in a short time.

There are still issues around Firebase and Cloud Functions testing, but we'll work on them as soon as we know them. I think I will write an article when it is over.

Recommended Posts

Get tweets with Google Cloud Function and automatically save images to Google Photos
I tried to read and save automatically with VOICEROID2 2
I tried to automatically read and save with VOICEROID2
Upload and delete files to Google Cloud Storages with django-storage
Get replies to specific tweets with tweepy
Function to save images by date [python3]
Add images to iOS photos with Pythonista
Upload images to Google Drive with Python
How to search using python's astroquery and get fits images with skyview
Automatically save images and videos hit by Twitter search to iPhone using Pythonista3
Automatically save images of your favorite characters from Google Image Search with Python
Save Twitter's tweets with Geo in CSV and plot them on Google Map.
Get data labels by linking with Google Cloud Vision API when previewing images with Rails
[Memorandum] ① Get and save tweets ~ I want to identify the news tweets that are spread ~
Get media timeline images and videos with Python + Tweepy
Get twitter tweets, followers, followers, likes, with urllib and beautifulSoup
Get Tweets with Tweepy
Easy to use Nifty Cloud API with botocore and python
LGTM --Compose LGTM images with videos and photos and output GIF animation
Cloud Functions to resize images using OpenCV with Cloud Storage triggers
Create a Mastodon bot with a function to automatically reply with Python
kobitonote.py --Synchronize and save items edited with Kobito to Evernote
Send experiment results (text and images) to slack with Python
Convert garbled scanned images to PDF with Pillow and PyPDF
Get a global IP and export it to Google Spreadsheets
Try to determine food photos using Google Cloud Vision API
Save images on the web to Drive with Python (Colab)
Get additional data to LDAP with python (Writer and Reader)
[Introduction to Python] How to get data with the listdir function
How to log in to AtCoder with Python and submit automatically
Automatically download images with scraping
Save images with web scraping
Save the results of crawling with Scrapy to the Google Data Store
Apache Beam 2.0.x with Google Cloud Dataflow starting with IntelliJ and Gradle
Correspondence analysis of sentences with COTOHA API and save to file
How to connect to Cloud Firestore from Google Cloud Functions with python code
Try to display google map and geospatial information authority map with python
The strongest way to use MeCab and CaboCha with Google Colab
Get OCTA simulation conditions from a file and save with pandas
I tried to automatically collect images of Kanna Hashimoto with Python! !!
The fastest way to get camera images regularly with python opencv
[Python] Try to recognize characters from images with OpenCV and pyocr
Download the images and videos included in the tweets you liked on Twitter and upload them to Google Drive