def updatePhoto(users):
cd = buildGAPIObject(API.DIRECTORY)
filenamePattern = getString(Cmd.OB_PHOTO_FILENAME_PATTERN)
checkForExtraneousArguments()
p = re.compile(u'^(ht|f)tps?://.*$')
i, count, users = getEntityArgument(users)
for user in users:
i += 1
user, userName, _ = splitEmailAddressOrUID(user)
filename = _substituteForUser(filenamePattern, user, userName)
if p.match(filename):
try:
status, image_data = httplib2.Http(disable_ssl_certificate_validation=GC.Values[GC.NO_VERIFY_SSL]).request(filename, u'GET')
if status[u'status'] != u'200':
entityActionFailedWarning([Ent.USER, user, Ent.PHOTO, filename], Msg.NOT_ALLOWED, i, count)
continue
if status[u'content-location'] != filename:
entityActionFailedWarning([Ent.USER, user, Ent.PHOTO, filename], Msg.NOT_FOUND, i, count)
continue
except (httplib2.HttpLib2Error, httplib2.ServerNotFoundError, httplib2.CertificateValidationUnsupported) as e:
entityActionFailedWarning([Ent.USER, user, Ent.PHOTO, filename], str(e), i, count)
continue
else:
image_data = readFile(filename, mode=u'rb', continueOnError=True, displayError=True)
if image_data is None:
entityActionFailedWarning([Ent.USER, user, Ent.PHOTO, filename], None, i, count)
continue
body = {u'photoData': base64.urlsafe_b64encode(image_data)}
try:
callGAPI(cd.users().photos(), u'update',
throw_reasons=[GAPI.USER_NOT_FOUND, GAPI.FORBIDDEN, GAPI.INVALID_INPUT],
userKey=user, body=body, fields=u'')
entityActionPerformed([Ent.USER, user, Ent.PHOTO, filename], i, count)
except GAPI.invalidInput as e:
entityActionFailedWarning([Ent.USER, user, Ent.PHOTO, filename], str(e), i, count)
except (GAPI.userNotFound, GAPI.forbidden):
entityUnknownWarning(Ent.USER, user, i, count)
# gam <UserTypeEntity> delete photo
评论列表
文章目录