Search and save images of Chino Kafu from Twitter

Using the Twitter API library for Python called Tweepy, from Twitter, Chino-chan, Gochiusa [Incense I created a script to search and save the image of Fu Chino.

Twitter API preparation

Refer to Summary of how to use Twitter API to register the application and obtain various Twitter API Keys.

Preparing Tweepy

You can install Tweepy with the following command (for pip).

$ pip install tweepy

How to use Tweepy is described in Tweepy Documentation.

For example, to get and display your own timeline, do the following.

import tweepy

CONSUMER_KEY        = 'Your Consumer Key'
CONSUMER_SECRET     = 'Your Consumer Secret'
ACCESS_TOKEN_KEY    = 'Your Access Token'
ACCESS_TOKEN_SECRET = 'Your Access Token Secret'

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN_KEY, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)

public_tweets = api.home_timeline()
for tweet in public_tweets:
    print tweet.text

Search & get images

Use ʻAPI.search ()` to search for keywords in Tweepy. To get the URL of the image from the search results, do as follows.

search_result = api.search(q=term)
for result in search_result:
    if result.entities.has_key('media'):
        for media in result.entities['media']:
            print media['media_url']

Finally, I download the image from the URL of the acquired image, If you add : orig to the end of the URL of the image, you can get the original image that has not been reduced. (Reference: You can get the original image by adding: orig to the image URL of Twitter (in some cases) --Yaruki Denized)

Created script

chino_image_downloader.py


# -*- coding: utf-8 -*-
import os
import tweepy
import urllib2

#=Image storage directory
IMAGES_DIR = './images/'

#=Twitter API Key settings
CONSUMER_KEY        = os.environ.get('TWITTER_CONSUMER_KEY')
CONSUMER_SECRET     = os.environ.get('TWITTER_CONSUMER_SECRET')
ACCESS_TOKEN_KEY    = os.environ.get('TWITTER_ACCESS_TOKEN_KEY')
ACCESS_TOKEN_SECRET = os.environ.get('TWITTER_ACCESS_TOKEN_SECRET')

#=Search keyword
KEYWORDS = ['Kafu Chino', 'Chino-chan']

#=Search options
RETURN_PAR_PAGE = 100
NUMBER_OF_PAGES = 10

class ChinoImageDownloader(object):
    def __init__(self):
        super(ChinoImageDownloader, self).__init__()
        self.set_twitter_api()
        self.media_url_list = []

    def run(self):
        for keyword in KEYWORDS:
            self.max_id = None
            for page in range(NUMBER_OF_PAGES):
                self.download_url_list = []
                self.search(keyword, RETURN_PAR_PAGE)
                for url in self.download_url_list:
                    print url
                    self.download(url)

    def set_twitter_api(self):
        try:
            auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
            auth.set_access_token(ACCESS_TOKEN_KEY, ACCESS_TOKEN_SECRET)
            self.api = tweepy.API(auth)
        except Exception as e:
            print "[-] Error: ", e
            self.api = None

    def search(self, term, rpp):
        try:
            if self.max_id:
                search_result = self.api.search(q=term, rpp=rpp, max_id=self.max_id)
            else:
                search_result = self.api.search(q=term, rpp=rpp)
            for result in search_result:
                if result.entities.has_key('media'):
                    for media in result.entities['media']:
                        url = media['media_url_https']
                        if url not in self.media_url_list:
                            self.media_url_list.append(url)
                            self.download_url_list.append(url)
            self.max_id = result.id
        except Exception as e:
            print "[-] Error: ", e

    def download(self, url):
        url_orig = '%s:orig' % url
        filename = url.split('/')[-1]
        savepath = IMAGES_DIR + filename
        try:
            response = urllib2.urlopen(url_orig)
            with open(savepath, "wb") as f:
                f.write(response.read())
        except Exception as e:
            print "[-] Error: ", e

def main():
    try:
        downloader = ChinoImageDownloader()
        downloader.run()
    except KeyboardInterrupt:
        pass

if __name__ == '__main__':
    main()

References

-TV Anime "Is the Order a Rabbit ??" Official Website -Summary of how to use Twitter API

Recommended Posts

Search and save images of Chino Kafu from Twitter
Get images by keyword search from Twitter
Save dog images from Google image search
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
[Go language] Collect and save Vtuber images using Twitter API
Crawling with Python and Twitter API 2-Implementation of user search function
DB read of Sqlite3 from Excel (without ODBC) and suggestion search
Extract images from cifar and CUCUMBER-9 datasets
Automatically generate images of koalas and bears
Get images from specific users on Twitter
Search for Pokemon haunting information from Twitter
[Python] Download original images from Google Image Search
Low-rank approximation of images by HOSVD and HOOI
Noise removal and background transparency of binary images
Save the search results on Twitter to CSV.
Perform a Twitter search from Python and try to generate sentences with Markov chains.
Extract images and tables from pdf with python to reduce the burden of reporting