This is something I have been bashing around for a while. It only works with the ifsdb.org online book database but it could be adapted to any web pages when you know which commands they use, how they return results ect.
I just made a simple program with other peoples code and lots of help to query the page for author and title and then return certain results that I wanted. Still needs cleaning up and a few extras added but it works here quite well. There is a small gui to make it easier to see what you are doing and return results graphically.
Let me know if it is of any use and can be improved somehow.
Code: Select all
;----------------------------------------------------------------------------------------------------------------------------
; Original code from from: El_Choni, http://forums.purebasic.com/english/viewtopic.php?t=15891
; Adapted by: F.Weil 20050719
; Bashed soundly by: MrMat
; Stuffed up thoroughly: Fang
; Forms and extra code: Fang
;
;============================================================================================================================
; prototypes
;============================================================================================================================
;============================================================================================================================
; declarations
;============================================================================================================================
Structure bookdetails
title.s
authors.s
year.s
isbn.s
publisher.s
price.s
pages.s
type.s
picture.s
EndStructure
;============================================================================================================================
;
;============================================================================================================================
Declare.s GetTitle(Author.s, Title.s)
Declare GetPage(URL.s)
Declare CheckError(value, sMessage.s, terminate)
Declare.s GetTitlePage(TitleNumber.s, Title.s)
Declare.l GetBookDetails(Bookreference.s, *details.bookdetails)
;============================================================================================================================
; constants
;============================================================================================================================
Enumeration 1
#Window_getbook
EndEnumeration
#WindowIndex = #PB_Compiler_EnumerationValue
Enumeration 1
#Gadget_getbook_fmain
#Gadget_getbook_lsauthor
#Gadget_getbook_sauthor
#Gadget_getbook_lstitle
#Gadget_getbook_stitle
#Gadget_getbook_fdetails
#Gadget_getbook_lauthor
#Gadget_getbook_author
#Gadget_getbook_ltitle
#Gadget_getbook_title
#Gadget_getbook_lyear
#Gadget_getbook_year
#Gadget_getbook_lisbn
#Gadget_getbook_isbn
#Gadget_getbook_lpublisher
#Gadget_getbook_publisher
#Gadget_getbook_lprice
#Gadget_getbook_price
#Gadget_getbook_lpages
#Gadget_getbook_pages
#Gadget_getbook_ltype
#Gadget_getbook_type
#Gadget_getbook_lpicture
#Gadget_getbook_picture
#Gadget_getbook_lnote
#Gadget_getbook_note
#Shortcut_getbook_enter
EndEnumeration
#GadgetIndex = #PB_Compiler_EnumerationValue
Enumeration 1
#StatusBar_getbook
EndEnumeration
#StatusBarIndex =#PB_Compiler_EnumerationValue
#StatusBar_getbook_messages = 0
;============================================================================================================================
; windows
;============================================================================================================================
Procedure.l Window_getbook()
If OpenWindow(#Window_getbook,108,81,500,345,"Get book details, press ENTER in title field to start the search",#PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_ScreenCentered|#PB_Window_Invisible)
FrameGadget(#Gadget_getbook_fmain,0,0,500,65,"")
TextGadget(#Gadget_getbook_lsauthor,5,15,100,20,"Author",#PB_Text_Center)
SetGadgetFont(#Gadget_getbook_lsauthor,LoadFont(#Gadget_getbook_lsauthor,"Arial",12,0))
StringGadget(#Gadget_getbook_sauthor,105,10,390,25,"")
SetGadgetFont(#Gadget_getbook_sauthor,LoadFont(#Gadget_getbook_sauthor,"Arial",12,0))
TextGadget(#Gadget_getbook_lstitle,5,40,100,20,"Title",#PB_Text_Center)
SetGadgetFont(#Gadget_getbook_lstitle,LoadFont(#Gadget_getbook_lstitle,"Arial",12,0))
StringGadget(#Gadget_getbook_stitle,105,35,390,25,"")
SetGadgetFont(#Gadget_getbook_stitle,LoadFont(#Gadget_getbook_stitle,"Arial",12,0))
FrameGadget(#Gadget_getbook_fdetails,0,60,500,265,"")
TextGadget(#Gadget_getbook_lauthor,5,75,100,20,"Author",#PB_Text_Center)
SetGadgetFont(#Gadget_getbook_lauthor,LoadFont(#Gadget_getbook_lauthor,"Arial",12,0))
StringGadget(#Gadget_getbook_author,105,70,390,25,"")
SetGadgetFont(#Gadget_getbook_author,LoadFont(#Gadget_getbook_author,"Arial",12,0))
TextGadget(#Gadget_getbook_ltitle,5,100,100,20,"Title",#PB_Text_Center)
SetGadgetFont(#Gadget_getbook_ltitle,LoadFont(#Gadget_getbook_ltitle,"Arial",12,0))
StringGadget(#Gadget_getbook_title,105,95,390,25,"")
SetGadgetFont(#Gadget_getbook_title,LoadFont(#Gadget_getbook_title,"Arial",12,0))
TextGadget(#Gadget_getbook_lyear,5,125,100,20,"Year",#PB_Text_Center)
SetGadgetFont(#Gadget_getbook_lyear,LoadFont(#Gadget_getbook_lyear,"Arial",12,0))
StringGadget(#Gadget_getbook_year,105,120,390,25,"")
SetGadgetFont(#Gadget_getbook_year,LoadFont(#Gadget_getbook_year,"Arial",12,0))
TextGadget(#Gadget_getbook_lisbn,5,150,100,20,"ISBN",#PB_Text_Center)
SetGadgetFont(#Gadget_getbook_lisbn,LoadFont(#Gadget_getbook_lisbn,"Arial",12,0))
StringGadget(#Gadget_getbook_isbn,105,145,390,25,"")
SetGadgetFont(#Gadget_getbook_isbn,LoadFont(#Gadget_getbook_isbn,"Arial",12,0))
TextGadget(#Gadget_getbook_lpublisher,5,175,100,20,"Publisher",#PB_Text_Center)
SetGadgetFont(#Gadget_getbook_lpublisher,LoadFont(#Gadget_getbook_lpublisher,"Arial",12,0))
StringGadget(#Gadget_getbook_publisher,105,170,390,25,"")
SetGadgetFont(#Gadget_getbook_publisher,LoadFont(#Gadget_getbook_publisher,"Arial",12,0))
TextGadget(#Gadget_getbook_lprice,5,200,100,20,"Price",#PB_Text_Center)
SetGadgetFont(#Gadget_getbook_lprice,LoadFont(#Gadget_getbook_lprice,"Arial",12,0))
StringGadget(#Gadget_getbook_price,105,195,390,25,"")
SetGadgetFont(#Gadget_getbook_price,LoadFont(#Gadget_getbook_price,"Arial",12,0))
TextGadget(#Gadget_getbook_lpages,5,225,100,20,"Pages",#PB_Text_Center)
SetGadgetFont(#Gadget_getbook_lpages,LoadFont(#Gadget_getbook_lpages,"Arial",12,0))
StringGadget(#Gadget_getbook_pages,105,220,390,25,"")
SetGadgetFont(#Gadget_getbook_pages,LoadFont(#Gadget_getbook_pages,"Arial",12,0))
TextGadget(#Gadget_getbook_ltype,5,250,100,20,"Type",#PB_Text_Center)
SetGadgetFont(#Gadget_getbook_ltype,LoadFont(#Gadget_getbook_ltype,"Arial",12,0))
StringGadget(#Gadget_getbook_type,105,245,390,25,"")
SetGadgetFont(#Gadget_getbook_type,LoadFont(#Gadget_getbook_type,"Arial",12,0))
TextGadget(#Gadget_getbook_lpicture,5,275,100,20,"Picture",#PB_Text_Center)
SetGadgetFont(#Gadget_getbook_lpicture,LoadFont(#Gadget_getbook_lpicture,"Arial",12,0))
StringGadget(#Gadget_getbook_picture,105,270,390,25,"")
SetGadgetFont(#Gadget_getbook_picture,LoadFont(#Gadget_getbook_picture,"Arial",12,0))
TextGadget(#Gadget_getbook_lnote,5,300,100,20,"Note",#PB_Text_Center)
SetGadgetFont(#Gadget_getbook_lnote,LoadFont(#Gadget_getbook_lnote,"Arial",12,0))
StringGadget(#Gadget_getbook_note,105,295,390,25,"")
SetGadgetFont(#Gadget_getbook_note,LoadFont(#Gadget_getbook_note,"Arial",12,0))
CreateStatusBar(#StatusBar_getbook,WindowID(#Window_getbook))
AddStatusBarField(500)
HideWindow(#Window_getbook,0)
ProcedureReturn WindowID(#Window_getbook)
EndIf
EndProcedure
;============================================================================================================================
; my constants
;============================================================================================================================
Enumeration
#File
EndEnumeration
;============================================================================================================================
;
;============================================================================================================================
#INTERNET_FLAG_RELOAD = $80000000
#INTERNET_DEFAULT_HTTP_PORT = 80
#INTERNET_SERVICE_HTTP = 3
#HTTP_QUERY_FLAG_NUMBER = $20000000
#HTTP_QUERY_CONTENT_LENGTH = 5
#HTTP_QUERY_STATUS_CODE = 19
#HTTP_STATUS_OK = 200
#INTERNET_OPEN_TYPE_DIRECT = 1
;============================================================================================================================
;
;============================================================================================================================
Global CacheFilename.s = "C:\CacheFile.txt"
;============================================================================================================================
; my procedures
;============================================================================================================================
Procedure CheckError(value, sMessage.s, terminate)
If value = 0
If terminate
Debug("Error: " + sMessage)
End
EndIf
EndIf
EndProcedure
;============================================================================================================================
;
;============================================================================================================================
Procedure GetPage(URL.s)
If FindString(URL, "/", 8) = 0
Url.s = Url + "/"
EndIf
DeleteUrlCacheEntry_(URL)
If URLDownloadToFile_(#Null, URL, CacheFilename, #Null, #Null) = 0
Domain.s = RemoveString(Left(URL, FindString(URL, "/", 8) - 1), "http://")
dwordSize = 4
hInet = InternetOpen_("Mozilla/5.0 (Windows; U; Windows NT 5.1; es-ES; rv:1.7.8) Gecko/20050511 Firefox/1.0.4", #INTERNET_OPEN_TYPE_DIRECT, #Null, #Null, 0)
CheckError(hInet, "Internet connection not available.", #True)
hURL = InternetOpenUrl_(hInet, URL, #Null, 0, #INTERNET_FLAG_RELOAD, 0)
CheckError(hURL, "InternetOpenUrl_() failed", #True)
hInetCon = InternetConnect_(hInet, Domain, #INTERNET_DEFAULT_HTTP_PORT, #Null, #Null, #INTERNET_SERVICE_HTTP, 0, 0)
CheckError(hInetCon, "Unable to connect to " + Domain, #True)
hHttpOpenRequest = HttpOpenRequest_(hInetCon, "HEAD", RemoveString(URL, "http://" + Domain + "/"), "http/1.0", #Null, 0, #INTERNET_FLAG_RELOAD, 0)
CheckError(hHttpOpenRequest, "Http open request to " + Domain + " failed", #True)
CheckError(HttpSendRequest_(hHttpOpenRequest, #Null, 0, 0, 0), "Http send request to " + Domain + " failed.", #True)
CheckError(HttpQueryInfo_(hHttpOpenRequest, #HTTP_QUERY_FLAG_NUMBER | #HTTP_QUERY_STATUS_CODE, @sCode, @dwordSize, @lpdwIndex), "Http query failed.", #False)
CheckError(Bool(sCode = #HTTP_STATUS_OK), "Status code query failed.", #False)
CheckError(HttpQueryInfo_(hHttpOpenRequest, #HTTP_QUERY_FLAG_NUMBER | #HTTP_QUERY_CONTENT_LENGTH, @sCode, @dwordSize, @lpdwIndex), "CONTENT_LENGTH query failed.", #False)
If sCode
DataBufferLength = sCode
Else
DataBufferLength = 4096
EndIf
*DataBuffer = AllocateMemory(DataBufferLength)
CheckError(*DataBuffer, "Not enough memory.", #True)
CheckError(CreateFile(#File, CacheFileName), "Unable to create file.", #True)
Repeat
CheckError(InternetReadFile_(hURL, *DataBuffer, DataBufferLength, @Bytes), "Download failed.", #True)
If Bytes
WriteData(#File, *DataBuffer, Bytes)
EndIf
Until Bytes = 0
CloseFile(#File)
FreeMemory(*DataBuffer)
InternetCloseHandle_(hInetCon)
InternetCloseHandle_(hURL)
InternetCloseHandle_(hInet)
EndIf
EndProcedure
;============================================================================================================================
;
;============================================================================================================================
Procedure.s GetTitle(Author.s, Title.s)
GetPage("http://www.isfdb.org/cgi-bin/ea.cgi?" + LCase(Author.s))
foundtitle.s = ""
If ReadFile(#File, CacheFileName)
Repeat
a$ = ReadString(#File)
TitlePos.l = FindString(LCase(a$), LCase(Title.s), 1)
If TitlePos.l <> 0
TitleNumPos.l = FindString(a$, "?", 1)
foundtitle = Mid(a$, TitleNumPos + 1, TitlePos - TitleNumPos - 13)
EndIf
Until Eof(#File) Or foundtitle <> ""
CloseFile(#File)
Else
Debug("Couldn't read file in GetTitle")
EndIf
DeleteFile(CacheFileName)
ProcedureReturn foundtitle
EndProcedure
;============================================================================================================================
;
;============================================================================================================================
Procedure.s GetTitlePage(TitleNumber.s, Title.s)
GetPage("http://www.isfdb.org/cgi-bin/title.cgi?" + TitleNumber.s)
CompoundTitle.s = ""
If ReadFile(#File, CacheFileName)
Repeat
a$ = ReadString(#File)
CompoundIndex.l = FindString(a$, "http://www.isfdb.org/cgi-bin/pl.cgi", 1)
If CompoundIndex.l <> 0
CompoundIndexPos = FindString(a$, "?", 1)
TitlePos.l = FindString(LCase(a$), LCase(Title.s), 1)
CompoundTitle.s = Mid(a$, CompoundIndexPos + 1, TitlePos - CompoundIndexPos - 13)
EndIf
Until Eof(#File) Or CompoundTitle <> ""
CloseFile(#File)
Else
Debug("Couldn't read file in GetTitlePage")
EndIf
DeleteFile(CacheFileName)
ProcedureReturn CompoundTitle
EndProcedure
;============================================================================================================================
;
;============================================================================================================================
Procedure.s ReadField(origtext.s, search.s)
found.s = ""
findpos.l = FindString(LCase(origtext), LCase(search), 1)
If findpos <> 0
found = Trim(Right(origtext, Len(origtext) - (findpos + Len(search) + 3)))
EndIf
ProcedureReturn found
EndProcedure
;============================================================================================================================
;
;============================================================================================================================
Procedure.s ReadPicture(origtext.s, search.s)
found.s = ""
findpos.l = FindString(LCase(origtext), LCase(search), 1)
If findpos <> 0
searchlen = Len(search)
endpos = FindString(LCase(origtext), LCase(".JPG"), 1)
found = search.s + Mid(origtext, searchlen, endpos - searchlen + 4)
EndIf
ProcedureReturn found
EndProcedure
;============================================================================================================================
;
;============================================================================================================================
Procedure.l GetBookDetails(Bookreference.s, *details.bookdetails)
GetPage("http://www.isfdb.org/cgi-bin/pl.cgi?" + Bookreference.s)
found.l = #False
If ReadFile(#File, CacheFileName)
authortext.s = "http://www.isfdb.org/cgi-bin/ea.cgi?"
Repeat
origtext.s = ReadString(#File)
If found = #False
If FindString(LCase(origtext), LCase("metadata"), 1) <> 0
found = #True
EndIf
EndIf
If found = #True
If *details\title = "" : *details\title = ReadField(origtext, "Title:") : EndIf
findpos.l = FindString(origtext, authortext, 1) ; Author search
If findpos <> 0
findpos2.l = FindString(origtext, Chr(34) + ">", findpos + Len(authortext))
If findpos2 <> 0
author.s = Trim(Mid(origtext, findpos2 + 2, Len(origtext) - (findpos2 + 1) - 4))
If author <> ""
If *details\authors <> ""
*details\authors + ", "
EndIf
*details\authors + author
EndIf
EndIf
EndIf
If *details\year = "" : *details\year = ReadField(origtext, "Year:") : EndIf
If *details\isbn = "" : *details\isbn = ReadField(origtext, "ISBN:") : EndIf
If *details\publisher = "" : *details\publisher = ReadField(origtext, "Publisher:") : EndIf
If *details\price = "" : *details\price = ReadField(origtext, "Price:") : EndIf
If *details\pages = "" : *details\pages = ReadField(origtext, "Pages:") : EndIf
If *details\type = "" : *details\type = ReadField(origtext, "Type:") : EndIf
If *details\picture = ""
*details\picture = ReadPicture(origtext, "http://images.amazon.com/images/")
EndIf
EndIf
Until Eof(#File)
CloseFile(#File)
Else
Debug("Couldn't read file in GetBookDetails")
EndIf
ProcedureReturn found
EndProcedure
;============================================================================================================================
; main event handler
;============================================================================================================================
If Window_getbook()
AddKeyboardShortcut(#Window_getbook, #PB_Shortcut_Return, #Shortcut_getbook_enter)
quitgetbook = 0
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
If EventWindow() = #Window_getbook
quitgetbook = 1
EndIf
Case #PB_Event_Menu
Select EventMenu()
Case #Shortcut_getbook_enter : Gosub CheckEnter
EndSelect
EndSelect
Until quitgetbook
CloseWindow(#Window_getbook)
EndIf
End
;============================================================================================================================
; Check if the ENTER key was pressed in the SQL query box
;============================================================================================================================
CheckEnter:
FocusID = GetFocus_()
Select FocusID
Case GadgetID(#Gadget_getbook_stitle)
Title.s = GetGadgetText(#Gadget_getbook_stitle)
Author.s = GetGadgetText(#Gadget_getbook_sauthor)
If Title.s <> "" And Author.s <> ""
StatusBarText(#StatusBar_getbook, #StatusBar_getbook_messages, "Information: Fetching pages to parse..")
TitleNumber.s = GetTitle(Author.s, Title.s)
StatusBarText(#StatusBar_getbook, #StatusBar_getbook_messages, "Information: Processing and parsing..")
If TitleNumber.s <> ""
Bookreference.s = GetTitlePage(TitleNumber.s, Title.s)
If Bookreference.s <> ""
If GetBookDetails(Bookreference.s, @info.bookdetails) <> 0
SetGadgetText(#Gadget_getbook_author, info\authors)
SetGadgetText(#Gadget_getbook_title, info\title)
SetGadgetText(#Gadget_getbook_year, info\year)
SetGadgetText(#Gadget_getbook_isbn, info\isbn)
SetGadgetText(#Gadget_getbook_publisher, info\publisher)
SetGadgetText(#Gadget_getbook_price, info\price)
SetGadgetText(#Gadget_getbook_pages, info\pages)
SetGadgetText(#Gadget_getbook_type, info\type)
SetGadgetText(#Gadget_getbook_picture, info\picture)
;SetGadgetText(#Gadget_getbook_note, info\note)
StatusBarText(#StatusBar_getbook, #StatusBar_getbook_messages, "Information: Found all details for the specified title..")
SetGadgetText(#Gadget_getbook_stitle, "")
SetGadgetText(#Gadget_getbook_sauthor, "")
Else
StatusBarText(#StatusBar_getbook, #StatusBar_getbook_messages, "Error: Could not get the specified books' details")
EndIf
Else
StatusBarText(#StatusBar_getbook, #StatusBar_getbook_messages, "Error: No book reference for the specified title")
EndIf
Else
StatusBarText(#StatusBar_getbook, #StatusBar_getbook_messages, "Error: No ifsdb book reference number for the specified title")
EndIf
Else
StatusBarText(#StatusBar_getbook, #StatusBar_getbook_messages, "Error: Fill in both author and title fields to proceed to the search")
EndIf
EndSelect
Return