old_contents: """!youtube <search term> return the first youtube search result for <search term>"""
  
  import re
  from urllib import quote
  
  import requests
  
  def youtube(searchterm):
      searchterm = quote(searchterm)
      url = "https://gdata.youtube.com/feeds/api/videos?q={0}&orderBy=relevance&alt=json"
      url = url.format(searchterm)
  
      j = requests.get(url).json()
  
      results = j["feed"]
      if "entry" not in results:
          return "sorry, no videos found"
  
      video = results["entry"][0]["link"][0]["href"]
      video = re.sub("&feature=youtube_gdata", "", video)
  
      return video
  
  def on_message(msg, server):
      text = msg.get("text", "")
      match = re.findall(r"!youtube (.*)", text)
      if not match:
          return
  
      searchterm = match[0]
      return youtube(searchterm.encode("utf8"))
  
new_contents: """!youtube <search term> return the first youtube search result for <search term>"""
  
  import re
  from urllib import quote
  
  import requests
  
  def youtube(searchterm):
      url = "https://www.youtube.com/results?search_query={0}"
      url = url.format(quote(searchterm))
  
      r = requests.get(url)
      results = re.findall('a href="(/watch[^&]*?)"', r.text)
  
      if not results:
          return "sorry, no videos found"
  
      return "https://www.youtube.com{0}".format(results[0])
  
  def on_message(msg, server):
      text = msg.get("text", "")
      match = re.findall(r"!youtube (.*)", text)
      if not match:
          return
  
      searchterm = match[0]
      return youtube(searchterm.encode("utf8"))
  
subject: fix youtube search plugin