def do_post_multipart(url, fields, files):
    content_type, body = encode_multipart_formdata(fields, files)
    #print body

    # Issue 1
    # http://code.google.com/p/pyamazonclouddrive/issues/detail?id=1
    hs={'content-type': content_type,'content-length': str(len(body))}

    return pyacd.do_post(url,body,hs)
def login(email=None,password=None,session=None):
  """Login either with email and password or session.

  :type email: string
  :param email: email address registered to Amazon

  :type password: string
  :param password: password

  :type session: pyacd.session.Session
  :param session: previous session

  :rtype: :class:`pyacd.session.Session`
  :return: Inclues cookies, username and customer_id.
  """
  if session:
    pyacd.session=Session(session)
  elif email is None or password is None:
    raise TypeError("Invalid args, email:%s, password:%s"%(email,password))
  else:
    pyacd.session=Session()
  
  end_point="https://"+pyacd.amazon_domain+"/clouddrive?sf=1&ref_=cd_home_sign_bnh"
  html=pyacd.do_get(end_point)

  NOT_LOGGED_INS=[r"ue_url='\/gp\/feature\.html",r'<form name="signIn" method="POST"',r'id="sign-in-container"']
  CONTINUE_REQUIRED=r'<form action="\/clouddrive" id="continueForm"'

  if False in [re.search(x,html) is None for x in NOT_LOGGED_INS]:
    if not (email and password):
      raise pyacd.PyAmazonCloudDriveError("Both email and password are required.")
    link = re.search(r'"(\/gp\/drive\/files.*?)"',html)
    if link:
      html=pyacd.do_get("https://"+pyacd.amazon_domain+link.groups()[0])
    form = re.search(r'<form name="signIn" method="POST" .*?<\/form>',re.sub(r"\n|\r","",html)).group()
    action = re.search('action="(.*?)"',form).groups()[0]
    inputs = [re.search(' name="(.*?)".*? value="(.*?)"',x) for x in re.findall('<input.*?>',form)]
    params = dict([x.groups() for x in inputs if x!=None])
    params["create"]=0
    params["email"]=email
    params["password"]=password
    body=urllib.urlencode(params)
    html=pyacd.do_post(action,body)
    if False in [re.search(x,html) is None for x in NOT_LOGGED_INS]:
      raise pyacd.PyAmazonCloudDriveError("Login failed.")

  if re.search(CONTINUE_REQUIRED,html):
    form = re.search(CONTINUE_REQUIRED+r".*?<\/form>",re.sub(r"\n|\r","",html)).group()
    action = re.search('action="(.*?)"',form).groups()[0]
    inputs = [re.search(' name="(.*?)".*? value="(.*?)"',x) for x in re.findall('<input.*?>',form)]
    params = dict([x.groups() for x in inputs if x!=None])
    if action[0]=="/":
      action = "https://"+pyacd.amazon_domain+action
    body=urllib.urlencode(params)
    html=pyacd.do_post(action,body)

  try:
    pyacd.session.customer_id=re.search('customerId: "(.+)"', html).groups()[0]
    pyacd.session.username="******"

    if re.search(r"ADrive\.touValidate = true;",html):
      pyacd.session.agreed_with_terms = True
  except:
    pass

  return pyacd.session
def login(email=None,password=None,session=None):
  """Login either with email and password or session.

  :type email: string
  :param email: email address registered to Amazon

  :type password: string
  :param password: password

  :type session: pyacd.session.Session
  :param session: previous session

  :rtype: :class:`pyacd.session.Session`
  :return: Inclues cookies, username and customer_id.
  """
  if session:
    pyacd.session=Session(session)
  elif email is None or password is None:
    raise TypeError("Invalid args, email:%s, password:%s"%(email,password))
  else:
    pyacd.session=Session()
  
  end_point="https://"+pyacd.amazon_domain+"/clouddrive"
  html=pyacd.do_get(end_point)

  NOT_LOGGED_INS=[r"ue_url='\/gp\/feature\.html",r'<form name="signIn" method="POST"']
  CONTINUE_REQUIRED=r'<form action="\/clouddrive" id="continueForm"'

  if False in [re.search(x,html) is None for x in NOT_LOGGED_INS]:
    if not (email and password):
      raise pyacd.PyAmazonCloudDriveError("Both email and password are required.")
    link = re.search(r'"(\/gp\/drive\/files.*?)"',html)
    if link:
      html=pyacd.do_get("https://"+pyacd.amazon_domain+link.groups()[0])
    form = re.search(r'<form name="signIn" method="POST" .*?<\/form>',re.sub(r"\n|\r","",html)).group()
    action = re.search('action="(.*?)"',form).groups()[0]
    inputs = [re.search(' name="(.*?)".*? value="(.*?)"',x) for x in re.findall('<input.*?>',form)]
    params = dict([x.groups() for x in inputs if x!=None])
    params["create"]=0
    params["email"]=email
    params["password"]=password
    body=urllib.urlencode(params)
    html=pyacd.do_post(action,body)
    if False in [re.search(x,html) is None for x in NOT_LOGGED_INS]:
      raise pyacd.PyAmazonCloudDriveError("Login failed.")

  if re.search(CONTINUE_REQUIRED,html):
    form = re.search(CONTINUE_REQUIRED+r".*?<\/form>",re.sub(r"\n|\r","",html)).group()
    action = re.search('action="(.*?)"',form).groups()[0]
    inputs = [re.search(' name="(.*?)".*? value="(.*?)"',x) for x in re.findall('<input.*?>',form)]
    params = dict([x.groups() for x in inputs if x!=None])
    if action[0]=="/":
      action = "https://"+pyacd.amazon_domain+action
    body=urllib.urlencode(params)
    html=pyacd.do_post(action,body)

  try:
    customer_id=html.split("customerId",1)[1]
    customer_id=customer_id.split(">",1)[0]
    customer_id=re.sub('.*value="','',customer_id)
    customer_id=re.sub('".*','',customer_id)
    pyacd.session.customer_id=customer_id

    username=html.split("customer_greeting",1)[1]
    username=username.split("<",1)[0]
    # ToDo: how to make it globalized
    try:
      # For www.amazon.com
      username=username.split(",")[1][1:]
      username=re.sub(r'\..*','',username)
    except:
      # For www.amazon.co.jp
      username = username.decode('shift-jis')
      username=username.split(u"、")[1].split(u"さん")[0]
    pyacd.session.username=username

    if re.search(r"ADrive\.touValidate = true;",html):
      pyacd.session.agreed_with_terms = True
  except:
    pass

  return pyacd.session