PageRenderTime 80ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/plugins/favotter.rb

https://github.com/Epictetus/termtter
Ruby | 77 lines | 67 code | 9 blank | 1 comment | 6 complexity | cbf901cb651568616404a32ad20ac0aa MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. require 'rubygems'
  3. require 'nokogiri'
  4. require 'open-uri'
  5. module Termtter::Client
  6. public_storage[:favorited_ids] = {}
  7. class << self
  8. def output_favorites(target, threshold)
  9. favorites = parse("http://favotter.matope.com/user.php?user=#{target}&threshold=#{threshold}")
  10. public_storage[:favorited_ids].clear
  11. alphabet = '$a'
  12. max_amount_width = favorites.map {|f| now = f[2].to_s.size }.max
  13. favorites.reverse.each do |id, text, amount, users|
  14. public_storage[:favorited_ids][alphabet] = id
  15. color = fav_color(amount)
  16. fav = "fav#{amount == 1 ? '' : 's'}"
  17. favorites_info = "(#{amount} #{fav})".rjust(max_amount_width + 7)
  18. format = "<GREEN>#{favorites_info} #{alphabet}</GREEN> <YELLOW>%s</YELLOW>: <#{color}>%s</#{color}>"
  19. values = [users.join(', '), CGI.escape(text)]
  20. puts CGI.unescape(TermColor.parse(format % values ))
  21. alphabet.succ!
  22. end
  23. end
  24. private
  25. def parse(url)
  26. doc = Nokogiri(open(url).read)
  27. doc.css('div.entry').map do |entry|
  28. id = entry['id'].gsub(/\Astatus_/, '')
  29. text = CGI.unescapeHTML(entry.css('span.status_text').first.content)
  30. amount = entry.css('div.info span.favotters').first.content
  31. amount = amount.match(/(\d+)/)[1].to_i
  32. users = entry.css('div.info span.favotters img').map {|u| u['title'] }
  33. [id, text, amount, users]
  34. end
  35. end
  36. def fav_color(amount)
  37. case amount
  38. when 1 then 'WHITE'
  39. when 2 then 'GREEN'
  40. when 3 then 'BLUE'
  41. when 4 then 'BLUE'
  42. else 'RED'
  43. end
  44. end
  45. end
  46. help = ['favotter [USERNAME] [THRESHOLD]', 'Show info from favotter']
  47. register_command('favotter', :help => help) do |arg|
  48. target =
  49. if arg.empty?
  50. config.user_name
  51. else
  52. args = arg.split
  53. threshold = args.size == 1 ? 1 : args[1]
  54. args[0]
  55. end
  56. if /@(.*)/ =~ target
  57. target = $1
  58. end
  59. output_favorites target, threshold
  60. end
  61. help = ['favotter_fav [FavoritedID]', 'Favorite favorited status']
  62. register_command('favotter_fav', :alias => :ffav, :help => help) do |arg|
  63. raise 'need favorited_id' if arg.empty?
  64. if id = public_storage[:favorited_ids][arg]
  65. execute("favorite #{id}")
  66. end
  67. end
  68. end