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.
Refer to Summary of how to use Twitter API to register the application and obtain various Twitter API Keys.
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
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)
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()
-TV Anime "Is the Order a Rabbit ??" Official Website -Summary of how to use Twitter API
Recommended Posts