Skip to content

Instantly share code, notes, and snippets.

@ttscoff
Created August 22, 2012 03:50
Show Gist options
  • Save ttscoff/3422143 to your computer and use it in GitHub Desktop.
Save ttscoff/3422143 to your computer and use it in GitHub Desktop.
Markdown Lucky Link from DuckDuckGo
#!/usr/bin/ruby
# Where have all the search API's gone, long time passing
# Where have all the search API's gone, long time ago
# Where have all the search API's gone, gone to graveyards every one
# When will they ever learn?
#
# Inelegantly uses DuckDuckGo's "\" lucky link search and
# scrapes the url from the redirect in the result
#
# $ luckyduck.rb "search term"
require 'cgi'
query = ARGV[0]
res = %x{curl -LSs http://duckduckgo.com/?q=%5C#{CGI.escape(query)}}
url = res.match(/window.location.replace\('(.*?)'\)/)
print "[#{query}](#{url[1]})" unless url.nil?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment