pysmug.smugsync

Package pysmug :: Module smugsync |
[hide private]
[frames] | no frames]

# Source Code for Module pysmug.smugsync

     1  # Copyright (c) 2008 Brian Zimmer <bzimmer@ziclix.com>
     2  #
     3  # Permission is hereby granted, free of charge, to any person obtaining a copy of
     4  # this software and associated documentation files (the "Software"), to deal in
     5  # the Software without restriction, including without limitation the rights to
     6  # use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
     7  # of the Software, and to permit persons to whom the Software is furnished to do
     8  # so, subject to the following conditions:
     9  #
    10  # The above copyright notice and this permission notice shall be included in all
    11  # copies or substantial portions of the Software.
    12  #
    13  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    19  # SOFTWARE.
    20
    21  import os
    22  import pysmug
    23
    24  #####
    25  #
    26  # 1) scan directory structure computing (year, date) pairs in existence
    27  # 2) query all albums looking for Category=Backup
    28  # 3) find missing, deleted albums
    29  # 4) *optional* for each deleted directory, delete album
    30  # 5) for each additional directory, create album
    31  # 6) query albums again to make sure smugmug has them all
    32  #
    33  ######
    34
    35 -class SmugSync:
    36    """Synchronize a local directory with SmugMug albums.
    37    """
    38 -  def __init__(self, smugmug=None):
    39      self.m = smugmug or pysmug.login()
    40
    41 -  def filesystem(self, directory):
    42      """Scan the file system to produce a ((subcategory, title), filename) generator.
    43
    44      @param directory: the root of the directory tree
    45      """
    46      while directory[-1] == os.sep:
    47        source = source[:-1]
    48
    49      for root, dns, fns in os.walk(directory):
    50        if not fns:
    51          continue
    52        album = tuple([x for x in root.replace(directory, "").split(os.sep) if x])
    53        if not album:
    54          continue
    55        for fn in fns:
    56          yield (album, os.path.join(root, fn))
    57
    58 -  def smugmug(self, category):
    59      """Scan the album tree to produce a ((subcategory, title), filename) generator.
    60
    61      @param category: the root category for backup albums
    62      """
    63      smugmug = dict()
    64      b = self.m.batch()
    65      for a in self.m.albums_get()["Albums"]:
    66        if a["Category"]["Name"] == category:
    67          albumId = a["id"]
    68          smugmug[albumId] = (a["SubCategory"]["Name"], a["Title"])
    69          b.images_get(AlbumID=albumId, Heavy=1)
    70      for params, results in b():
    71        # the API can throw an exception if the album is empty!
    72        albumId = params["AlbumID"]
    73        for image in results.get("Images", []):
    74          yield (smugmug[albumId], image["FileName"])
    75