import requests
import tempfile


def download_file(url):
  """
  Download and return temporary file
  """
  print("url detected, downloading...")

  response = requests.get(url)

  # detect file type from MIME-TYPE of request
  content_type = response.headers['content-type']
  if content_type == 'application/pdf':
    file_type = ".pdf"
  elif content_type == "application/vnd.ms-powerpoint":
    file_type = ".ppt"
  elif content_type == "application/vnd.openxmlformats-officedocument.presentationml.presentation":
    file_type = ".pptx"
  else:
    print("couldn't figure out type of downloaded file. aborting")
    raise

  print("downloaded {0}".format(file_type))

  # write to temporary file
  temp = tempfile.NamedTemporaryFile(suffix=file_type)
  temp.write(response.content)

  return temp