forked from fazibear/blirc
/
blirc.rb
155 lines (130 loc) · 3.76 KB
/
blirc.rb
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
require 'rubygems'
require 'sinatra'
require 'open-uri'
require 'md5'
require 'oauth'
require 'oauth/consumer'
require 'json'
enable :sessions
configure do
$oKEY = 'VdulYO3aqGeSTy0D6dsa'
$oPASS = 'LDl3M1r9szhymatoxNtUEM7QN20nkQif2VgSDu0w'
end
configure :production do
$oKEY = 'gkaRVeN4gYyjAaqW2nJR'
$oPASS = 'WmTawWjg9EVHy24IuxD7wJczwfP7zn2he3y195l8'
end
before do
session[:oauth] ||= {}
@consumer ||= OAuth::Consumer.new $oKEY, $oPASS, {
:site => "http://blip.pl"
}
if !session[:oauth][:request_token].nil? && !session[:oauth][:request_token_secret].nil?
@request_token = OAuth::RequestToken.new(@consumer, session[:oauth][:request_token], session[:oauth][:request_token_secret])
end
if !session[:oauth][:access_token].nil? && !session[:oauth][:access_token_secret].nil?
@access_token = OAuth::AccessToken.new(@consumer, session[:oauth][:access_token], session[:oauth][:access_token_secret])
session[:user] ||= get_current_user
@user = session[:user]
end
end
helpers do
LINK_REGEXP = %r{https?:\/\/[^\>\<\s\"\]]+}
TAG_REGEXP = %r{#[0-9a-zA-ZĄĆĘŃÓŁŚŹŻąćęńółśźżäëïöüÄËÏÖÜ_-]+}
USER_REGEXP = %r{(?!\b)+(\^)(\w+)}
def get_dashboard_since
since = session[:last_id]
return "" unless since
begin
json = @access_token.get("/dashboard/since/#{since}?include=user,recipient&limit=50").body
updates = JSON.parse(json)
rescue => e
return ""
end
return "" unless updates.any?
session[:last_id] = updates.first['id']
dash_to_txt(updates.reverse)
end
def get_dashboard
begin
json = @access_token.get("/dashboard?include=user,recipient&limit=50").body
updates = JSON.parse(json)
rescue => e
return ""
end
return "" unless updates.any?
session[:last_id] = updates.first['id']
dash_to_txt(updates.reverse)
end
def format_body(body)
body.gsub(TAG_REGEXP) do |match|
%{<span class="tag_color">#{match}</span>}
end.gsub(LINK_REGEXP) do |match|
%{<a href="#{match}" class="link_color">[link]</a>}
end.gsub(USER_REGEXP) do |match|
%{<span class="user_color">#{match}</span>}
end
end
def dash_to_txt(dash)
out = ""
dash.each do |update|
out << erb(:line, :layout => false, :locals => {
:date => time_format(Time.parse(update["created_at"])),
:user => update["user"]["login"],
:recipient => update["recipient"] ? update["recipient"]["login"] : "",
:kind => update["type"],
:body => format_body(update["body"])
} )
end
out
end
def get_current_user
begin
puts "ask for user"
json = @access_token.get('/profile?include=background').body
JSON.parse(json)
rescue => e
{}
end
end
def time_format(time)
time.strftime('%H:%M %d-%m-%Y')
end
end
get "/" do
if @access_token
@background_url = @user["background"]["url"]
@current_user = @user["login"]
@body = get_dashboard
erb :console
else
erb :start
end
end
get "/refresh" do
get_dashboard_since
end
post "/request" do
@request_token = @consumer.get_request_token :oauth_callback => ""
session[:oauth][:request_token] = @request_token.token
session[:oauth][:request_token_secret] = @request_token.secret
redirect @request_token.authorize_url
end
get "/callback" do
@access_token = @request_token.get_access_token :oauth_verifier => params[:oauth_verifier]
session[:oauth][:access_token] = @access_token.token
session[:oauth][:access_token_secret] = @access_token.secret
redirect "/"
end
post "/update" do
begin
@access_token.post("/updates?include=user,recipient", { 'update[body]' => params[:text] })
rescue => e
return ""
end
""
end
get "/logout" do
session[:oauth] = {}
redirect "/"
end