from bs4 import BeautifulSoup
import re


def ftp_profile(publish_settings):
  """Takes PublishSettings, extracts ftp user, password, and host"""
  soup = BeautifulSoup(publish_settings, 'html.parser')

  profiles = soup.find_all('publishprofile')
  ftp_profile = [profile for profile in profiles if profile['publishmethod'] == 'FTP'][0]

  matches = re.search('ftp://(.+)/site/wwwroot', ftp_profile['publishurl'])

  host = matches.group(1) if matches else ''

  username = ftp_profile['username'].replace("\\$", "%5C%24")
  password = ftp_profile['userpwd']
  return host, username, password, ftp_profile['publishurl']