#!/usr/bin/env ruby
#
#  tw2td.rb - 過去のツイートを取得し、tDiary にポストするスクリプト
#
#   (c) 2012, Norihisa Washitake http://washitake.com/diary/public/
#

# thumbnail information retrieved from
# http://blog.irons.jp/2009/12/23/twitter_thumb_url/
$thumb_url_patterns = [
  # Twitpic
  [/http:\/\/twitpic[.]com\/(\w+)/,
  '<img src="http://twitpic.com/show/thumb/\1" width="150" height="150" />'],
  
  # Mobypicture
  [/http:\/\/moby[.]to\/(\w+)/, '<img src="http://moby.to/\1:small" />'],
  
  # yFrog
  [/http:\/\/yfrog[.]com\/(\w+)/, '<img src="http://yfrog.com/\1.th.jpg" />'],
  
  # 携帯百景
  [/http:\/\/movapic[.]com\/pic\/(\w+)/,
   '<img src="http://image.movapic.com/pic/s_\1.jpeg" />'],
  
  # はてなフォトライフ
  [/http:\/\/f[.]hatena[.]ne[.]jp\/(([\w\-])[\w\-]+)\/((\d{8})\d+)/,
   '<img src="http://img.f.hatena.ne.jp/images/fotolife/\2/\1/\4/\3_120.jpg" />'],
  
  # PhotoShare
  [/http:\/\/(?:www[.])?bcphotoshare[.]com\/photos\/\d+\/(\d+)/,
   '<img src="http://images.bcphotoshare.com/storages/\1/thumb180.jpg" width="180" height="180" />'],
  
  # img.ly
  [/http:\/\/img[.]ly\/(\w+)/, '<img src="http://img.ly/show/thumb/\1" width="150" height="150" />'],
  
  # brightkite
  [/http:\/\/brightkite[.]com\/objects\/((\w{2})(\w{2})\w+)/, '<img src="http://cdn.brightkite.com/\2/\3/\1-feed.jpg" />'],
  
  # Twitgoo
  [/http:\/\/twitgoo[.]com\/(\w+)/,
   '<img src="http://twitgoo.com/\1/mini" />'],
  
  # pic.im
  [/http:\/\/pic[.]im\/(\w+)/,
   '<img src="http://pic.im/website/thumbnail/\1" />'],
  
  # youtube
  [/http:\/\/(?:www[.]youtube[.]com\/watch(?:\?|#!)v=|youtu[.]be\/)([\w\-]+)(?:[-_.!~*\'()a-zA-Z0-9;\/?:@&=+$,%#]*)/,
   '<img src="http://i.ytimg.com/vi/\1/hqdefault.jpg" width="240" height="180" />'],
  
  # imgur
  [/http:\/\/imgur[.]com\/(\w+)[.]jpg/,
   '<img src="http://i.imgur.com/\1l.jpg" />'],
  
  # TweetPhoto, Plixi, Lockerz
  [/http:\/\/tweetphoto[.]com\/\d+|http:\/\/plixi[.]com\/p\/\d+|http:\/\/lockerz[.]com\/s\/\d+/,
   '<img src="http://api.plixi.com/api/TPAPI.svc/imagefromurl?size=mobile&url=\0" />'],
  
  # Ow.ly
  [/http:\/\/ow[.]ly\/i\/(\w+)/,
   '<img src="http://static.ow.ly/photos/thumb/\1.jpg" width="100" height="100" />'],
  
  # Instagram
  [/http:\/\/instagr[.]am\/p\/([\w\-]+)\//,
   '<img src="http://instagr.am/p/\1/media/?size=t" width="150" height="150" />'],
]

def get_thumb_url(link)
  $thumb_url_patterns.each do |pair|
    ptn = pair[0]
    lnk = pair[1]

    return link.sub(ptn, lnk) if ptn.match(link)
  end
  return ""
end

