Advertisement
will9512

video and photo to immich by face

Mar 12th, 2024
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.81 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Tue Mar 12 12:45:51 2024
  4.  
  5. @author: HP
  6. """
  7.  
  8. import requests
  9. import json
  10.  
  11. def login_to_api(server_url, email, password):
  12.     login_url = f"{server_url}/api/auth/login"
  13.     login_payload = json.dumps({
  14.         "email": email,
  15.         "password": password
  16.     })
  17.     login_headers = {'Content-Type': 'application/json', 'Accept': 'application/json'}
  18.     response = requests.request("POST", login_url, headers=login_headers, data=login_payload)
  19.     print(f"Login response: {response.status_code}, Body: {response.text}")
  20.     return response.json()['accessToken']
  21.  
  22. def get_assets(server_url, token, count, user_id):
  23.     photo_url = f"{server_url}/api/person/{user_id}/assets"
  24.     photo_headers = {'Authorization': f'Bearer {token}', 'Accept': 'application/json'}
  25.     photo_response = requests.get(photo_url, headers=photo_headers)
  26.     assets = photo_response.json()
  27.     return assets
  28.  
  29. def add_assets_to_album(server_url, token, album_id, asset_ids, key=None):
  30.     url = f"{server_url}/api/album/{album_id}/assets"
  31.     headers = {
  32.         'Authorization': f'Bearer {token}',
  33.         'Content-Type': 'application/json',
  34.         'Accept': 'application/json'
  35.     }
  36.     payload = json.dumps({"ids": asset_ids})
  37.     params = {'key': key} if key else {}
  38.     print(f"Sending request to {url} with payload: {payload} and params: {params}")
  39.     response = requests.put(url, headers=headers, data=payload, params=params)
  40.     print(f"Add assets response: {response.status_code}, Body: {response.text}")
  41.     if response.status_code == 200:
  42.         print("Assets successfully added to the album.")
  43.         return True
  44.     else:
  45.         try:
  46.             error_response = response.json()
  47.             print(f"Error adding assets to album: {error_response.get('error', 'Unknown error')}")
  48.         except json.JSONDecodeError:
  49.             print(f"Failed to decode JSON response. Status code: {response.status_code}, Response text: {response.text}")
  50.         return False
  51.  
  52. def main():
  53.     server_url =     (https://localhost:port)
  54.     email =     (immich login)
  55.     password =    (immich pass)
  56.     user_id =    (https://localhost:port/people/**this number***)
  57.     album_id =   (https://localhost:port/albums/**this number***)
  58.  
  59.     token = login_to_api(server_url, email, password)
  60.     assets = get_assets(server_url, token, 1000, user_id)  # Fetching the latest 100 assets
  61.     photo_asset_ids = [asset['id'] for asset in assets if asset['type'] == 'IMAGE']  # Extracting the IDs
  62.     video_asset_ids = [asset['id'] for asset in assets if asset['type'] == 'VIDEO']  # Extracting the IDs
  63.     all_asset_ids = photo_asset_ids + video_asset_ids  # Combine photo and video IDs
  64.     add_assets_to_album(server_url, token, album_id, all_asset_ids)  # Use the
  65.    
  66.    
  67. if __name__ == "__main__":
  68.     main()
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement