Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Updating commands because Yahoo shut down their search API. Bastards.
  • Loading branch information
ttscoff committed Jul 22, 2011
1 parent d2d3907 commit 5ac3fe8
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 84 deletions.
32 changes: 23 additions & 9 deletions Commands/Blogger Link.tmCommand
Expand Up @@ -5,36 +5,50 @@
<key>beforeRunningCommand</key>
<string>nop</string>
<key>command</key>
<string>#!/usr/bin/env ruby -rjcode -Ku
<string>#!/usr/bin/ruby
SUPPORT = ENV['TM_SUPPORT_PATH']
DIALOG = ENV['DIALOG']
require SUPPORT + '/lib/ui'
require SUPPORT + '/lib/escape'
require SUPPORT + '/lib/osx/plist'
require SUPPORT + '/lib/exit_codes'
require "#{ENV['TM_BUNDLE_SUPPORT']}/lib/yahoo"
require 'erb'
require 'cgi'
require 'net/http'
require 'rexml/document'
blogger = STDIN.read
input = blogger
blogsite = ENV['BLOG_SITE'] ? ENV['BLOG_SITE'] : "www.tuaw.com"
blogsite = "www." + blogsite unless blogsite =~ /^www\./
query = e_url(input + " site:#{blogsite}/bloggers -inurl:page")
query = input + " site:#{blogsite}/editor"
yahoo = WebSearch.new('TM_YAHOO', query, 'all', 20, 1, nil, 1)
escapedUrl = "http://api.search.live.net/xml.aspx?Appid=6B9E3A4B9F0D8F963A24815A0317BF1DCA3B0E9A&amp;query=#{e_url(query)}&amp;sources=web&amp;web.offset=0"
TextMate.exit_show_tool_tip "Blogger not found, check your spelling" if yahoo.parse_results.length == 0
xml_data = Net::HTTP.get_response(URI.parse(escapedUrl)).body
doc = REXML::Document.new(xml_data)
bings = []
doc.elements.each('SearchResponse/web:Web/web:Results/web:WebResult') do |result|
begin
bings &lt;&lt; {
'title' =&gt; result.elements['web:Title'].text.gsub('"','&amp;raquo;').gsub("'",'&amp;rsquo;'),
'url' =&gt; result.elements['web:Url'].text
}
rescue
TextMate.exit_show_tool_tip "Error in returned data"
end
end
TextMate.exit_show_tool_tip "Blogger not found, check your spelling" if bings.empty?
ysuggest = []
yahoo.parse_results.each {|result|
if result['Url'].gsub(/-/,' ') =~ /#{input}/i &amp;&amp; result['Url'] =~ /^http:\/\/#{blogsite}\/bloggers\/[^\/]+\/?$/
blogger_name = result['Url'].match(/\/bloggers\/([^\/]+)\/?/)[1].chomp.gsub(/-/,' ').gsub(/\b([a-z])/i) {|w| w.capitalize }
bings.each {|result|
if result['url'].gsub(/-/,' ') =~ /#{input}/i &amp;&amp; result['url'] =~ /^http:\/\/#{blogsite}\/editor\/[^\/]+\/?$/
blogger_name = result['url'].match(/\/editor\/([^\/]+)\/?/)[1].chomp.gsub(/-/,' ').gsub(/\b([a-z])/i) {|w| w.capitalize }
ysuggest &lt;&lt; {
'title' =&gt; blogger_name,
'url' =&gt; result['Url']
'url' =&gt; result['url']
}
end
}
Expand Down
2 changes: 1 addition & 1 deletion Commands/Check Grammar:Spelling.tmCommand
Expand Up @@ -27,7 +27,7 @@ def strip_headers(str)
str.gsub!(/&lt;!--\s?more\s?--&gt;/,"") unless str.nil?
str.gsub!(/^&lt;\d+&gt; .*$/,'') unless str.nil?
str.gsub!(/&lt;\d+&gt;/,'') unless str.nil?
str.gsub!(/^(doctype|categories|link|title|tags)\:( [^\n]+)?/i, "") unless str.nil?
str.gsub!(/^(doctype|categories|link|title|tags|appid|excerpt)\:( [^\n]+)?/i, "") unless str.nil?
return str
end
Expand Down
91 changes: 57 additions & 34 deletions Commands/Link Blog Post.tmCommand
Expand Up @@ -4,57 +4,80 @@
<dict>
<key>beforeRunningCommand</key>
<string>nop</string>
<key>bundleUUID</key>
<string>37D5C267-16F1-4386-9E6D-47667DF151F6</string>
<key>command</key>
<string>#!/usr/bin/env ruby -rjcode -Ku
SUPPORT = ENV['TM_SUPPORT_PATH']
DIALOG = ENV['DIALOG']
require SUPPORT + '/lib/ui'
require SUPPORT + '/lib/escape'
require SUPPORT + '/lib/ui'
require SUPPORT + '/lib/osx/plist'
require SUPPORT + '/lib/exit_codes'
require "#{ENV['TM_BUNDLE_SUPPORT']}/lib/yahoo"
require 'erb'
require 'date'
require 'net/http'
require 'rexml/document'
require 'cgi'
string = STDIN.read
offset = 0
blogsite = ENV['BLOG_SITE'] ? ENV['BLOG_SITE'] : "mashable.com"
phrase = TextMate::UI.request_string(:title =&gt; "Enter keywords",:prompt =&gt; "Find posts with keywords:",:default =&gt; string)
TextMate.exit_discard unless phrase
query = phrase + " site:#{blogsite} AND NOT intitle:tag"
input = STDIN.read
blogsite = ENV['BLOG_SITE'] ? ENV['BLOG_SITE'] : "www.tuaw.com"
# input = "Search text" if input.empty?
answer = TextMate::UI.request_string(:title =&gt; "Enter keywords",:prompt =&gt; "Find posts on #{blogsite} with keywords:",:default =&gt; input)
TextMate.exit_discard unless answer
query = e_url(answer + " -inurl:tag -inurl:?q= -inurl:rss.xml -inurl:bloggers site:#{blogsite}")
yahoo = WebSearch.new('TM_YAHOO', query, 'all', 25, 1, nil, 1)
def do_search(offset,phrase)
escapedUrl = "http://api.search.live.net/xml.aspx?Appid=6B9E3A4B9F0D8F963A24815A0317BF1DCA3B0E9A&amp;query=#{e_url(phrase)}&amp;sources=web&amp;web.offset=#{offset}"
ysuggest = []
yahoo.parse_results.each {|result|
if result['Url'] =~ /.*?\/(\d{4})\/(\d{2})\/(\d{2})\/.*/
postdate = "\t\t#{$2}-#{$3}-#{$1}"
sortdate = "#{$1}/#{$2}/#{$3}"
title = result['Title'].gsub('"','&amp;raquo;').gsub(/&lt;\/?b&gt;/,'')
title = title[0..50]+"…" if title.length &gt; 50
((60 - title.length) / 4).to_i.times {|i| title += "\t" }
title += postdate
ysuggest &lt;&lt; {
'title' =&gt; title,
'url' =&gt; result['Url'],
'date' =&gt; sortdate
}
xml_data = Net::HTTP.get_response(URI.parse(escapedUrl)).body
doc = REXML::Document.new(xml_data)
bings = []
doc.elements.each('SearchResponse/web:Web/web:Results/web:WebResult') do |result|
begin
bings &lt;&lt; {
'title' =&gt; result.elements['web:Title'].text.gsub('"','&amp;raquo;').gsub("'",'&amp;rsquo;'),
'url' =&gt; result.elements['web:Url'].text
}
rescue
TextMate.exit_show_tool_tip "Error in returned data"
end
end
}
ysuggest.sort! { |a,b| Date.parse(b['date']) &lt;=&gt; Date.parse(a['date']) }
plist = { 'menuItems' =&gt; ysuggest }.to_plist
bings &lt;&lt; {
'title' =&gt; 'More results…',
'url' =&gt; ''
} unless bings.empty?
res = OSX::PropertyList.load(`#{e_sh DIALOG} -up #{e_sh plist}`)
TextMate.exit_show_tool_tip "No matches" if bings.empty?
plist = { 'menuItems' =&gt; bings }.to_plist
res = OSX::PropertyList.load(`#{e_sh DIALOG} -up #{e_sh plist}`)
unless res.has_key? 'selectedMenuItem'
if ENV['TM_CURRENT_LINE'] =~ /^\[/
TextMate.exit_insert_text "]: "
else
TextMate.exit_discard
end
end
if res['selectedMenuItem']['title'] == "More results…"
offset += 10
do_search(offset,phrase)
else
return res
end
end
TextMate.exit_show_tool_tip "No links found or nothing selected" unless res.has_key? 'selectedMenuItem'
res = do_search(offset,query)
url = res['selectedMenuItem']['url']
title = res['selectedMenuItem']['title']
input = phrase # res['selectedMenuItem']['title']
if input.empty? &amp;&amp; !(ENV['TM_SCOPE'].scan(/markdown/).empty?) &amp;&amp; ENV['TM_CURRENT_LINE'] =~ /^(\s+)?$/
input = "blog" + answer.chomp.gsub(/\s+/,"_")
if !(ENV['TM_SCOPE'].scan(/markdown/).empty?) &amp;&amp; ENV['TM_CURRENT_LINE'] =~ /^#{string}(\s+)?$/
print ERB.new(ENV['TM_REF_LINK_FORMAT']).result
elsif ENV['TM_CURRENT_LINE'] =~ /\[(dev )?#{phrase.sub(/ iphone/,'')}/ || ENV['TM_CURRENT_LINE'] =~ /\[#{string}/
string = phrase if string.empty?
print "${1:#{string}}]: #{url}$0"
else
input = res if input.empty?
print ERB.new(ENV['TM_BLINK_FORMAT']).result
print ERB.new(ENV['TM_BLINK_FORMAT']).result.strip
end
</string>
<key>fallbackInput</key>
Expand Down
66 changes: 49 additions & 17 deletions Commands/Link iPhone App (web).tmCommand
Expand Up @@ -5,39 +5,71 @@
<key>beforeRunningCommand</key>
<string>nop</string>
<key>command</key>
<string>#!/usr/bin/env ruby -rjcode -Ku
<string>#!/usr/bin/ruby
SUPPORT = ENV['TM_SUPPORT_PATH']
DIALOG = ENV['DIALOG']
require SUPPORT + '/lib/ui'
require SUPPORT + '/lib/escape'
require SUPPORT + '/lib/osx/plist'
require SUPPORT + '/lib/exit_codes'
require "#{ENV['TM_BUNDLE_SUPPORT']}/lib/yahoo"
require 'net/http'
require 'erb'
require 'rexml/document'
input = STDIN.read
blogsite = "itunes.apple.com"
offset = 0
answer = TextMate::UI.request_string(:title =&gt; "Enter keywords",:prompt =&gt; "Find iPhone apps with keywords:",:default =&gt; input)
TextMate.exit_discard unless answer
query = e_url(answer + " inurl:/us/app/ site:#{blogsite}")
yahoo = WebSearch.new('TM_YAHOO', query, 'all', 25, 1, nil, 1)
query = answer + " title:\"on the App Store\" site:#{blogsite}"
ysuggest = []
yahoo.parse_results.each {|result|
title = result['Title'].gsub('"','&amp;raquo;').gsub(/&lt;\/?b&gt;/,'')
title = title[0..50]+"…" if title.length &gt; 50
ysuggest &lt;&lt; {
'title' =&gt; title,
'url' =&gt; result['Url'].gsub(/\?mt=8$/,''),
}
}
def do_search(offset,phrase)
escapedUrl = "http://api.search.live.net/xml.aspx?Appid=6B9E3A4B9F0D8F963A24815A0317BF1DCA3B0E9A&amp;query=#{e_url(phrase)}&amp;sources=web&amp;web.offset=#{offset}"
plist = { 'menuItems' =&gt; ysuggest }.to_plist
xml_data = Net::HTTP.get_response(URI.parse(escapedUrl)).body
doc = REXML::Document.new(xml_data)
bings = []
doc.elements.each('SearchResponse/web:Web/web:Results/web:WebResult') do |result|
url = result.elements['web:Url'].text
if url =~ /\/app\//
title = result.elements['web:Title'].text.gsub('"','&amp;raquo;').gsub("'",'&amp;rsquo;').gsub(/&lt;\/?b&gt;/,'')
title = title[0..50]+"…" if title.length &gt; 50
begin
bings &lt;&lt; {
'title' =&gt; title,
'url' =&gt; url
}
rescue
TextMate.exit_show_tool_tip "Error in returned data"
end
end
end
bings &lt;&lt; {
'title' =&gt; 'More results…',
'url' =&gt; ''
} unless bings.empty?
res = OSX::PropertyList.load(`#{e_sh DIALOG} -up #{e_sh plist}`)
TextMate.exit_show_tool_tip "No matches" if bings.empty?
plist = { 'menuItems' =&gt; bings }.to_plist
res = OSX::PropertyList.load(`#{e_sh DIALOG} -up #{e_sh plist}`)
unless res.has_key? 'selectedMenuItem'
if ENV['TM_CURRENT_LINE'] =~ /^\[/
TextMate.exit_insert_text "]: "
else
TextMate.exit_discard
end
end
if res['selectedMenuItem']['title'] == "More results…"
offset += 10
do_search(offset,phrase)
else
return res
end
end
res = do_search(offset,query)
TextMate.exit_show_tool_tip "No links found or nothing selected" unless res.has_key? 'selectedMenuItem'
url = res['selectedMenuItem']['url']
title = res['selectedMenuItem']['title']
price = ""
Expand All @@ -62,7 +94,7 @@ end
<key>keyEquivalent</key>
<string>^@w</string>
<key>name</key>
<string>Link iPhone App (web)</string>
<string>Link iTunes App (web)</string>
<key>output</key>
<string>insertAsSnippet</string>
<key>scope</key>
Expand Down
57 changes: 35 additions & 22 deletions Commands/Make Tag Link.tmCommand
Expand Up @@ -12,9 +12,10 @@ require SUPPORT + '/lib/ui'
require SUPPORT + '/lib/escape'
require SUPPORT + '/lib/osx/plist'
require SUPPORT + '/lib/exit_codes'
require ENV['TM_BUNDLE_SUPPORT'] + '/lib/yahoo'
require ENV['TM_BUNDLE_SUPPORT'] + '/lib/cooldialog'
require 'erb'
require 'net/http'
require 'rexml/document'
input = STDIN.read
Expand All @@ -25,30 +26,44 @@ end
blogsite = ENV['BLOG_SITE'] ? ENV['BLOG_SITE'] : "www.tuaw.com"
blogsite = "www." + blogsite unless blogsite =~ /^www\./
offset = 0
linktext = TextMate::UI.request_string(:title =&gt; "Search Query",:prompt =&gt; "Enter terms to create a tag link",:default =&gt; input)
elegant_exit("Cancelled") unless linktext
query = e_url(linktext + " site:#{blogsite}/tag")
query = linktext + " site:#{blogsite}/tag"
yahoo = WebSearch.new('TM_YAHOO', query, 'all', 20, 1, nil, 1)
escapedUrl = "http://api.search.live.net/xml.aspx?Appid=6B9E3A4B9F0D8F963A24815A0317BF1DCA3B0E9A&amp;query=#{e_url(query)}&amp;sources=web&amp;web.offset=#{offset}"
if yahoo.parse_results.length &gt; 0
ysuggest = []
yahoo.parse_results.each {|result|
unless (result['Url'] =~ /bloglines|page/)
tag_title = result['Title'].gsub(/ -- TUAW/,'')
ysuggest &lt;&lt; {
'title' =&gt; tag_title.gsub('"','&amp;raquo;'),
'url' =&gt; result['Url']}
end
}
xml_data = Net::HTTP.get_response(URI.parse(escapedUrl)).body
doc = REXML::Document.new(xml_data)
bings = []
doc.elements.each('SearchResponse/web:Web/web:Results/web:WebResult') do |result|
unless (result.elements['web:Title'].text =~ /bloglines|page/)
begin
tag_title = result.elements['web:Title'].text.gsub(/ -- TUAW/,'').gsub('"','&amp;raquo;').gsub("'",'&amp;rsquo;')
bings &lt;&lt; {
'title' =&gt; tag_title,
'url' =&gt; result.elements['web:Url'].text
}
rescue
TextMate.exit_show_tool_tip "Error in returned data"
end
end
end
elegant_exit("No related tags found in index") if bings.empty?
plist = { 'menuItems' =&gt; ysuggest }.to_plist
res = OSX::PropertyList.load(`#{e_sh DIALOG} -up #{e_sh plist}`)
elegant_exit('cancelled') unless res.has_key? 'selectedMenuItem'
url = res['selectedMenuItem']['url']
title = res['selectedMenuItem']['title']
plist = { 'menuItems' =&gt; bings }.to_plist
res = OSX::PropertyList.load(`#{e_sh DIALOG} -up #{e_sh plist}`)
unless res.has_key? 'selectedMenuItem'
if ENV['TM_CURRENT_LINE'] =~ /^\[/
TextMate.exit_insert_text "]: "
else
elegant_exit('cancelled')
end
end
url = res['selectedMenuItem']['url']
title = res['selectedMenuItem']['title']
TextMate.exit_replace_text url if ENV['TM_CURRENT_LINE'] =~ /^link: #{input}/
if (input.empty? &amp;&amp; !(ENV['TM_SCOPE'].scan(/markdown/).empty?) &amp;&amp; ENV['TM_CURRENT_LINE'] =~ /^(\s+)?$/) || ENV['TM_CURRENT_LINE'] =~ /^(#{input})?(\s+)?$/
Expand All @@ -57,9 +72,7 @@ if (input.empty? &amp;&amp; !(ENV['TM_SCOPE'].scan(/markdown/).empty?) &amp;&amp
else
print ERB.new(ENV['TM_BLINK_FORMAT']).result
end
else
elegant_exit("No related tags found in index")
end</string>
</string>
<key>fallbackInput</key>
<string>word</string>
<key>input</key>
Expand Down
3 changes: 2 additions & 1 deletion Snippets/Post Headers.tmSnippet
Expand Up @@ -8,7 +8,8 @@ title: ${1:Post Title}
link: ${2:http://}
categories: $3
tags: $4
appid: $5
excerpt: $6
$0</string>
<key>name</key>
<string>Post Headers</string>
Expand Down

0 comments on commit 5ac3fe8

Please sign in to comment.