def _add_photo(self, id, dry_run=False): """Add the given photo to the working copy.""" # Gather necessary info. info = self.api.photos_getInfo(photo_id=id)[0] # <photo> elem datedir = info.find("dates").get("taken")[:7] dir = join(self.base_dir, datedir) url, filename = self._download_info_from_info(info, size=self.size) path = join(dir, filename) title = info.findtext("title") log.info("A %s [%s]", path, utils.one_line_summary_from_text(title, 40)) last_update = _photo_last_update_from_info(info) if not dry_run: # Create the dirs, as necessary. pics_dir = join(dir, ".pics") if not exists(dir): self.fs.mkdir(dir) if not exists(pics_dir): self.fs.mkdir(pics_dir, hidden=True) # Get the photo itself. #TODO: add a reporthook for progressbar (unless too quick to bother) #TODO: handle ContentTooShortError (py2.5) fname, headers = urllib.urlretrieve(url, path) mtime = utils.timestamp_from_datetime(last_update) os.utime(path, (mtime, mtime)) # Gather and save all metadata. if _photo_num_comments_from_info(info): comments = self.api.photos_comments_getList(photo_id=id)[0] else: comments = None self._save_photo_data(dir, id, info, comments) return datedir, last_update
def _update_photo(self, id, local_datedir, local_info, dry_run=False): """Update the given photo in the working copy.""" info = self._fetch_info_from_photo_id(id) datedir = info.find("dates").get("taken")[:7] last_update = _photo_last_update_from_info(info) # Figure out what work needs to be done. # From *experimentation* it looks like the "secret" attribute # changes if the photo itself changes (i.e. is replaced or # "Edited" or rotated). todos = [] if datedir != local_datedir: log.debug("update %s: datedir change: %r -> %r", id, local_datedir, datedir) todos.append("remove-old") todos.append("photo") elif info.get("secret") != local_info.get("secret"): log.debug("update %s: photo secret change: %r -> %r", id, local_info.get("secret"), info.get("secret")) todos.append("photo") todos.append("info") if _photo_num_comments_from_info(info): todos.append("comments") if not todos: return datedir, last_update # Do the necessary updates. XXX # Figure out ext for videos. _download_info_from_info is wrong # here b/c is isn't using *local* info. url, filename = self._download_info_from_info(info, size=self.size) #XXX #size_ext = (self.size != "original" and "."+self.size or "") #ext = (self.size != "original" and ".jpg" # or "."+local_info.get("originalformat")) # - Remove the old bits, if the datedir has changed. if "remove-old" in todos: d = join(self.base_dir, local_datedir) path = join(d, filename) log.info("D %s [%s]", path, utils.one_line_summary_from_text(local_info.findtext("title"), 40)) if not dry_run: log.debug("rm `%s'", path) os.remove(path) self._remove_photo_data(d, id) remaining_paths = set(os.listdir(d)) remaining_paths.difference_update(set([".pics"])) if not remaining_paths: log.info("D %s", d) self.fs.rm(d) # - Add the new stuff. d = join(self.base_dir, datedir) action_str = ("photo" in todos and "U " or " u") path = join(d, filename) log.info("%s %s [%s]", action_str, path, utils.one_line_summary_from_text(info.findtext("title"), 40)) if not dry_run: if not exists(d): self.fs.mkdir(d) pics_dir = join(d, ".pics") if not exists(pics_dir): self.fs.mkdir(pics_dir, hidden=True) if "photo" in todos: fname, headers = urllib.urlretrieve(url, path) mtime = utils.timestamp_from_datetime(last_update) os.utime(path, (mtime, mtime)) if "comments" in todos: comments = self.api.photos_comments_getList(photo_id=id)[0] else: comments = None self._save_photo_data(d, id, info, comments=comments) #print "... %s" % id #print "originalsecret: %s <- %s" % (info.get("originalsecret"), local_info.get("originalsecret")) #print "secret: %s <- %s" % (info.get("secret"), local_info.get("secret")) #print "rotation: %s <- %s" % (info.get("rotation"), local_info.get("rotation")) return datedir, last_update