
import requests
import weechat

URL = "http://api.giphy.com/v1/gifs/random?api_key=dc6zaTOxFJmzC&tag=%s"


def giphy(data, buf, args):
    search = args.replace(" ", "+")
    response = requests.get(URL % search)
    data = response.json()
    image_url = data["data"]["image_url"]
    weechat.command(buf, " /giphy %s -- %s" % (search, image_url))

    return weechat.WEECHAT_RC_OK


def main():
    if not weechat.register("giphy", "Keith Smiley", "1.0.0", "MIT",
                            "Insert a random giphy URL", "", ""):
        return weechat.WEECHAT_RC_ERROR

    weechat.hook_command("giphy", "Insert a random giphy URL", "",
                         "", "", "giphy", "")

if __name__ == "__main__":
    main()


import requests
import weechat

URL = "http://api.giphy.com/v1/gifs/random?api_key=dc6zaTOxFJmzC&tag=%s"


def giphy(data, buf, args):
    search = args.replace(" ", "+")
    response = requests.get(URL % search)
    data = response.json()
    try:
        image_url = data["data"]["image_url"]
    except TypeError:
        image_url = "No matches found"

    weechat.command(buf, " /giphy %s -- %s" % (search, image_url))

    return weechat.WEECHAT_RC_OK


def main():
    if not weechat.register("giphy", "Keith Smiley", "1.0.0", "MIT",
                            "Insert a random giphy URL", "", ""):
        return weechat.WEECHAT_RC_ERROR

    weechat.hook_command("giphy", "Insert a random giphy URL", "",
                         "", "", "giphy", "")

if __name__ == "__main__":
    main()

Write error if no matching gifs are found
Write error if no matching gifs are found

  
  import requests
  import weechat
  
  URL = "http://api.giphy.com/v1/gifs/random?api_key=dc6zaTOxFJmzC&tag=%s"
  
  
  def giphy(data, buf, args):
      search = args.replace(" ", "+")
      response = requests.get(URL % search)
      data = response.json()
+     try:
-     image_url = data["data"]["image_url"]
+         image_url = data["data"]["image_url"]
+     except TypeError:
+         image_url = "No matches found"
+ 
      weechat.command(buf, " /giphy %s -- %s" % (search, image_url))
  
      return weechat.WEECHAT_RC_OK
  
  
  def main():
      if not weechat.register("giphy", "Keith Smiley", "1.0.0", "MIT",
                              "Insert a random giphy URL", "", ""):
          return weechat.WEECHAT_RC_ERROR
  
      weechat.hook_command("giphy", "Insert a random giphy URL", "",
                           "", "", "giphy", "")
  
  if __name__ == "__main__":
      main()
  
Write error if no matching gifs are found
## Code Before:

import requests
import weechat

URL = "http://api.giphy.com/v1/gifs/random?api_key=dc6zaTOxFJmzC&tag=%s"


def giphy(data, buf, args):
    search = args.replace(" ", "+")
    response = requests.get(URL % search)
    data = response.json()
    image_url = data["data"]["image_url"]
    weechat.command(buf, " /giphy %s -- %s" % (search, image_url))

    return weechat.WEECHAT_RC_OK


def main():
    if not weechat.register("giphy", "Keith Smiley", "1.0.0", "MIT",
                            "Insert a random giphy URL", "", ""):
        return weechat.WEECHAT_RC_ERROR

    weechat.hook_command("giphy", "Insert a random giphy URL", "",
                         "", "", "giphy", "")

if __name__ == "__main__":
    main()

## Instruction:
Write error if no matching gifs are found
## Code After:

import requests
import weechat

URL = "http://api.giphy.com/v1/gifs/random?api_key=dc6zaTOxFJmzC&tag=%s"


def giphy(data, buf, args):
    search = args.replace(" ", "+")
    response = requests.get(URL % search)
    data = response.json()
    try:
        image_url = data["data"]["image_url"]
    except TypeError:
        image_url = "No matches found"

    weechat.command(buf, " /giphy %s -- %s" % (search, image_url))

    return weechat.WEECHAT_RC_OK


def main():
    if not weechat.register("giphy", "Keith Smiley", "1.0.0", "MIT",
                            "Insert a random giphy URL", "", ""):
        return weechat.WEECHAT_RC_ERROR

    weechat.hook_command("giphy", "Insert a random giphy URL", "",
                         "", "", "giphy", "")

if __name__ == "__main__":
    main()
