def get_recent_likes(user_id): """Find recently liked media""" # Find recent likes likes = scratch.stalk_likes_in_follows(user_id) likes = likes[0:NUM_UNICODE_CHARS] # Only process up to first 30 items # Convert likes to a saveable format likes = [models.media.dictionary_from_object(u) for u in likes] # Save the liked media user_likes_dir = os.path.join(wf().likesdir, user_id + '.json') with open(user_likes_dir, 'wb') as likes_file: likes_file.write(json.dumps(likes)) # Download the media scratch.download_images(likes, wf().mediadir, resolution='thumbnail')
def recent_media(user_id, username): """Retrieve and display recent media""" # Make a go back item special_unicode_value = prepare_item_with_command(LOAD_USER, user_id, username) wf().add_item( title='Go back', valid=False, autocomplete=unichr(special_unicode_value), icon=ICON_BACK) # Get recent media information media = scratch.user_media(user_id, max_pages=1) needed_media_ids = [m.id for m in media] thumbnail_dir = os.path.join(wf().mediadir, user_id) if not os.path.exists(thumbnail_dir): os.makedirs(thumbnail_dir) # Get list of thumbnails already downloaded dir_contents = os.listdir(thumbnail_dir) have_media_ids = [thumb.split('.')[0] for thumb in dir_contents] # Download thumbnails not already available wanting_media_ids = list( set(needed_media_ids) - set(have_media_ids) ) wanting_media = [m for m in media if m.id in wanting_media_ids] scratch.download_images(wanting_media, thumbnail_dir, resolution='thumbnail') display_media(media, media_dir=thumbnail_dir)
def display_media(media, media_dir, download_thumbnails=False): """Display a thumbnail, username, and caption for each media as an Alfred item""" # Download thumbnail images of liked media if download_thumbnails: scratch.download_images(media, wf().mediadir, resolution='thumbnail') # Create an alfred item for each like for m in media: media_url = m.images['thumbnail'].url filetype = media_url.split('.')[-1] # Check if image exists thumbnail_path = os.path.join( wf().mediadir, '{id}.{type}'.format(id=m.id, type=filetype) ) if not os.path.exists(thumbnail_path): # Download the media scratch.download_images([m], wf().mediadir, resolution='thumbnail') try: # Caption might be null caption = m.caption.text.replace('\n', '\t') except: caption = '' wf().add_item( title=u'User: {user}'.format(user=m.user.username), subtitle=u'Caption: {caption}'.format(caption=caption), valid=True, arg="url " + m.link, icon=thumbnail_path)
# Issue notification that a search is started Notifier.notify( title="Instastalk", subtitle="User: {user}".format(user=username), message="Searching {follows} follows for likes".format(follows=count_follows), ) # Find recent likes likes = scratch.stalk_likes_in_follows(user_id) likes_recent = likes[0:MAX_LIKED_MEDIA] # Only process up to first 50 items # Convert likes to a saveable format likes_recent_dict = [models.Media.dictionary_from_object(u) for u in likes] # Save the liked media if not os.path.exists(likes_dir): os.makedirs(likes_dir) user_likes_path = os.path.join(likes_dir, user_id + ".json") with open(user_likes_path, "wb") as likes_file: likes_file.write(json.dumps(likes_recent_dict)) # Download the media scratch.download_images(likes, media_dir, resolution="thumbnail") # Issue notification that the search is complete num_likes = len(likes_recent_dict) Notifier.notify( title="Instastalk", subtitle="User: {user}".format(user=username), message="Found {likes} liked media".format(likes=num_likes), sound="default", )