é¢åèãã®ã§ tumblr api ã使ãã¾ãããã tumblr ã«ç»é²ããæ¹æ³ã¯èª¬æãã¾ããã
import os import sys import time import requests api_url = 'https://api.tumblr.com/v2/tagged' api_key = sys.argv[1] tag = sys.argv[2] num = int(sys.argv[3]) prefix = '' dir_name = '' if (sys.argv[4] != ''): dir_name = sys.argv[4] prefix = dir_name + '_' full_dir = 'tumblr_images' if (dir_name != ''): full_dir = os.path.join(full_dir, dir_name) os.makedirs(full_dir, exist_ok=True) payload = { 'api_key': api_key, 'tag': tag, 'before': '' } photo_urls = [] for i in range(num): r = requests.get(api_url, params=payload) r_json = r.json() for data in r_json['response']: if data['type'] != 'photo': continue for photo in data['photos']: photo_urls.append(photo['original_size']['url']) if(len(r_json['response']) == 0): continue payload['before'] = r_json['response'][len(r_json['response']) - 1]['timestamp'] time.sleep(1) image_idx = 0 for url in photo_urls: image_idx += 1 extension = url[-3:] if extension != 'jpg' and extension != 'png': continue with open(full_dir + '/' + prefix + str(image_idx) + '.jpg', 'wb') as handler: response = requests.get(url) if not response.ok: continue handler.write(response.content) time.sleep(1)
ãã㧠Google Colaboratory ã使ã£ã¦ä¸è¨ã®ã»ã«ãç¨æãã¾ãã
Google Colaboratory
㯠Google Chrome ã§ä½¿ããã®ã«ãªãã¾ãã
ä¸è¨ã¯ Google Drive ããã¦ã³ãããã»ã«ã§ãã
from google.colab import drive drive.mount('/content/drive')
ä¸è¨ã Google Drive ã® deep/tumblr_image_get.py
ã«ããåæã§ä¸è¨ã®ã»ã«ãç¨æã
#!drive/My\ Drive/deep; python tumblr_image_get.py tumlr_API_KEY "ä¸æ¡ããã¿" 100 prefix
- tumlr_API_KEY 㯠tumlr ã§åå¾ãã¦ä¸ãããã
- tumblr_images ã£ã¦è¨ããã£ã¬ã¯ããªã Google Drive ã® deep 以ä¸ã«ç¨æãã¾ãããã
ãããªæãã ã¨æãã¾ãã
ã§ç»åã¯ãããªæãã