本文整理汇总了Python中smugpy.SmugMug.images_upload方法的典型用法代码示例。如果您正苦于以下问题:Python SmugMug.images_upload方法的具体用法?Python SmugMug.images_upload怎么用?Python SmugMug.images_upload使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类smugpy.SmugMug
的用法示例。
在下文中一共展示了SmugMug.images_upload方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestApiImageUpload
# 需要导入模块: from smugpy import SmugMug [as 别名]
# 或者: from smugpy.SmugMug import images_upload [as 别名]
class TestApiImageUpload(unittest.TestCase):
def setUp(self):
self.smugmug = SmugMug(api_key=API_KEY, api_version='1.2.2', app_name='TestApp')
def test_image_upload_missing_param(self):
self.smugmug.login_withHash(UserID='test', PasswordHash='ABCDE')
if sys.version_info < (2, 7):
self.assertRaises(SmugMugException, lambda: self.smugmug.images_upload())
else:
with self.assertRaises(SmugMugException):
self.smugmug.images_upload()
self.smugmug.reset_auth()
def test_image_upload_without_auth(self):
if sys.version_info < (2, 7):
self.assertRaises(SmugMugException, lambda: self.smugmug.images_upload(File='tests/smuggy.jpg', AlbumID=1234))
else:
with self.assertRaises(SmugMugException):
self.smugmug.images_upload(File='tests/smuggy.jpg', AlbumID=1234)
self.smugmug.reset_auth()
def test_image_upload(self):
self.smugmug.login_withHash(UserID='test', PasswordHash='ABCDE')
rsp = self.smugmug.images_upload(File='tests/smuggy.jpg', AlbumID=1234)
self.assertEqual(rsp['method'], 'smugmug.images.upload')
self.smugmug.reset_auth()
def test_image_upload_with_filename(self):
self.smugmug.login_withHash(UserID='test', PasswordHash='ABCDE')
rsp = self.smugmug.images_upload(File='tests/smuggy.jpg', AlbumID=1234, FileName='rename.jpg')
self.assertEqual(rsp['method'], 'smugmug.images.upload')
self.smugmug.reset_auth()
示例2: TestApiImageUploadOauth
# 需要导入模块: from smugpy import SmugMug [as 别名]
# 或者: from smugpy.SmugMug import images_upload [as 别名]
class TestApiImageUploadOauth(unittest.TestCase):
def setUp(self):
self.smugmug = SmugMug(api_key=API_KEY, api_version='1.3.0', app_name='TestApp', oauth_secret=OAUTH_SECRET)
def test_image_upload_oauth(self):
self.smugmug.set_oauth_token('ABC','123')
rsp = self.smugmug.images_upload(File='tests/smuggy.jpg', AlbumID=1234)
self.assertEqual(rsp['method'], 'smugmug.images.upload')
self.smugmug.reset_auth()
示例3: SmugLine
# 需要导入模块: from smugpy import SmugMug [as 别名]
# 或者: from smugpy.SmugMug import images_upload [as 别名]
class SmugLine(object):
def __init__(self, api_key, email=None, password=None):
self.api_key = api_key
self.email = email
self.password = password
self.smugmug = SmugMug(
api_key=api_key,
api_version="1.2.2",
app_name="SmugLine")
self.login()
self.md5_sums = {}
def get_filter(self, media_type='images'):
if media_type == 'videos':
return VIDEO_FILTER
if media_type == 'images':
return IMG_FILTER
if media_type == 'all':
return ALL_FILTER
def upload_file(self, album, image):
self.smugmug.images_upload(AlbumID=album['id'], **image)
def upload_json(self, source_folder, json_file):
images = json.load(open(json_file))
# prepend folder
for image in images:
image['File'] = source_folder + image['File']
# group by album
groups = []
images.sort(key=lambda x: x['AlbumName'])
for k, g in groupby(images, key=lambda x: x['AlbumName']):
groups.append(list(g))
for group in groups:
album_name = group[0]['AlbumName']
album = self.get_or_create_album(album_name)
self._upload(group, album_name, album)
def upload_folder(self, source_folder, album_name, file_filter=IMG_FILTER):
album = self.get_or_create_album(album_name)
images = self.get_images_from_folder(source_folder, file_filter)
self._upload(images, album_name, album)
def upload_with_folders(self, source_folder, file_filter=IMG_FILTER):
excludes = ['.DS_Store', '.git', 'desktop.ini']
for root, dirnames, filenames in os.walk(source_folder, topdown=True):
# exclude some common "hidden" files and dirs
dirnames[:] = [d for d in dirnames if d not in excludes]
filenames[:] = [f for f in filenames if f not in excludes]
# skip the root
if (root != source_folder):
# if there are NO subfolders and there are images, create SmugMug album
# named root and upload all filenames
if (not dirnames and filenames):
album_description = root[len(source_folder) + 1:]
album_name = album_description
# album_name = album_description[album_description.find('/') + 1:]
album = self.get_or_create_album(album_name, "")
images = self.get_images_from_folder(root, file_filter)
self._upload(images, album_name, album)
return
def _upload(self, images, album_name, album):
images = self._remove_duplicates(images, album)
for image in images:
print('uploading {0} -> {1}'.format(image, album_name))
self.upload_file(album, image)
def _get_remote_images(self, album, extras=None):
remote_images = self.smugmug.images_get(
AlbumID=album['id'],
AlbumKey=album['Key'],
Extras=extras)
return remote_images
def _get_md5_hashes_for_album(self, album):
remote_images = self._get_remote_images(album, 'MD5Sum')
md5_sums = [x['MD5Sum'] for x in remote_images['Album']['Images']]
self.md5_sums[album['id']] = md5_sums
return md5_sums
def _file_md5(self, filename, block_size=2**20):
md5 = hashlib.md5()
f = open(filename, 'rb')
while True:
data = f.read(block_size)
if not data:
break
md5.update(data)
return md5.hexdigest()
def _include_file(self, f, md5_sums):
if self._file_md5(f) in md5_sums:
print('skipping {0} (duplicate)'.format(f))
#.........这里部分代码省略.........
示例4: SmugLine
# 需要导入模块: from smugpy import SmugMug [as 别名]
# 或者: from smugpy.SmugMug import images_upload [as 别名]
class SmugLine(object):
def __init__(self, api_key, email=None, password=None):
self.api_key = api_key
self.email = email
self.password = password
self.smugmug = SmugMug(api_key=api_key, api_version="1.2.2", app_name="SmugLine")
self.login()
self.md5_sums = {}
def get_filter(self, media_type="images"):
if media_type == "videos":
return VIDEO_FILTER
if media_type == "images":
return IMG_FILTER
if media_type == "all":
return ALL_FILTER
def upload_file(self, album, image):
result = "-1"
retries = 0
while (result != "smugmug.images.upload") and (retries < 5):
try:
retries = retries + 1
if result == "-2":
print ("Exception, retrying (attempt {0}).".format(retries))
time.sleep(retries * 3)
rsp = self.smugmug.images_upload(AlbumID=album["id"], **image)
result = rsp["method"]
except Exception as inst:
print inst
result = "-2"
pass
if result == "-2":
print ("ERROR: File upload failed.")
# source: http://stackoverflow.com/a/16696317/305019
def download_file(self, url, folder, filename=None):
local_filename = os.path.join(folder, filename or url.split("/")[-1])
if os.path.exists(local_filename):
print ("{0} already exists...skipping".format(local_filename))
return
r = requests.get(url, stream=True)
with open(local_filename, "wb") as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
f.flush()
return local_filename
def set_file_timestamp(self, filename, image):
# apply the image date
image_info = self.get_image_info(image)
timestamp = time.strptime(image_info["Image"]["Date"], "%Y-%m-%d %H:%M:%S")
t = time.mktime(timestamp)
os.utime(filename, (t, t))
def upload_json(self, source_folder, json_file):
images = json.load(open(json_file))
# prepend folder
for image in images:
image["File"] = source_folder + image["File"]
# group by album
groups = []
images.sort(key=lambda x: x["AlbumName"])
for k, g in groupby(images, key=lambda x: x["AlbumName"]):
groups.append(list(g))
for group in groups:
album_name = group[0]["AlbumName"]
album = self.get_or_create_album(album_name)
self._upload(group, album_name, album)
def upload_folder(self, source_folder, album_name, file_filter=IMG_FILTER):
album = self.get_or_create_album(album_name)
images = self.get_images_from_folder(source_folder, file_filter)
self._upload(images, album_name, album)
def account_folder_number(self, source_folder):
images = self.get_images_from_folder(source_folder, file_filter)
return len(images)
def upload_folder_structure(self, album_title, source_folder, file_filter, uploaded_files, total_files):
album_name = source_folder.replace("./", "").split("/", 1)[1]
subcategory_name = source_folder.replace("./", "").split("/", 1)[0]
categories = self.smugmug.categories_get()
category = None
for candidate_category in categories["Categories"]:
if candidate_category["Name"] == album_title:
category = candidate_category
if category is None:
category = self.smugmug.categories_create(Name=album)["Category"]
subcategories = self.smugmug.subcategories_get(CategoryID=category["id"])
subcategory = None
for candidate_subcategory in subcategories["SubCategories"]:
if candidate_subcategory["Name"] == subcategory_name:
#.........这里部分代码省略.........
示例5: SmugLine
# 需要导入模块: from smugpy import SmugMug [as 别名]
# 或者: from smugpy.SmugMug import images_upload [as 别名]
class SmugLine(object):
def __init__(self, api_key, email=None, password=None):
self.api_key = api_key
self.email = email
self.password = password
self.smugmug = SmugMug(
api_key=api_key,
api_version="1.2.2",
app_name="SmugLine")
self.login()
self.md5_sums = {}
def get_filter(self, media_type='images'):
if media_type == 'videos':
return VIDEO_FILTER
if media_type == 'images':
return IMG_FILTER
if media_type == 'all':
return ALL_FILTER
def upload_file(self, album, image):
self.smugmug.images_upload(AlbumID=album['id'], **image)
# source: http://stackoverflow.com/a/16696317/305019
def download_file(self, url, folder, filename=None):
local_filename = os.path.join(folder, filename or url.split('/')[-1])
if os.path.exists(local_filename):
print('{0} already exists...skipping'.format(local_filename))
return
r = requests.get(url, stream=True)
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
f.flush()
return local_filename
def upload_json(self, source_folder, json_file):
images = json.load(open(json_file))
# prepend folder
for image in images:
image['File'] = source_folder + image['File']
# group by album
groups = []
images.sort(key=lambda x: x['AlbumName'])
for k, g in groupby(images, key=lambda x: x['AlbumName']):
groups.append(list(g))
for group in groups:
album_name = group[0]['AlbumName']
album = self.get_or_create_album(album_name)
self._upload(group, album_name, album)
def upload_folder(self, source_folder, album_name, file_filter=IMG_FILTER):
album = self.get_or_create_album(album_name)
images = self.get_images_from_folder(source_folder, file_filter)
self._upload(images, album_name, album)
def download_album(self, album_name, dest_folder, file_filter=IMG_FILTER):
album = self.get_album_by_name(album_name)
if album is None:
print('Album {0} not found'.format(album_name))
return
images = self._get_images_for_album(album, file_filter)
self._download(images, dest_folder)
def _upload(self, images, album_name, album):
images = self._remove_duplicates(images, album)
for image in images:
print('uploading {0} -> {1}'.format(image, album_name))
self.upload_file(album, image)
def _download(self, images, dest_folder):
for img in images:
print('downloading {0} -> {1}'.format(img['FileName'], dest_folder))
self.download_file(img['OriginalURL'], dest_folder, img['FileName'])
def _get_remote_images(self, album, extras=None):
remote_images = self.smugmug.images_get(
AlbumID=album['id'],
AlbumKey=album['Key'],
Extras=extras)
return remote_images
def _get_md5_hashes_for_album(self, album):
remote_images = self._get_remote_images(album, 'MD5Sum')
md5_sums = [x['MD5Sum'] for x in remote_images['Album']['Images']]
self.md5_sums[album['id']] = md5_sums
return md5_sums
def _get_images_for_album(self, album, file_filter=IMG_FILTER):
extras = 'FileName,OriginalURL'
images = self._get_remote_images(album, extras)['Album']['Images']
for image in [img for img in images \
if file_filter.match(img['FileName'])]:
yield image
#.........这里部分代码省略.........
示例6: SmugLine
# 需要导入模块: from smugpy import SmugMug [as 别名]
# 或者: from smugpy.SmugMug import images_upload [as 别名]
class SmugLine(object):
def __init__(self, api_key, email=None, password=None):
self.api_key = api_key
self.email = email
self.password = password
self.smugmug = SmugMug(
api_key=api_key,
api_version="1.2.2",
app_name="SmugLine")
self.login()
self.md5_sums = {}
def get_filter(self, media_type='images'):
if media_type == 'videos':
return VIDEO_FILTER
if media_type == 'images':
return IMG_FILTER
if media_type == 'all':
return ALL_FILTER
def upload_file(self, source_file, album):
album_id = album['id']
self.smugmug.images_upload(File=source_file, AlbumID=album_id)
def upload(self, source_folder, album_name, file_filter=IMG_FILTER):
album = self.get_or_create_album(album_name)
images = self.get_images_from_folder(source_folder, file_filter)
images = self._remove_duplicates(images, album)
for image in images:
print('uploading {0} -> {1}'.format(image, album_name))
self.upload_file(image, album)
def _get_remote_images(self, album, extras=None):
remote_images = self.smugmug.images_get(
AlbumID=album['id'],
AlbumKey=album['Key'],
Extras=extras)
return remote_images
def _get_md5_hashes_for_album(self, album):
remote_images = self._get_remote_images(album, 'MD5Sum')
md5_sums = [x['MD5Sum'] for x in remote_images['Album']['Images']]
self.md5_sums[album['id']] = md5_sums
return md5_sums
def _file_md5(self, filename, block_size=2**20):
md5 = hashlib.md5()
f = open(filename)
while True:
data = f.read(block_size)
if not data:
break
md5.update(data)
return md5.hexdigest()
def _include_file(self, f, md5_sums):
if self._file_md5(f) in md5_sums:
print('skipping {0} (duplicate)'.format(f))
return False
return True
def _remove_duplicates(self, images, album):
md5_sums = self._get_md5_hashes_for_album(album)
return [x for x in images if self._include_file(x, md5_sums)]
def get_albums(self):
albums = self.smugmug.albums_get(NickName=self.nickname)
return albums
def list_albums(self):
print('available albums:')
for album in self.get_albums()['Albums']:
if album['Title']:
print(album['Title'])
def get_or_create_album(self, album_name):
album = self.get_album_by_name(album_name)
if album:
return album
return self.create_album(album_name)
def get_album_by_name(self, album_name):
albums = self.get_albums()
try:
matches = [x for x in albums['Albums'] \
if x.get('Title') == album_name]
return matches[0]
except:
return None
def _format_album_name(self, album_name):
return album_name[0].upper() + album_name[1:]
def get_album_info(self, album):
return self.smugmug.albums_getInfo(AlbumID=album['id'], AlbumKey=album['Key'])
def create_album(self, album_name, privacy='unlisted'):
public = (privacy == 'public')
album_name = self._format_album_name(album_name)
album = self.smugmug.albums_create(Title=album_name, Public=public)
#.........这里部分代码省略.........