The motive is almost the same as the previous article (Use Pythonista3 to read Twitter TL aloud to iphone). This time, only images and videos are extracted from Twitter search results and automatically saved on the iPhone.
iPhone8 13.2.3 Pythonista3 3.2 Tweepy 3.8.0
・ Installation of Pythonista3 ・ Twitter API registration application ・ Installation of StaSh ・ Installation of tweepy, requests, youtube-dl
sav_photo_video.py
#!Pythonista3
#A program that puts images that match the search from twitter into the album of iphone
import os, requests, photos, youtube_dl
import twitterconfig
from twitterconfig import api
#A function that returns a list of image and video urls from the words you search for
def twitter_to_url_out(q, max_id):
gazou_suu = 0
douga_suu = 0
kensakukekka = api.search(q = q, count = 20, max_id = max_id)
if max_id != None:
kensakukekka = kensakukekka[1:]
gazou_url_list = []
douga_url_list = []
max_id_list = []
for match_tweet in kensakukekka:
max_id_list.append(match_tweet.id)
try:
video_url = match_tweet.extended_entities['media'][0]['video_info']['variants'][1]['url']
douga_url_list.append(video_url)
except:
try:
for x in match_tweet.extended_entities['media']:
gazou_url = x['media_url']
gazou_url_list.append(gazou_url)
except:
continue
max_id = min(max_id_list)
gazou_url_list = sorted(set(gazou_url_list), key=gazou_url_list.index)
douga_url_list = sorted(set(douga_url_list), key=douga_url_list.index)
return max_id, gazou_url_list, douga_url_list
#url_A function that takes a list as an argument and saves each video
def douga_hozon(urllist):
douga_suu = 0
for url in urllist:
try:
with youtube_dl.YoutubeDL() as ydl:
ydl.download([url])
douga_suu += 1
except:
continue
return douga_suu
#url_A function that takes a list as an argument and saves each image
def gazou_hozon(urllist):
gazou_list = []
for url in urllist:
response = requests.get(url)
try:
response.raise_for_status()
except:
continue
filename = str(url.split('/')[-1])
newfile = open(filename,'wb')
for chunk in response.iter_content(10000):
newfile.write(chunk)
newfile.close()
gazou_list.append(filename)
image_asset_list = []
for gazou in gazou_list:
image_asset = photos.create_image_asset(gazou)
image_asset_list.append(image_asset)
try:
os.remove(gazou)
except:
continue
album_list = photos.get_albums()
album_name_dict = {}
number = 0
for album in album_list:
album_name = str(album).split('"')[1]
album_name_dict[album_name] = number
number += 1
if kensakukotoba in album_name_dict.keys():
new_album = album_list[int(album_name_dict[kensakukotoba])]
else:
new_album = photos.create_album(kensakukotoba)
new_album.add_assets(image_asset_list)
gazou_suu = len(image_asset_list)
return gazou_suu
kensakukotoba = input('The word you want to search for\n')
hosiikazu = int(input('How many images do you want\n'))
max_id = None
s_gazou_url_list = []
s_douga_url_list = []
syu_kai = 1
while len(s_gazou_url_list) < hosiikazu:
print(str(syu_kai) + 'Week')
max_id, gazou_url_list, douga_url_list = twitter_to_url_out(q = kensakukotoba, max_id = max_id)
syu_kai += 1
for gazou_url in gazou_url_list:
s_gazou_url_list.append(gazou_url)
for douga_url in douga_url_list:
s_douga_url_list.append(douga_url)
s_gazou_url_list = sorted(set(s_gazou_url_list), key=s_gazou_url_list.index)
s_douga_url_list = sorted(set(s_douga_url_list), key=s_douga_url_list.index)
if syu_kai == 30:
break
douga_suu = douga_hozon(s_douga_url_list)
gazou_suu = gazou_hozon(s_gazou_url_list)
print('End of saving(Number of images:' + str(gazou_suu) + ' ,Number of videos:' + str(douga_suu) + ')')
Initial state. There are no photos or albums.
Open the program and specify the search keyword and the minimum number of sheets you want to save
Wait a moment···
When the save is finished, it is finished.
If you look at it again, an album will be created automatically, and the images saved this time will be stored.
The video is in the directory containing the code in pythonista.
I didn't know how to put a video in an album like a photo. (Is pythonista photos not compatible with videos?) If anyone knows, please let me know. Please change the limit of the number of searches and the search method so that you can use it easily.
What I was interested in after using it several times ○ Better way to save videos For the time being, I thought I should be able to divide the folders when I have time.
○ Automatic deletion of duplicate images If you search with the same keyword several times, the same image will be saved here and there. I try not to make an album with the same name, but I thought it would be nice if there was a part to compare the search results with the existing album.
What I want to do has taken shape! However, there seems to be more room for improvement to make it easier to use ...
Recommended Posts