Skip to content

Instantly share code, notes, and snippets.

@musantro
Created December 5, 2022 19:40
Show Gist options
  • Save musantro/31e0d88e4e0b67263a33fa12c6692d70 to your computer and use it in GitHub Desktop.
Save musantro/31e0d88e4e0b67263a33fa12c6692d70 to your computer and use it in GitHub Desktop.
import requests
import json
import os
import time
# Constants for the Mega API endpoints and parameters
API_BASE_URL = "https://g.api.mega.co.nz/cs"
LS_ENDPOINT = "/fs/ls"
MKDIR_ENDPOINT = "/fs/mkdir"
MV_ENDPOINT = "/fs/mv"
GET_ENDPOINT = "/fs/get"
# Constants for the Mega API headers and parameters
API_KEY_HEADER = "X-API-Key"
SESSION_ID_HEADER = "X-Session-ID"
# Constants for the Mega API authentication
USERNAME = "your_username"
PASSWORD = "your_password"
# Constants for the photo classification
PHOTO_MIME_TYPES = [
"image/jpeg",
"image/png",
"image/gif",
"image/tiff",
"image/bmp",
"image/webp",
"image/vnd.adobe.photoshop",
]
# Set the API key and session ID to use for the Mega API requests
API_KEY = os.environ["MEGA_API_KEY"]
SESSION_ID = os.environ["MEGA_SESSION_ID"]
# Set the headers to use for the Mega API requests
headers = {
API_KEY_HEADER: API_KEY,
SESSION_ID_HEADER: SESSION_ID,
}
# Set the parameters for the Mega API requests
params = {
"show_deleted": False,
"show_versions": False,
"show_nodes": False,
"show_path": False,
}
# Set the folder ID of the top-level folder to use for the photo classification
folder_id = os.environ["MEGA_FOLDER_ID"]
def classify_photos(folder_id):
# Send a request to the Mega API to list the contents of the folder
url = f"{API_BASE_URL}{LS_ENDPOINT}"
response = requests.get(url, headers=headers, params={"n": folder_id})
contents = json.loads(response.content)["f"]
# Iterate over the files and folders in the folder
for item in contents:
# Check if the item is a file
if item["t"] == 0:
# Send a request to the Mega API to download the file
url = f"{API_BASE_URL}{GET_ENDPOINT}"
response = requests.get(url, headers=headers, params={"n": item["h"]})
data = json.loads(response.content)
# Check if the MIME type of the file indicates that it is a photo
if data["at"]["mime"] in PHOTO_MIME_TYPES:
# Set the name and timestamp of the photo
name = data["at"]["n"]
timestamp = int(data["at"]["ts"])
# Set the year and month of the photo
year = time.strftime("%Y", time.gmtime(timestamp))
month = time.strftime("%m", time.gmtime(timestamp))
# Set the name of the folder for the photo
folder_name = f"{year}-{month}"
# Send a request to the Mega API to create the folder for the photo
url = f"{API_BASE_URL}{MKDIR_ENDPOINT}"
response = requests.post(
url, headers=headers, json={"n": folder_name, "p": folder_id}
)
folder_id = json.loads(response.content)["h"]
# Send a request to the Mega API to move the photo to the folder
url = f"{API_BASE_URL}{MV_ENDPOINT}"
response = requests.post(
url, headers=headers, json={"n": item["h"], "t": folder_id}
)
# Repeat these steps for each file and folder in the contents of the folder
classify_photos(folder_id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment