Page 1 of 1

Programmatically accessing the FiveTech forums

Posted: Sat Mar 24, 2018 11:27 am
by Antonio Linares
Counting the amount of forums:

Code: Select all | Expand

#include "FiveWin.ch"

function Main()

   local cURL  := "http://forums.fivetechsupport.com/index.php"
   local cHTML := WebPageContents( cURL, .T. )
   
   MsgInfo( NumAt( "viewforum.php", cHTML ) )
   
return nil

Re: Programmatically accessing the FiveTech forums

Posted: Sat Mar 24, 2018 12:04 pm
by Antonio Linares
Surely there is a faster way to do it, but this is what we have for now :-)

Enhancements are welcome ;-)

forums.prg

Code: Select all | Expand

#include "FiveWin.ch"

function Main()

   local nForums := GetForumsCount()
   local aForums := {}, nForum := 1, cName
   
   while Len( aForums ) < nForums
      if ! Empty( cName := GetForumName( nForum++ ) )
         AAdd( aForums, { Len( aForums ) + 1, cName } )
      endif
   end
   
   XBrowser( aForums )

return nil

function GetForumName( nForum )

   local cURL := "http://forums.fivetechsupport.com/viewforum.php?f=" + AllTrim( Str( nForum ) )
   local cHTML := WebPageContents( cURL, .T. )
   
   cHTML = SubStr( cHTML, At( "<title>", cHTML ) + 7 )
   cHTML = SubStr( cHTML, 1, At( "</title>", cHTML ) - 1 )
   
   if "View" $ cHTML
      cHTML = SubStr( cHTML, At( "- ", cHTML ) + 2 )
   else
      cHTML = ""
   endif
   
return cHTML

function GetForumsCount()

   local cURL  := "http://forums.fivetechsupport.com/index.php"
   local cHTML := WebPageContents( cURL, .T. )
   
return NumAt( "viewforum.php", cHTML )
 

Re: Programmatically accessing the FiveTech forums

Posted: Sat Mar 24, 2018 4:31 pm
by cnavarro
A little more

Code: Select all | Expand



#include "FiveWin.ch"

//----------------------------------------------------------------------------//

Static oForum

Function Main()

   oForum   := TForumFwh():New()
   ? oForum:TotalPosts()
   ? oForum:TotalTopics()
   //oForum:ViewForums()
   //? oForum:ViewForums( 2 )
   ? oForum:PageNumber()
   oForum:ReadForum( 3 )
   oForum:ViewForums()

Return nil

//----------------------------------------------------------------------------//

//----------------------------------------------------------------------------//
//
//----------------------------------------------------------------------------//

CLASS TForumFwh

   DATA nForums             INIT 0
   DATA aForumsName         INIT {}
   DATA aPostsForums        INIT {}
   DATA aTopicsForums       INIT {}
   DATA cBaseUrl
   DATA cPageHtml
   DATA nTotalPosts         INIT 0
   DATA nTotalTopics        INIT 0

   METHOD New() CONSTRUCTOR
   METHOD ForumsCount()
   METHOD ForumNames()
   METHOD GetUserName( cText, dDate )
   METHOD PageNumber( cText )
   METHOD ReadForum( nForum )
   METHOD ReadForumName( nForum )
   METHOD ReadWeb()
   METHOD TotalPosts()
   METHOD TotalTopics()
   METHOD ViewForums()
   

ENDCLASS

//----------------------------------------------------------------------------//

METHOD New( cUrl ) CLASS TForumFwh

   local cOldUrl
   DEFAULT cUrl  := "http://forums.fivetechsupport.com"
   ::cBaseUrl    := cUrl
   cOldUrl       := ::cBaseUrl
   ::cBaseUrl    += "/index.php"
   ::ReadWeb()
   ::ForumsCount()

   //::aPostsForums  := Array( ::nForums )
   ::aTopicsForums := Array( ::nForums )

   ::cBaseUrl    := cOldUrl

Return Self

//----------------------------------------------------------------------------//

METHOD ReadWeb() CLASS TForumFwh

   ::cPageHtml   := WebPageContents( ::cBaseUrl, .T. )

Return ::cPageHtml

//----------------------------------------------------------------------------//

METHOD ForumsCount() CLASS TForumFwh

   ::nForums   := NumAt( "viewforum.php", ::cPageHtml )

Return ::nForums

//----------------------------------------------------------------------------//

METHOD ForumNames() CLASS TForumFwh

   local cName      := ""
   local nForum     := 1
   ::ForumsCount()
   ::aForumsName    := {}
   while Len( ::aForumsName ) < ::nForums
      if ! Empty( cName := ::ReadForumName( nForum++ ) )
         AAdd( ::aForumsName, { Len( ::aForumsName ) + 1, StrTran( cName, "amp;", "" ), 0, 0, 0 } )
      endif
   end

Return ::aForumsName

//----------------------------------------------------------------------------//
// Counter topics, pages and size of page of forum selected
//----------------------------------------------------------------------------//

METHOD ReadForum( nForum, nStart ) CLASS TForumFwh

   local lSw        := .T.
   local cHtml      := ""
   local cOldUrl    := ::cBaseUrl
   local cHtmlWeb   := ::cPageHtml
   local nTopics    := 0
   local nPages     := 0
   local nPagSize   := 0
   local nTopicsPag := 0
   local cTemp
   DEFAULT nForum   := 1
   DEFAULT nStart   := 0
   nForum           := Min( nForum, ::nForums )
   ::cBaseUrl       += "/viewforum.php?f=" + AllTrim( Str( nForum ) ) + ;
                      if( Empty( nStart ), "", "&start=" + AllTrim( Str( nStart ) ) )
   cHtml            := ::ReadWeb()
   //cHtml            := StrTran( cHtml, Chr( 10 ), "" )
   nTopics          := Val( AllTrim( TextBetween( cHtml, '<div class="pagination">', ;
                         'topics &bull; <a href="#"', 1 ) ) )
   nPages           := Val( AllTrim( TextBetween( cHtml, ;
                         'title="Click to jump to page?">Page <strong>1</strong> of <strong>', ;
                         '</strong></a> &bull; <span><strong>1</strong><span class="page-sep">', 1 ) ) )
   cTemp            := StrTran( AllTrim( TextBetween( cHtml, "var per_page = ", "var base_url", 1 ) ), ";", "" )
   nPagSize         := Val( StrTran( cTemp, "'", "" ) )

   ::cBaseUrl       := cOldUrl
   ::cPageHtml      := cHtmlWeb

   if Empty( ::aForumsName )
      MsgRun( "Reading names of Forums", "Please Wait", { || ::ForumNames() } )
   endif
   if !Empty( ::aForumsName )
      ::aForumsName[ nForum ][ 3 ]  := nTopics
      ::aForumsName[ nForum ][ 4 ]  := nPages
      ::aForumsName[ nForum ][ 5 ]  := nPagsize
      nTopicPag                     := NumAt( "topictitle", cHtml )
   endif
   
   //MemoWrit( "foro.txt", StrTran( cHtml, Chr( 10 ), CRLF ) )
   //
   //
   //

Return cHtml

//----------------------------------------------------------------------------//

METHOD ReadForumName( nForum ) CLASS TForumFwh

   local cOldUrl  := ::cBaseUrl
   local cHtmlWeb := ::cPageHtml
   local cName
   DEFAULT nForum := 1
   ::cBaseUrl     += "/viewforum.php?f=" + AllTrim( Str( nForum ) )
   ::ReadWeb()
   cName          := SubStr( ::cPageHtml, At( "<title>", ::cPageHtml ) + 7 )
   cName          := SubStr( cName, 1, At( "</title>", cName ) - 1 )
   cName          := if( "View" $ cName, SubStr( cName, At( "- ", cName ) + 2 ), "" )

   ::cBaseUrl     := cOldUrl
   ::cPageHtml    := cHtmlWeb

Return cName

//----------------------------------------------------------------------------//

METHOD TotalPosts() CLASS TForumFwh

   local cHtml
   cHtml       := StrTran( ::cPageHtml, "<strong>", "" )
   cHtml       := StrTran( cHtml, "</strong>", "" )
   cHtml       := StrTran( cHtml, "&bull;", "" )
   cHtml       := Substr( cHtml, At( "Total posts ", cHtml ), ;
                                 At( "Total topics", cHtml ) - At( "Total posts", cHtml ) )
   cHtml       := AllTrim( cHtml )
   ::nTotalPosts  := Val( Right( cHtml, Len( cHtml ) - At( "posts", cHtml ) - 5 ) )

Return ::nTotalPosts

//----------------------------------------------------------------------------//

METHOD TotalTopics() CLASS TForumFwh

   local cHtml
   cHtml       := StrTran( ::cPageHtml, "<strong>", "" )
   cHtml       := StrTran( cHtml, "</strong>", "" )
   cHtml       := StrTran( cHtml, "&bull;", "" )
   cHtml       := Substr( cHtml, At( "Total topics", cHtml ), ;
                                 At( "Total members", cHtml ) - At( "Total topics", cHtml ) )
   cHtml       := AllTrim( cHtml )
   ::nTotalTopics  := Val( Right( cHtml, Len( cHtml ) - At( "topics", cHtml ) - 6 ) )

Return ::nTotalTopics

//----------------------------------------------------------------------------//

METHOD ViewForums( nForum ) CLASS TForumFwh

   local cForum     := ""
   DEFAULT nForum   := 0

   if Empty( ::aForumsName )
      MsgRun( "Reading names of Forums", "Please Wait", { || ::ForumNames() } )
   endif
   if Empty( nForum )
      XBrowse( ::aForumsName )
   else
      nForum   := Min( nForum, ::nForums )
      cForum   := ::aForumsName[ nForum ][ 2 ]
   endif

Return cForum

//----------------------------------------------------------------------------//

METHOD PageNumber( cText ) CLASS TForumFwh

   local nAt
   local nPage    := 1
   DEFAULT cText  := ::cPageHtml

   if ( nAt := AT( "Page <strong>", cText ) ) > 0
      cText    := SubStr( cText, nAt + 14, 50 )
      nPage    := Val( AfterAtNum( "<strong>", cText, 1 ) )
   endif

return nPage

//----------------------------------------------------------------------------//

METHOD GetUserName( cText, dDate ) CLASS TForumFwh

   local c1      := "/memberlist.php?mode=viewprofile&amp;u="
   local c2      := '"username'
   local nAt     := Rat( c1, cText )
   local n2      := Rat( c2, cText )
   local cUser   := ""
   local cDate
   DEFAULT cText := ::cPageHtml

   nAt           := Max( nAt, n2 )
   if nAt > 0
      cText      := SubStr( cText, nAt, 200 )
      cUser      := TextBetween( cText, ">", "<", 1 )
      cDate      := AllTrim( TextBetween( cText, "&raquo;", "</p>" ) )
      cDate      := Upper( AfterAtNum( " ", cDate, 1 ) )
      dDate      := uCharToVal( cDate, 'D' )
   endif

return cUser

//----------------------------------------------------------------------------//
//
//----------------------------------------------------------------------------//

function TextBetween( cText, cStartTag, cCloseTag, nPos, cLeft, cRight )

   local cRet  := ""

   if !( cStartTag $ cText )
      cLeft    := cText
      cRight   := ""
      return ""
   endif

   cRight   := AfterAtNum( cStartTag, cText,  nPos )
   cRet     := BeforAtNum( cCloseTag, cRight, 1    )

   if PCount() > 4
      cLeft    := BeforAtNum( cStartTag, cText,  nPos )
      cRight   := AfterAtNum( cCloseTag, cRight, 1    )
   endif

return cRet

//----------------------------------------------------------------------------//

 


Also
viewtopic.php?f=3&t=33496&p=198156&hilit=download+all+samples#p197636

Re: Programmatically accessing the FiveTech forums

Posted: Sun Mar 25, 2018 12:54 pm
by cnavarro
More

Code: Select all | Expand


#include "FiveWin.ch"

//----------------------------------------------------------------------------//

Static oForum

Function Main()

   oForum   := TForumFwh():New()
   ? oForum:TotalPosts()
   ? oForum:TotalTopics()
   //oForum:ViewForums()
   //? oForum:ViewForums( 2 )
   ? oForum:PageNumber()
   //oForum:ReadPageForum( 3, 0 )
   oForum:ReadAllForums()
   oForum:ViewForums()

Return nil

//----------------------------------------------------------------------------//

#define ORDEN        1
#define NAME         2
#define TOPICS       3
#define PAGES        4
#define TOPICSPAGE   5

//----------------------------------------------------------------------------//
//
//----------------------------------------------------------------------------//

CLASS TForumFwh

   DATA nForums             INIT 0
   DATA aForumsName         INIT {}
   DATA aPostsForums        INIT {}
   DATA aTopicsForums       INIT {}
   DATA cBaseUrl
   DATA cPageHtml
   DATA nTotalPosts         INIT 0
   DATA nTotalTopics        INIT 0
   

   DATA nPagesForum         INIT 0
   DATA nTopicsForPage      INIT 0
   DATA nTopicsForum        INIT 0

   METHOD New() CONSTRUCTOR
   METHOD ForumsCount()
   METHOD ForumNames()
   METHOD GetUserName( cText, dDate )
   METHOD PageNumber( cText )
   METHOD ReadAllForums()
   METHOD ReadPageForum( nForum )
   METHOD ReadForumName( nForum )
   METHOD ReadWeb()
   METHOD TotalPosts()
   METHOD TotalTopics()
   METHOD ViewForums()
   

ENDCLASS

//----------------------------------------------------------------------------//

METHOD New( cUrl ) CLASS TForumFwh

   local cOldUrl
   DEFAULT cUrl  := "http://forums.fivetechsupport.com"
   ::cBaseUrl    := cUrl
   cOldUrl       := ::cBaseUrl
   ::cBaseUrl    += "/index.php"
   ::ReadWeb()
   ::ForumsCount()

   //::aPostsForums  := Array( ::nForums )
   ::aTopicsForums := Array( ::nForums )

   ::cBaseUrl    := cOldUrl

Return Self

//----------------------------------------------------------------------------//

METHOD ReadWeb() CLASS TForumFwh

   ::cPageHtml   := WebPageContents( ::cBaseUrl, .T. )

Return ::cPageHtml

//----------------------------------------------------------------------------//

METHOD ForumsCount() CLASS TForumFwh

   ::nForums   := NumAt( "viewforum.php", ::cPageHtml )

Return ::nForums

//----------------------------------------------------------------------------//

METHOD ForumNames() CLASS TForumFwh

   local cName      := ""
   local nForum     := 1
   ::ForumsCount()
   ::aForumsName    := {}
   while Len( ::aForumsName ) < ::nForums
      if ! Empty( cName := ::ReadForumName( nForum++ ) )
         AAdd( ::aForumsName, { Len( ::aForumsName ) + 1, StrTran( cName, "amp;", "" ), 0, 0, 0 } )
      endif
   end

Return ::aForumsName

//----------------------------------------------------------------------------//

METHOD ReadAllForums() CLASS TForumFwh

   local x
   For x = 1 to ::nForums
      MsgRun( "Reading Datas of Forums " + Str( x ), "Please Wait", { || ::ReadPageForum( x, 0 ) } )
   Next x
Return nil

//----------------------------------------------------------------------------//
// Counter topics, pages and size of page of forum selected
//----------------------------------------------------------------------------//

METHOD ReadPageForum( nForum, nStart ) CLASS TForumFwh

   local lSw        := .T.
   local cHtml      := ""
   local cOldUrl    := ::cBaseUrl
   local cHtmlWeb   := ::cPageHtml
   local nTopics    := 0
   local nPages     := 0
   local nPagSize   := 0
   local nTopicsPag := 0
   local cTemp
   local x
   DEFAULT nForum   := 1
   DEFAULT nStart   := 0
   nForum           := Min( nForum, ::nForums )
   ::cBaseUrl       += "/viewforum.php?f=" + AllTrim( Str( nForum ) ) + ;
                      if( Empty( nStart ), "", "&start=" + AllTrim( Str( nStart ) ) )
   cHtml            := ::ReadWeb()

   ::cBaseUrl       := cOldUrl
   ::cPageHtml      := cHtmlWeb
   if Empty( ::aForumsName )
      MsgRun( "Reading names of Forums", "Please Wait", { || ::ForumNames() } )
   endif

   if !Empty( ::aForumsName ) .and. nStart = 0
      nTopics          := Val( AllTrim( TextBetween( cHtml, '<div class="pagination">', ;
                            'topics &bull; <a href="#"', 1 ) ) )
      nPages           := Val( AllTrim( TextBetween( cHtml, ;
                         'title="Click to jump to page?">Page <strong>1</strong> of <strong>', ;
                         '</strong></a> &bull; <span><strong>1</strong><span class="page-sep">', 1 ) ) )
      cTemp            := StrTran( AllTrim( TextBetween( cHtml, "var per_page = ", "var base_url", 1 ) ), ";", "" )
      nPagSize         := Val( StrTran( cTemp, "'", "" ) )
      //::nPagesForum    := nPages
      //::nTopicsForPage := nPagSize
      //::nTopicsForum   := nTopics
      ::aForumsName[ nForum ][ TOPICS ]      := nTopics
      ::aForumsName[ nForum ][ PAGES ]       := nPages
      ::aForumsName[ nForum ][ TOPICSPAGE ]  := nPagsize
   endif

   nTopicsPag                     := NumAt( "topictitle", cHtml )

   
   //MemoWrit( "foro.txt", StrTran( cHtml, Chr( 10 ), CRLF ) )
   //
   //
   //

Return cHtml

//----------------------------------------------------------------------------//

METHOD ReadForumName( nForum ) CLASS TForumFwh

   local cOldUrl  := ::cBaseUrl
   local cHtmlWeb := ::cPageHtml
   local cName
   DEFAULT nForum := 1
   ::cBaseUrl     += "/viewforum.php?f=" + AllTrim( Str( nForum ) )
   ::ReadWeb()
   cName          := SubStr( ::cPageHtml, At( "<title>", ::cPageHtml ) + 7 )
   cName          := SubStr( cName, 1, At( "</title>", cName ) - 1 )
   cName          := if( "View" $ cName, SubStr( cName, At( "- ", cName ) + 2 ), "" )

   ::cBaseUrl     := cOldUrl
   ::cPageHtml    := cHtmlWeb

Return cName

//----------------------------------------------------------------------------//

METHOD TotalPosts() CLASS TForumFwh

   local cHtml
   cHtml       := StrTran( ::cPageHtml, "<strong>", "" )
   cHtml       := StrTran( cHtml, "</strong>", "" )
   cHtml       := StrTran( cHtml, "&bull;", "" )
   cHtml       := Substr( cHtml, At( "Total posts ", cHtml ), ;
                                 At( "Total topics", cHtml ) - At( "Total posts", cHtml ) )
   cHtml       := AllTrim( cHtml )
   ::nTotalPosts  := Val( Right( cHtml, Len( cHtml ) - At( "posts", cHtml ) - 5 ) )

Return ::nTotalPosts

//----------------------------------------------------------------------------//

METHOD TotalTopics() CLASS TForumFwh

   local cHtml
   cHtml       := StrTran( ::cPageHtml, "<strong>", "" )
   cHtml       := StrTran( cHtml, "</strong>", "" )
   cHtml       := StrTran( cHtml, "&bull;", "" )
   cHtml       := Substr( cHtml, At( "Total topics", cHtml ), ;
                                 At( "Total members", cHtml ) - At( "Total topics", cHtml ) )
   cHtml       := AllTrim( cHtml )
   ::nTotalTopics  := Val( Right( cHtml, Len( cHtml ) - At( "topics", cHtml ) - 6 ) )

Return ::nTotalTopics

//----------------------------------------------------------------------------//

METHOD ViewForums( nForum ) CLASS TForumFwh

   local cForum     := ""
   DEFAULT nForum   := 0

   if Empty( ::aForumsName )
      MsgRun( "Reading names of Forums", "Please Wait", { || ::ForumNames() } )
   endif
   if Empty( nForum )
      XBrowse( ::aForumsName )
   else
      nForum   := Min( nForum, ::nForums )
      cForum   := ::aForumsName[ nForum ][ 2 ]
   endif

Return cForum

//----------------------------------------------------------------------------//

METHOD PageNumber( cText ) CLASS TForumFwh

   local nAt
   local nPage    := 1
   DEFAULT cText  := ::cPageHtml

   if ( nAt := AT( "Page <strong>", cText ) ) > 0
      cText    := SubStr( cText, nAt + 14, 50 )
      nPage    := Val( AfterAtNum( "<strong>", cText, 1 ) )
   endif

return nPage

//----------------------------------------------------------------------------//

METHOD GetUserName( cText, dDate ) CLASS TForumFwh

   local c1      := "/memberlist.php?mode=viewprofile&amp;u="
   local c2      := '"username'
   local nAt     := Rat( c1, cText )
   local n2      := Rat( c2, cText )
   local cUser   := ""
   local cDate
   DEFAULT cText := ::cPageHtml

   nAt           := Max( nAt, n2 )
   if nAt > 0
      cText      := SubStr( cText, nAt, 200 )
      cUser      := TextBetween( cText, ">", "<", 1 )
      cDate      := AllTrim( TextBetween( cText, "&raquo;", "</p>" ) )
      cDate      := Upper( AfterAtNum( " ", cDate, 1 ) )
      dDate      := uCharToVal( cDate, 'D' )
   endif

return cUser

//----------------------------------------------------------------------------//
//
//----------------------------------------------------------------------------//

function TextBetween( cText, cStartTag, cCloseTag, nPos, cLeft, cRight )

   local cRet  := ""

   if !( cStartTag $ cText )
      cLeft    := cText
      cRight   := ""
      return ""
   endif

   cRight   := AfterAtNum( cStartTag, cText,  nPos )
   cRet     := BeforAtNum( cCloseTag, cRight, 1    )

   if PCount() > 4
      cLeft    := BeforAtNum( cStartTag, cText,  nPos )
      cRight   := AfterAtNum( cCloseTag, cRight, 1    )
   endif

return cRet

//----------------------------------------------------------------------------//