code: def _get_proper_filename(file, kind, extension,
                               date=None, possible_names=None):
          
          if isinstance(file, pathlib.Path):
              file = str(file.absolute())
  
          if file is not None and not isinstance(file, str):
              # Probably a stream-like object, we cannot set a filename here
              return file
  
          if file is None:
              file = ''
          elif os.path.isfile(file):
              # Make no modifications to valid existing paths
              return file
  
          if os.path.isdir(file) or not file:
              try:
                  name = None if possible_names is None else next(
                      x for x in possible_names if x
                  )
              except StopIteration:
                  name = None
  
              if not name:
                  if not date:
                      date = datetime.datetime.now()
                  name = '{}_{}-{:02}-{:02}_{:02}-{:02}-{:02}'.format(
                      kind,
                      date.year, date.month, date.day,
                      date.hour, date.minute, date.second,
                  )
              file = os.path.join(file, name)
  
          directory, name = os.path.split(file)
          name, ext = os.path.splitext(name)
          if not ext:
              ext = extension
  
          result = os.path.join(directory, name + ext)
          if not os.path.isfile(result):
              return result
  
          i = 1
          while True:
              result = os.path.join(directory, '{} ({}){}'.format(name, i, ext))
              if not os.path.isfile(result):
                  return result
              i += 1
docs: Gets a proper filename for 'file', if this is a path.
  
             'kind' should be the kind of the output file (photo, document...)
             'extension' should be the extension to be added to the file if
                         the filename doesn't have any yet
             'date' should be when this file was originally sent, if known
             'possible_names' should be an ordered list of possible names
  
             If no modification is made to the path, any existing file
             will be overwritten.
             If any modification is made to the path, this method will
             ensure that no existing file will be overwritten.