-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample_twitter.lua
More file actions
219 lines (179 loc) · 5.27 KB
/
example_twitter.lua
File metadata and controls
219 lines (179 loc) · 5.27 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package.path = package.path .. ';lib/?.lua'
require "weblua"
util = require "util"
local itemIndex = -1
local tweets = nil
---Escreve um texto na parte inferior da área do canvas lua
--@param text Texto a ser escrito
function writeText(text)
canvas:attrColor(255, 255, 255, 0)
canvas:clear();
local cw, ch = canvas:attrSize()
canvas:attrFont("vera", 24)
local tw, th = canvas:measureText("A")
canvas:drawText(5, ch-th, text)
canvas:flush()
end
function breakString(str, maxLineSize)
local t = {}
local i, fim, countLns = 1, 0, 0
if (str == nil) or (str == "") then
return t
end
str = string.gsub(str, "\n", " ")
str = string.gsub(str, "\r", " ")
while i < #str do
countLns = countLns + 1
if i > #str then
t[countLns] = str
i = #str
else
fim = i+maxLineSize-1
if fim > #str then
fim = #str
else
--se o caracter onde a string deve ser quebrada
--não for um espaço, procura o próximo espaço
if string.byte(str, fim) ~= 32 then
fim = string.find(str, ' ', fim)
if fim == nil then
fim = #str
end
end
end
t[countLns]=string.sub(str, i, fim)
i=fim+1
end
end
return t
end
function showItem()
if (itemIndex < 0) or (#tweets == 0) then
return
end
local i = itemIndex
title = tweets[i].text
local cw, ch = canvas:attrSize()
canvas:attrColor(255, 255, 255, 255)
canvas:attrColor("white")
canvas:drawRect("fill", 0, 0, cw, ch)
canvas:attrColor("blue")
canvas:attrFont("vera", 24)
local cat = tweets[i].created_at or ""
cat = cat .. ' (' .. itemIndex ..' de '.. #tweets ..')'
canvas:drawText(5, 0, cat)
canvas:attrColor("black")
canvas:attrFont("vera", 22)
local tw, th = canvas:measureText("a")
local charsByLine = tonumber(string.format("%d", cw / tw))
local desc = title
local desctb = breakString(desc, charsByLine)
local y = 30
for k,ln in pairs(desctb) do
canvas:drawText(5, y, ln)
y = y + th + 4
end
local imgAtualizar = canvas:new("media/atualizar.png")
local imw, imh = imgAtualizar:attrSize()
canvas:compose(cw-2*imw, ch-imh, imgAtualizar)
local imgFechar = canvas:new("media/fechar.png")
local imw, imh = imgFechar:attrSize()
canvas:compose(cw-imw, ch-imh, imgFechar)
local imgEsq = canvas:new("media/esq.png")
local imw, imh = imgEsq:attrSize()
canvas:compose(cw-imw*2, 0, imgEsq)
local imgDir = canvas:new("media/dir.png")
canvas:compose(cw-imw, 0, imgDir)
canvas:flush()
end
function callback(table)
print 'callback'
tweets = table
itemIndex = 1
showItem()
-- print('response_table:', table)
-- for k, v in pairs(table) do
-- print(k,":", v)
-- for i, j in pairs(v) do
-- print('--->', i,":", j)
-- end
-- end
end
--[[
function callback(header, body)
if body then
xmlhandler = xml_to_table(body)
for i=1,5,1 do
print("\tTweet: ", xmlhandler.root.rss.channel.item[i].title)
print("\tData de Publicação: ", xmlhandler.root.rss.channel.item[i].pubDate)
end
if xmlhandler.root.rss == nil then
canvas:clear()
canvas:flush()
return
end
print("Total de itens:\t", #xmlhandler.root.rss.channel.item)
itemIndex = 1
showItem()
end
end
]]
---Retorna um novo índice de notícia a ser exibida.
--@param index Valor do índice da notícia atualmente exibida
--@param forward Se igual a true, incrementa o índice em 1,
--senão, decrementa em 1.
--@returns Retorna o novo índice da notícia a ser exibida.
function moveItemIndex(index, forward)
--Se o index for menor que zero, é porque
--o XML do feed ainda não foi baixado, logo,
--não há notícia a ser exibida.
if index < 0 then
return index
end
if forward then
index = index + 1
if index > #tweets then
index = 1
end
else
index = index - 1
if index <= 0 then
index = #tweets
end
end
return index
end
local flag = false
function handler(evt)
if flag ~= true then
writeText("Buscando tweets...")
local paramstable = {
screen_name='leojsandrade',
}
rest_request("http://api.twitter.com/1/statuses/user_timeline.json", paramstable, callback, 'json', 'get')
writeText("")
flag = true
end
if (evt.class == 'key' and evt.type == 'press') then
print("key:", evt.key)
local ok = false
if evt.key == 'BLUE' then
writeText("Buscando tweets...")
local paramstable = {
screen_name='leojsandrade',
}
rest_request("http://api.twitter.com/1/statuses/user_timeline.json", paramstable, callback, 'json', 'get')
end
if evt.key == "CURSOR_RIGHT" then
ok = true
itemIndex = moveItemIndex(itemIndex, true)
elseif evt.key == "CURSOR_LEFT" then
ok = true
itemIndex = moveItemIndex(itemIndex, false)
end
if ok then
showItem()
end
end
end
event.register(handler)