Created
July 18, 2013 20:36
-
-
Save BillKeenan/6032846 to your computer and use it in GitHub Desktop.
updated for duckduckgo as of 2013.07.18
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/http' | |
def e_url(string) | |
string.gsub(/([^a-zA-Z0-9_.-]+)/n) do | |
'%' + $1.unpack('H2' * $1.size).join('%').upcase | |
end | |
end | |
def fetch(uri) | |
url = URI.parse(uri) | |
loc = false | |
r = Net::HTTP.get_response(url) | |
if r.code == "301" | |
loc = r.header['location'] | |
r = Net::HTTP.get_response(URI.parse(r.header['location'])) | |
end | |
r.body | |
end | |
ARGF.each do |input| | |
res = fetch("http://duckduckgo.com/?q=%5C#{e_url(input)}%2F") | |
if res | |
match = res.strip.match(/.*url\=(.*?)'/) | |
unless match.nil? | |
print %Q{[#{input.strip}](#{match[1].strip})} | |
else | |
print "no match" | |
print input | |
end | |
else | |
print input | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment