import requests
import os
import sys
import zipfile
import json
from pathlib import Path
API_URL = "https://photo-api-v3.3412334014.workers.dev/upload" # 替换为你的 Worker 地址
def upload_file(file_path, lat=None, lng=None):
with open(file_path, 'rb') as f:
files = {'file': (Path(file_path).name, f)}
data = {}
if lat is not None and lng is not None:
data = {'lat': lat, 'lng': lng}
try:
r = requests.post(API_URL, files=files, data=data)
print(f"✅ 上传成功: {file_path} -> {r.json()}")
except Exception as e:
print(f"❌ 上传失败: {file_path} - {e}")
def upload_folder(folder_path):
lat = lng = None
json_file = None
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.lower().endswith('.json'):
json_file = os.path.join(root, file)
break
if json_file: break
if json_file:
with open(json_file, 'r') as f:
try:
data = json.load(f)
lat = data.get('lat') or data.get('latitude')
lng = data.get('lng') or data.get('longitude')
except: pass
for root, dirs, files in os.walk(folder_path):
for file in files:
ext = Path(file).suffix.lower()
if ext in ('.jpg','.jpeg','.png','.gif','.bmp','.mp4','.mov','.avi','.mkv'):
full_path = os.path.join(root, file)
upload_file(full_path, lat, lng)
if __name__ == "__main__":
if len(sys.argv) < 2:
print("用法: python upload.py <文件或文件夹路径>")
sys.exit(1)
target = sys.argv[1]
if os.path.isfile(target):
upload_file(target)
elif os.path.isdir(target):
upload_folder(target)
else:
print("路径无效")