def download_picasa_media(self): log.info(u'Downloading Picasa Only Files ...') # noinspection PyTypeChecker for media in DatabaseMedia.get_media_by_search( self._root_folder, self._db, media_type=MediaType.PICASA, start_date=self.startDate, end_date=self.endDate, skip_linked=True): if os.path.exists(media.local_full_path): continue log.info(u"Downloading %s ..." % media.local_full_path) tmp_path = os.path.join(media.local_folder, '.gphotos.tmp') if not os.path.isdir(media.local_folder): os.makedirs(media.local_folder) res = Utils.retry(5, urllib.urlretrieve, media.url, tmp_path) if res: os.rename(tmp_path, media.local_full_path) # set the access date to create date since there is nowhere # else to put it on linux (and is useful for debugging) os.utime(media.local_full_path, (Utils.to_timestamp( media.modify_date), Utils.to_timestamp(media.create_date))) else: log.warning(u"WARNING: failed to download %s", media.local_path)
def do_download_file(self, base_url: str, media_item: DatabaseMedia): """ Runs in a process pool and does a download of a single media item. """ if self.case_insensitive_fs: relative_folder = str(media_item.relative_folder).lower() filename = str(media_item.filename).lower() else: relative_folder = media_item.relative_folder filename = media_item.filename local_folder = self._root_folder / relative_folder local_full_path = local_folder / filename if media_item.is_video(): download_url = "{}=dv".format(base_url) timeout = self.video_timeout else: download_url = "{}=d".format(base_url) timeout = self.image_timeout temp_file = tempfile.NamedTemporaryFile(dir=local_folder, delete=False) t_path = Path(temp_file.name) try: response = self._session.get(download_url, stream=True, timeout=timeout) response.raise_for_status() shutil.copyfileobj(response.raw, temp_file) temp_file.close() temp_file = None response.close() t_path.rename(local_full_path) create_date = Utils.safe_timestamp(media_item.create_date) os.utime( str(local_full_path), ( Utils.safe_timestamp(media_item.modify_date).timestamp(), create_date.timestamp(), ), ) if _use_win_32: file_handle = win32file.CreateFile( str(local_full_path), win32file.GENERIC_WRITE, 0, None, win32con.OPEN_EXISTING, 0, None, ) win32file.SetFileTime(file_handle, *(create_date, ) * 3) file_handle.close() os.chmod(str(local_full_path), 0o666 & ~self.current_umask) except KeyboardInterrupt: log.debug("User cancelled download thread") raise finally: if temp_file: temp_file.close() if t_path.exists(): t_path.unlink()
def do_download_file(self, base_url: str, media_item: DatabaseMedia): """ Runs in a process pool and does a download of a single media item. """ local_folder = self._root_folder / media_item.relative_folder local_full_path = local_folder / media_item.filename if media_item.is_video(): download_url = '{}=dv'.format(base_url) timeout = self.video_timeout else: download_url = '{}=d'.format(base_url) timeout = self.image_timeout temp_file = tempfile.NamedTemporaryFile(dir=local_folder, delete=False) t_path = Path(temp_file.name) try: response = self._session.get(download_url, stream=True, timeout=timeout) response.raise_for_status() shutil.copyfileobj(response.raw, temp_file) temp_file.close() response.close() t_path.rename(local_full_path) os.utime(str(local_full_path), (Utils.safe_timestamp(media_item.modify_date), Utils.safe_timestamp(media_item.create_date))) except KeyboardInterrupt: log.debug("User cancelled download thread") raise finally: if t_path.exists(): t_path.unlink()
def download_drive_media(self): log.info(u'Downloading Drive Files ...') # noinspection PyTypeChecker for media in DatabaseMedia.get_media_by_search( self._root_folder, self._db, media_type=MediaType.DRIVE, start_date=self.startDate, end_date=self.endDate): if os.path.exists(media.local_full_path): if Utils.to_timestamp(media.modify_date) > \ os.path.getctime(media.local_full_path): log.warning(u'{} was modified'.format( media.local_full_path)) else: continue if not os.path.isdir(media.local_folder): os.makedirs(media.local_folder) temp_filename = os.path.join(self._root_folder, '.temp-photo') log.info(u'downloading {} ...'.format(media.local_full_path)) f = self._googleDrive.CreateFile({'id': media.id}) try: Utils.retry(10, f.GetContentFile, temp_filename) if os.path.exists(media.local_full_path): os.remove(media.local_full_path) os.rename(temp_filename, media.local_full_path) # set the access date to create date since there is nowhere # else to put it on linux (and is useful for debugging) os.utime(media.local_full_path, (Utils.to_timestamp( media.modify_date), Utils.to_timestamp(media.create_date))) except ApiRequestError: log.error(u'DOWNLOAD FAILURE for {}'.format( media.local_full_path))
def to_media(self) -> DatabaseMedia: db_media = DatabaseMedia( _id=self.RemoteId, _filename=self.AlbumName, _size=self.Size, _create_date=self.EndDate, ) return db_media
def to_media(self) -> DatabaseMedia: pth = Path(self.Path) if self.Path else None db_media = DatabaseMedia( _id=self.RemoteId, _relative_folder=pth, _filename=self.FileName, _orig_name=self.OriginalFileName, _duplicate_number=self.DuplicateNo, _size=self.FileSize, _mime_type=self.MimeType, _description=self.Description, _date=self.ModifyDate, _create_date=self.CreateDate, ) return db_media
def test_database_media(self): d = DatabaseMedia() assert d.url is None assert d.location is None