Skip to content

Instantly share code, notes, and snippets.

@josephcc
Created December 12, 2013 21:34
Show Gist options
  • Select an option

  • Save josephcc/7935886 to your computer and use it in GitHub Desktop.

Select an option

Save josephcc/7935886 to your computer and use it in GitHub Desktop.
class FindClipsController < ApplicationController
layout false
def clip_for_option
@option = Option.find(params[:id])
@question = @option.question
render 'clip'
end
def clip_for_source
@source = Source.find(params[:id])
@question = @source.question
@options = @question.options
render 'clip'
end
def new
render :layout => 'application'
end
def bing(keywords)
begin
bing = Bing.new('yNHg1Tb5VSloNuV6/ZlKGt44m6xthgVMeryDx1oB0p8', 50, 'Web') # this key is good for 5000 searches / month
results = bing.search(keywords)
html = ''
results[0][:Web].each do |r|
html += "<a href='#{r[:Url]}'><h4>#{r[:Title]}</h4></a><p>#{r[:DisplayUrl]}</p><p>#{r[:Description]}</p><hr/>"
end
html = "<div>#{html}</div>"
render :json => {:title => "Search results for `#{keywords}'", :content => html, :can_clip => false}
rescue
render :json => {:title => 'error', :content => 'not a url or search function is down, try pasting a url.', :can_clip => false}
end
end
def readability
url = params[:url]
question_id = params[:question_id]
if url.nil? or question_id.nil?
render :json => {:title => 'error', :content => 'no url or question_id', :can_clip => false}
end
if url =~ URI::regexp
s = Source.by_question(question_id).where(:url => url)
if not s.nil? and s.length > 0
while s.first.text.nil?
Thread.sleep(0.1)
s.reload
end
s = s.first
else
s = Source.new
s.question_id = question_id
s.url = url
s.save()
end
render :json => {:title => s.title, :content => s.text, :source_id => s.id, :can_clip => true}
else
self.bing(url)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment