Created
January 27, 2016 01:46
-
-
Save emasaka/8387a5ed8deed9314c10 to your computer and use it in GitHub Desktop.
Termtter plugin for nagaino
This file contains 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
# -*- coding: utf-8 -*- | |
require 'net/http' | |
require 'cgi/util' | |
require 'json' | |
require 'open-uri' | |
require 'regexp_trie' | |
URL_SHORTTERS_RE = open('http://nagaino.herokuapp.com/hosts.json') do |io| | |
/(?:#{RegexpTrie.union(JSON.load(io)).source})[\w]+/ | |
end | |
config.plugins.nagaino.set_default(:skip_users, []) | |
module Termtter::Client | |
register_hook( | |
:name => :nagaino, | |
:point => :filter_for_output, | |
:exec_proc => lambda do |statuses, event| | |
skip_users = config.plugins.nagaino.skip_users | |
shorturls = [] | |
statuses.each do |s| | |
skip_users.include?(s.user.screen_name) and next | |
s.text.scan(URL_SHORTTERS_RE) { shorturls << $& } | |
end | |
expand_h = query_nagaino(shorturls) | |
statuses.each do |s| | |
skip_users.include?(s.user.screen_name) and next | |
s.text.gsub!(URL_SHORTTERS_RE) {|m| expand_h[m] || m } | |
end | |
statuses | |
end | |
) | |
def self.query_nagaino(urls) | |
http_class = | |
if config.proxy.host.nil? or config.proxy.host.empty? | |
Net::HTTP::Proxy(config.proxy.host, config.proxy.port, | |
config.proxy.user_name, config.proxy.password ) | |
else | |
Net::HTTP | |
end | |
endpoint = 'http://nagaino.herokuapp.com/api/v0/expandText' | |
res = http_class.post_form(URI.parse(endpoint), | |
{'shortUrls' => CGI.escape(urls.join("\n")), | |
'format' => 'json_simple' }) | |
JSON.parse(res.body)['data']['expand'] | |
rescue | |
{} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment