I wanted to play around with http.request, so I thought I would write a program that will allow a search of all the discussion titles in the Codea forum. It takes about a minute to read all 67 pages and extract the titles (50 per page), but once read you can scroll thru the titles (about 3,312 so far). There is a fast scroll (sliding your finger up/down the right side of the screen) or a slow scroll (sliding your finger up/down the left side of the screen). There is also a character by character search function that displays the titles containing what is keyed in the string area as each character is keyed. Just tap in the string area and key what you want to search for. There is a title count that shows how many titles are being shown, either all or just the matched titles. Use the backspace key to clear the string area to redisplay all the titles or to start a new search. When the program is started, the page currently being read is shown in the pageRequested area. Right now there are 67 pages, but that count is growing fast. If what you want to search for or look at isn’t very far back, a triple tap will abort the page request function and allow you to look at what is loaded so far. There isn’t an option to continue an aborted request, so you’ll have to restart the program if you want to load every page. To keep the program small, I didn’t add anything to save the titles or to add a function to display the text of any title discussion. To the left of each title is a page number so you can look in the forum on that page for the title you are interested in. You can flip between the Codea forum and this program to view any discussion. It’s possible that the page number won’t match the forum page if there were some discussions added since the program was started. I think this will be handy for the new coders or anyone else, because you can easily search the full forum for something you might be interested in or have a question about.
function setup()
srch={}
title={}
searchFound=false
pageDone=true
doneReading=false
page=0
yOffset=0
str1='http://www.twolivesleft.com/Codea/Talk/discussions'
parameter.text("string","",searchTitle)
parameter.watch("pageRequested")
parameter.watch("titles")
end
function draw()
background(40,40,50)
fill(255)
textMode(CORNER)
getPages()
if searchFound then
showFound()
else
showAll()
end
end
function showAll() -- show all titles
line=0
titles=0
for z=1,#title do
for y=1,#title[z] do
titles=titles+1
if titles>yOffset and titles<yOffset+26 then
line=line+1
text("--------------------------",1,HEIGHT-line*20)
line=line+1
text(z.." "..title[z][y],10,HEIGHT-line*20)
end
end
end
end
function showFound() -- show matched titles
line=0
titles=0
for z=1,#srch do
titles=titles+1
if titles>yOffset and titles<yOffset+26 then
line=line+1
text("--------------------------",1,HEIGHT-line*20)
line=line+1
text(srch[z],10,HEIGHT-line*20)
end
end
end
function getPages() -- read next page
if pageDone and not doneReading then
page=page+1
pageDone=false
pageRequested=string.format("%s/p%d",str1,page)
http.request(pageRequested,gotData,failed)
end
end
function failed(error) -- read error
print(error)
end
function searchTitle() -- search title for string
yOffset=0
srch={}
for z=1,#title do
for y=1,#title[z] do
s,e=string.find(string.lower(title[z][y]),string.lower(string))
if s~=nil then
searchFound=true
table.insert(srch,z.." "..title[z][y])
end
end
end
end
function gotData(data,status,headers) -- page received
title[page]={}
findDiscussion(data,page)
end
function findDiscussion(str,pg) -- find discussion titles by page
s=1
itemFound=false
while true do
s,e=string.find(str,'ItemContent Discussion',s)
if s==nil then
pageDone=true
if not itemFound then
pageRequested="complete"
doneReading=true
end
return
end
itemFound=true
s1,e1=string.find(str,'"Title">',e)
s2,e2=string.find(str,'</a>',e1)
st=string.sub(str,e1+1,s2-1)
table.insert(title[pg],st)
s=e2
end
end
function touched(t)
if t.state==BEGAN and t.tapCount==3 then -- abort page request
doneReading=true
pageRequested="request aborted"
end
if t.state==MOVING then
if t.x>WIDTH/2 then -- right side of screen
yOffset=yOffset+t.deltaY -- fast scroll
else
yOffset=yOffset+t.deltaY/40 -- slow scroll
end
if yOffset<0 then -- top of list
yOffset=0
end
if yOffset+10>titles then -- bottom of list
yOffset=titles-10
end
end
end