def backup_media_thread(self, dl_propics, dl_photos, dl_docs, docs_max_size=None, before_date=None, after_date=None, progress_callback=None): """Backups the specified media contained in the given database file""" self.backup_running = True # Create a connection to the database db = TLDatabase(self.backup_dir) # Store how many bytes we have/how many bytes there are in total current = 0 total = self.calculate_download_size(dl_propics, dl_photos, dl_docs, docs_max_size, after_date, before_date) # Keep track from when we started to determine the estimated time left start = datetime.now() if dl_propics: # TODO Also query chats and channels for user in db.query_users('where photo not null'): if not self.backup_running: return # Try downloading the photo output = self.media_handler.get_propic_path(user) try: if not self.valid_file_exists(output): self.client.download_profile_photo( user.photo, add_extension=False, file_path=output) sleep(self.download_delay) except RPCError as e: print('Error downloading profile photo:', e) finally: current += AVERAGE_PROPIC_SIZE if progress_callback: progress_callback(current, total, self.calculate_etl(current, total, start)) if dl_photos: for msg in db.query_messages(self.get_query(MessageMediaPhoto, before_date, after_date)): if not self.backup_running: return # Try downloading the photo output = self.media_handler.get_msg_media_path(msg) try: if not self.valid_file_exists(output): self.client.download_msg_media( msg.media, add_extension=False, file_path=output) sleep(self.download_delay) except RPCError as e: print('Error downloading photo:', e) finally: current += msg.media.photo.sizes[-1].size if progress_callback: progress_callback(current, total, self.calculate_etl(current, total, start)) # TODO Add an internal callback to determine how the current document download is going, # and update our currently saved bytes count based on that if dl_docs: for msg in db.query_messages(self.get_query(MessageMediaDocument, before_date, after_date)): if not self.backup_running: return if not docs_max_size or msg.media.document.size <= docs_max_size: # Try downloading the document output = self.media_handler.get_msg_media_path(msg) try: if not self.valid_file_exists(output): self.client.download_msg_media( msg.media, add_extension=False, file_path=output) sleep(self.download_delay) except RPCError as e: print('Error downloading document:', e) finally: current += msg.media.document.size if progress_callback: progress_callback(current, total, self.calculate_etl(current, total, start)) db.close()
def backup_media_thread(self, dl_propics, dl_photos, dl_docs, docs_max_size=None, before_date=None, after_date=None, progress_callback=None): """Backups the specified media contained in the given database file""" self.backup_running = True # Create a connection to the database db = TLDatabase(self.backup_dir) # Store how many bytes we have/how many bytes there are in total current = 0 total = self.calculate_download_size(dl_propics, dl_photos, dl_docs, docs_max_size, after_date, before_date) # Keep track from when we started to determine the estimated time left start = datetime.now() if dl_propics: # TODO Also query chats and channels for user in db.query_users('where photo not null'): if not self.backup_running: return # Try downloading the photo output = self.media_handler.get_propic_path(user) try: if not self.valid_file_exists(output): self.client.download_profile_photo(user.photo, add_extension=False, file_path=output) sleep(self.download_delay) except RPCError as e: print('Error downloading profile photo:', e) finally: current += AVERAGE_PROPIC_SIZE if progress_callback: progress_callback( current, total, self.calculate_etl(current, total, start)) if dl_photos: for msg in db.query_messages( self.get_query(MessageMediaPhoto, before_date, after_date)): if not self.backup_running: return # Try downloading the photo output = self.media_handler.get_msg_media_path(msg) try: if not self.valid_file_exists(output): self.client.download_msg_media(msg.media, add_extension=False, file_path=output) sleep(self.download_delay) except RPCError as e: print('Error downloading photo:', e) finally: current += msg.media.photo.sizes[-1].size if progress_callback: progress_callback( current, total, self.calculate_etl(current, total, start)) # TODO Add an internal callback to determine how the current document download is going, # and update our currently saved bytes count based on that if dl_docs: for msg in db.query_messages( self.get_query(MessageMediaDocument, before_date, after_date)): if not self.backup_running: return if not docs_max_size or msg.media.document.size <= docs_max_size: # Try downloading the document output = self.media_handler.get_msg_media_path(msg) try: if not self.valid_file_exists(output): self.client.download_msg_media(msg.media, add_extension=False, file_path=output) sleep(self.download_delay) except RPCError as e: print('Error downloading document:', e) finally: current += msg.media.document.size if progress_callback: progress_callback( current, total, self.calculate_etl(current, total, start)) db.close()