Skip to content

Instantly share code, notes, and snippets.

@mitcdh
Created August 13, 2024 15:43
Show Gist options
  • Save mitcdh/8b43ff3c9cea5bc01209ba963cb8ccfb to your computer and use it in GitHub Desktop.
Save mitcdh/8b43ff3c9cea5bc01209ba963cb8ccfb to your computer and use it in GitHub Desktop.
Filter flickr photos not in a particular album
import flickrapi
import time
import os
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# Get environment variables
api_key = os.environ.get("FLICKR_API_KEY")
api_secret = os.environ.get("FLICKR_API_SECRET")
def flickr_authentication():
try:
flickr_api = flickrapi.FlickrAPI(api_key, api_secret, format='parsed-json')
flickr_api.token_cache.forget()
print("Performing OAuth authentication...")
flickr_api.get_request_token(oauth_callback="oob")
authorize_url = flickr_api.auth_url(perms="write")
print(f"Please visit this URL to authorize the application: {authorize_url}")
verifier = input("Enter the verifier code: ")
flickr_api.get_access_token(verifier)
return flickr_api
except flickrapi.FlickrError as e:
print("Unauthorized error occurred. Retrying authentication...")
flickr_api.token_cache.forget()
return flickr_authentication()
except Exception as e:
print(f"An unexpected error occurred: {e}")
raise
# Authenticate with Flickr API
flickr = flickr_authentication()
# Get your user ID
user_id = os.environ.get("FLICKR_USER_ID")
# Get all your photos (including private ones)
all_photos = []
page = 1
per_page = 500
while True:
photos = flickr.people.getPublicPhotos(user_id=user_id, extras='tags', page=page, per_page=per_page)['photos']['photo']
all_photos.extend(photos)
if len(photos) < per_page:
break
page += 1
print(f"Total photos: {len(all_photos)}")
# Get photos in the specific photoset to exclude
exclude_photoset_id = os.environ.get("EXCLUDE_PHOTOSET_ID")
exclude_photoset = []
page = 1
per_page = 500
while True:
photos = flickr.photosets.getPhotos(photoset_id=exclude_photoset_id, extras='tags', page=page, per_page=per_page)['photoset']['photo']
exclude_photoset.extend(photos)
if len(photos) < per_page:
break
page += 1
# exclude_photoset = flickr.photosets.getPhotos(photoset_id=exclude_photoset_id)['photoset']['photo']
print(f"Exclude photoset: {len(exclude_photoset)}")
exclude_photo_ids = [photo['id'] for photo in exclude_photoset]
# print(f"Exclude photo IDs: {exclude_photo_ids}")
# Filter out photos that are in the exclude photoset
filtered_photos = []
for photo in all_photos:
if photo['id'] not in exclude_photo_ids:
filtered_photos.append(photo)
# Get the photoset ID to add photos to (or create a new photoset)
# target_photoset_id = os.environ.get("FLICKR_TARGET_PHOTOSET_ID")
# Or to create a new photoset:
target_photoset = flickr.photosets.create(title=os.environ.get("TARGET_PHOTOSET_NAME"), primary_photo_id=filtered_photos[0]['id'])
target_photoset_id = target_photoset['photoset']['id']
# # Add the filtered photos to the target photoset
for photo in filtered_photos:
try:
flickr.photosets.addPhoto(photoset_id=target_photoset_id, photo_id=photo['id'])
print(f"Added photo {photo['id']} to photoset {target_photoset_id}")
time.sleep(0.5) # Pause to avoid hitting API rate limits
except flickrapi.FlickrError:
print(f"Error adding photo {photo['id']} to photoset {target_photoset_id}")
print("Done adding photos to photoset!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment