Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process directories #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 54 additions & 33 deletions upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from google.oauth2.credentials import Credentials
import json
import os.path
import os
import argparse
import logging

Expand Down Expand Up @@ -129,51 +130,71 @@ def create_or_retrieve_album(session, album_title):
logging.error("Could not find or create photo album '\{0}\'. Server Response: {1}".format(album_title, resp))
return None

def upload_photos(session, photo_file_list, album_name):

album_id = create_or_retrieve_album(session, album_name) if album_name else None

# interrupt upload if an upload was requested but could not be created
if album_name and not album_id:
def upload_photo(session, photo_file_name, album_name, album_id):
try:
photo_file = open(photo_file_name, mode='rb')
photo_bytes = photo_file.read()
except OSError as err:
logging.error("Could not read file \'{0}\' -- {1}".format(photo_file_name, err))
return

session.headers["Content-type"] = "application/octet-stream"
session.headers["X-Goog-Upload-Protocol"] = "raw"

for photo_file_name in photo_file_list:
session.headers["X-Goog-Upload-File-Name"] = os.path.basename(photo_file_name)

try:
photo_file = open(photo_file_name, mode='rb')
photo_bytes = photo_file.read()
except OSError as err:
logging.error("Could not read file \'{0}\' -- {1}".format(photo_file_name, err))
continue
logging.info("Uploading photo -- \'{}\'".format(photo_file_name))

session.headers["X-Goog-Upload-File-Name"] = os.path.basename(photo_file_name)
upload_token = session.post('https://photoslibrary.googleapis.com/v1/uploads', photo_bytes)

logging.info("Uploading photo -- \'{}\'".format(photo_file_name))
if (upload_token.status_code == 200) and (upload_token.content):
create_body = json.dumps({
"albumId": album_id,
"newMediaItems": [
{
"description": "",
"simpleMediaItem": {
"uploadToken": upload_token.content.decode()
}
}
]
}, indent=4)

upload_token = session.post('https://photoslibrary.googleapis.com/v1/uploads', photo_bytes)
resp = session.post('https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate', create_body).json()

if (upload_token.status_code == 200) and (upload_token.content):
logging.debug("Server response: {}".format(resp))

create_body = json.dumps({"albumId":album_id, "newMediaItems":[{"description":"","simpleMediaItem":{"uploadToken":upload_token.content.decode()}}]}, indent=4)
if "newMediaItemResults" in resp:
status = resp["newMediaItemResults"][0]["status"]
if status.get("code") and (status.get("code") > 0):
logging.error("Could not add \'{0}\' to library -- {1}".format(os.path.basename(photo_file_name), status["message"]))
else:
logging.info("Added \'{}\' to library and album \'{}\' ".format(os.path.basename(photo_file_name), album_name))
else:
logging.error("Could not add \'{0}\' to library. Server Response -- {1}".format(os.path.basename(photo_file_name), resp))
else:
logging.error("Could not upload \'{0}\'. Server Response - {1}".format(os.path.basename(photo_file_name), upload_token))

def upload_photos(session, photo_file_list, album_name):

resp = session.post('https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate', create_body).json()
album_id = create_or_retrieve_album(session, album_name) if album_name else None

logging.debug("Server response: {}".format(resp))
# interrupt upload if an upload was requested but could not be created
if album_name and not album_id:
return

if "newMediaItemResults" in resp:
status = resp["newMediaItemResults"][0]["status"]
if status.get("code") and (status.get("code") > 0):
logging.error("Could not add \'{0}\' to library -- {1}".format(os.path.basename(photo_file_name), status["message"]))
else:
logging.info("Added \'{}\' to library and album \'{}\' ".format(os.path.basename(photo_file_name), album_name))
else:
logging.error("Could not add \'{0}\' to library. Server Response -- {1}".format(os.path.basename(photo_file_name), resp))
session.headers["Content-type"] = "application/octet-stream"
session.headers["X-Goog-Upload-Protocol"] = "raw"

else:
logging.error("Could not upload \'{0}\'. Server Response - {1}".format(os.path.basename(photo_file_name), upload_token))
for photo_file_name in photo_file_list:
if not os.path.exists(photo_file_name):
logging.error("Could not upload \'{0}\'. file do not exists".format(photo_file_name))
elif os.path.isdir(photo_file_name):
for root, dirs, files in os.walk(photo_file_name):
dirs[:] = [d for d in dirs if not d[0] == '.']
for file in files:
if not file[0] == '.':
file_name = os.path.join(root, file)
upload_photo(session, file_name, album_name, album_id)
else:
upload_photo(session, photo_file_name, album_name, album_id)

try:
del(session.headers["Content-type"])
Expand Down