File: bitly.rb

package info (click to toggle)
mikutter 4.1.3%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,260 kB
  • sloc: ruby: 20,126; sh: 183; makefile: 19
file content (85 lines) | stat: -rw-r--r-- 2,763 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# -*- coding: utf-8 -*-
require 'cgi'

module Plugin::Bitly
  USER = 'mikutter'.freeze
  APIKEY = 'R_70170ccac1099f3ae1818af3fa7bb311'.freeze
  SHRINKED_MATCHER = %r[\Ahttp://(bit\.ly|j\.mp)/].freeze

  extend self

  # bitlyユーザ名を返す
  def user
    if UserConfig[:bitly_user] == '' or not UserConfig[:bitly_user]
      USER
    else
      UserConfig[:bitly_user]
    end end

  # bitly API keyを返す
  def apikey
    if UserConfig[:bitly_apikey] == '' or not UserConfig[:bitly_apikey]
      APIKEY
    else
      UserConfig[:bitly_apikey]
    end end

  def expand_url_many(urls)
    query = "login=#{user}&apiKey=#{apikey}&" + urls.map{ |url|
      "shortUrl=#{CGI.escape(url)}" }.join('&')
    3.times do
      result = begin
                 JSON.parse(Net::HTTP.get("api.bit.ly", "/v3/expand?#{query}"))
               rescue Exception
                 nil end
      if result and result['status_code'].to_i == 200
        return Hash[ *result['data']['expand'].map{|token|
                       [token['short_url'], token['long_url']] }.flatten ] end
    end
  end
end

Plugin.create :bitly do
  expand_mutex = Mutex.new
  waiting_expand_entities = TimeLimitedStorage.new(String, Set, 30)

  # URLを展開し、entityを更新する
  # bit.lyの一度のリクエストでexpandできる最大数は15
  # http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/expand
  expand_queue = TimeLimitedQueue.new(15, 0.5, Set) do |set|
    Thread.new {
      expand_mutex.synchronize do
        Plugin::Bitly.expand_url_many(set).each do |shrinked, expanded|
          waiting_expand_entities[shrinked].each do |query|
            query.(expanded)
          rescue => exception
            error exception
          end
        end
      end
    }.trap{|exception|
      warn exception
      set.each do |url|
        waiting_expand_entities[url].each do
          query.call nil end end
    }.next {
      set.each do |url|
        waiting_expand_entities[url] = Set.new end
    }.terminate end

  on_gui_timeline_add_messages do |i_timeline, messages|
    messages.map(&:entity).each do |entity|
      entity.select{|_|
        :urls == _[:slug] and Plugin::Bitly::SHRINKED_MATCHER =~ _[:url]
      }.each do |link|
        expand_mutex.synchronize do
          (waiting_expand_entities[link[:url]] ||= Set.new) << ->expanded{
            entity.add link.merge(url: expanded, face: expanded) }
          expand_queue.push link[:url] end end end end

  filter_expand_url do |urlset|
    divided = urlset.group_by{|url| !!(Plugin::Bitly::SHRINKED_MATCHER =~ url) }
    divided[false] ||= []
    divided[true] ||= []
    [divided[false] + divided[true].each_slice(15).map{|chunk|Plugin::Bitly.expand_url_many(chunk).values}.flatten] end
end