PageRenderTime 25ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/facepunch/forums.lua

http://facepunch-lua-sdk.googlecode.com/
Lua | 261 lines | 241 code | 2 blank | 18 comment | 0 complexity | 5578d0885189b608f793f6eb4ca19a20 MD5 | raw file
  1. -------------------------------------------------------------------------------
  2. -- Forums module
  3. -- Facepunch Lua API
  4. -- Authors: Andrew McWatters
  5. -- Gran PC
  6. -- Gregor Steiner
  7. -------------------------------------------------------------------------------
  8. local error = error
  9. local facepunch = require( "facepunch" )
  10. local http = require( "facepunch.http" )
  11. local post = require( "facepunch.post" )
  12. local pairs = pairs
  13. local session = require( "facepunch.session" )
  14. local thread = require( "facepunch.thread" )
  15. local string = string
  16. local table = table
  17. local tonumber = tonumber
  18. local setmetatable = setmetatable
  19. module( "facepunch.forums" )
  20. -------------------------------------------------------------------------------
  21. -- indexPageForumCategoryPattern
  22. -- Purpose: Pattern for creating and filling the forum objects from the
  23. -- homepage.
  24. -------------------------------------------------------------------------------
  25. indexPageForumCategoryPattern = "" ..
  26. -- Category ID
  27. "forums\" id=\"cat(%d-)\"" ..
  28. -- Name
  29. ".-<h2><a href=\"forums/.-\">(.-)</a></h2>" ..
  30. -- Forums
  31. ".-</thead>(.-)</table>"
  32. -------------------------------------------------------------------------------
  33. -- indexPageForumPattern
  34. -- Purpose: Pattern for creating and filling the forum objects from the
  35. -- "forums" result of indexPageForumCategoryPattern.
  36. -------------------------------------------------------------------------------
  37. indexPageForumPattern = "" ..
  38. -- Forum ID
  39. "<tr id=\"forum(%d-)\"" ..
  40. -- Thread and Post Count
  41. ".-title=\"(.-) Threads, (.-) Posts\"" ..
  42. -- Forum Name
  43. ".->(.-)</a></h2>" ..
  44. -- Viewers
  45. ".-%((.-) Viewing" ..
  46. -- Description
  47. ".-<p class=\"forumdescription\">(.-)</p>" ..
  48. -- Last Poster ID, Name
  49. ".-<a href=\"/members/(%d-)\".-alt=\"(.-)\">" ..
  50. -- Last Post Thread
  51. ".-Go to first unread post in thread '(.-)'"..
  52. -- Last Post Date, URL
  53. ".-\"lastpostdate\">(.-) <a href=\"(.-)\""
  54. -------------------------------------------------------------------------------
  55. -- forumPageThreadPattern
  56. -- Purpose: Pattern for filling thread objects from a forum page.
  57. -------------------------------------------------------------------------------
  58. forumPageThreadPattern = "" ..
  59. -- Properties, thread ID
  60. "<tr class=\"threadbit (.-)\" id=\"thread_(%d-)\"" ..
  61. -- Thread icon, name
  62. ".-<img src=\"(.-)\" alt=\"(.-)\" border=\"0\" />" ..
  63. -- Thread title
  64. ".-thread_title_%d-\">(.-)</a>" ..
  65. -- Has images / new posts?
  66. "(.-)</h3>" ..
  67. -- Author UID, username, readers?
  68. ".-<div class=\"author\">.-<a href=\"members/(.-)%-.-\">(.-)</a>(.-)\n" ..
  69. -- Last Post Date
  70. ".-<dd>(.-)</dd>" ..
  71. -- Last Poster UID, username, reply link
  72. ".-by <a href=\"members/(.-)%-.-\">(.-)</a> <a href=\"(.-)\"" ..
  73. -- Reply count
  74. ".-\">(.-)</a>" ..
  75. -- View count
  76. ".-<span>(.-)</span>"
  77. -------------------------------------------------------------------------------
  78. -- forum
  79. -- Purpose: Class index
  80. -------------------------------------------------------------------------------
  81. local forum = {}
  82. -------------------------------------------------------------------------------
  83. -- __metatable
  84. -- Purpose: Class metatable
  85. -------------------------------------------------------------------------------
  86. __metatable = {
  87. __index = forum,
  88. __type = "forum"
  89. }
  90. -------------------------------------------------------------------------------
  91. -- forum.new()
  92. -- Purpose: Creates a new forum object
  93. -- Output: forum
  94. -------------------------------------------------------------------------------
  95. function new()
  96. local t = {
  97. forumID = nil,
  98. threadCount = nil,
  99. postCount = nil,
  100. forumName = nil,
  101. viewers = nil,
  102. forumDescription = nil,
  103. lastPosterID = nil,
  104. lastPosterName = nil,
  105. lastThreadName = nil,
  106. lastPostDate = nil,
  107. lastPostURL = nil
  108. }
  109. setmetatable( t, __metatable )
  110. return t
  111. end
  112. -------------------------------------------------------------------------------
  113. -- forum()
  114. -- Purpose: Shortcut to forum.new()
  115. -- Output: forum
  116. -------------------------------------------------------------------------------
  117. local metatable = {
  118. __call = function( _, ... )
  119. return new( ... )
  120. end
  121. }
  122. setmetatable( _M, metatable )
  123. -------------------------------------------------------------------------------
  124. -- forum:__tostring()
  125. -- Purpose: Returns a string representation of a forum
  126. -- Output: string
  127. -------------------------------------------------------------------------------
  128. function __metatable:__tostring()
  129. if not self.forumName then return "invalid forum" end
  130. return "forum: " .. self.forumName
  131. end
  132. -------------------------------------------------------------------------------
  133. -- forums.getListing()
  134. -- Purpose: Returns 0 if the page is retrieved successfully, then the forum
  135. -- listing, otherwise it returns 1 and nil.
  136. -- Output: error code, thread page
  137. -------------------------------------------------------------------------------
  138. function getListing()
  139. local r, c = http.get( facepunch.baseURL .. "/forum.php", session.getActiveSession() )
  140. if ( c == 200 ) then
  141. local ret = {}
  142. for catID,
  143. catName,
  144. forums in string.gmatch( r, indexPageForumCategoryPattern ) do
  145. for forumID,
  146. threadCount,
  147. postCount,
  148. forumName,
  149. viewers,
  150. forumDescription,
  151. lastPosterID,
  152. lastPosterName,
  153. lastThreadName,
  154. lastPostDate,
  155. lastPostURL in string.gmatch( forums, indexPageForumPattern ) do
  156. local retForum = new()
  157. retForum.forumID = tonumber( forumID )
  158. -- need to specify base, otherwise it takes the second return value for gsub
  159. retForum.threadCount = tonumber( string.gsub( threadCount, ",", "" ), 10 )
  160. retForum.postCount = tonumber( string.gsub( postCount, ",", "" ), 10 )
  161. retForum.forumName = forumName
  162. retForum.viewers = tonumber( string.gsub( viewers, ",", "" ), 10 )
  163. retForum.forumDescription = forumDescription
  164. retForum.lastPosterID = tonumber( lastPosterID )
  165. retForum.lastPosterName = lastPosterName
  166. retForum.lastThreadName = lastThreadName
  167. retForum.lastPostDate = lastPostDate
  168. retForum.lastPostURL = facepunch.baseURL .. lastPostURL
  169. retForum.category = catName
  170. table.insert( ret, retForum )
  171. end
  172. end
  173. return 0, ret
  174. else
  175. return 1, nil
  176. end
  177. end
  178. -------------------------------------------------------------------------------
  179. -- forums.getPage()
  180. -- Purpose: Returns 0 if the page is retrieved successfully, then the forum
  181. -- page by ID and page number, if provided, otherwise it returns 1 and
  182. -- nil
  183. -- Input: forumID - ID of the forum to get
  184. -- pageNumber - number of the page to get
  185. -- Output: error code, forum page
  186. -------------------------------------------------------------------------------
  187. function getPage( forumID, pageNumber )
  188. pageNumber = pageNumber or ""
  189. if ( pageNumber ~= "" ) then
  190. pageNumber = "/" .. pageNumber
  191. end
  192. local r, c = http.get( facepunch.baseURL .. "/forums/" .. forumID .. pageNumber, session.getActiveSession() )
  193. if ( c == 200 ) then
  194. return 0, r
  195. else
  196. return 1, nil
  197. end
  198. end
  199. -------------------------------------------------------------------------------
  200. -- forums.getThreadsInPage()
  201. -- Purpose: Returns a list of threads in a forum
  202. -- Input: forumPage - string of the requested page
  203. -- Output: table
  204. -------------------------------------------------------------------------------
  205. function getThreadsInPage( forumPage )
  206. local t = {}
  207. for properties,
  208. threadID,
  209. threadIconURL,
  210. threadIcon,
  211. threadTitle,
  212. hasImages,
  213. authorID,
  214. authorName,
  215. threadReaders,
  216. lastPostDate,
  217. lastPosterID,
  218. lastPosterName,
  219. lastPostURL,
  220. replies,
  221. views
  222. in string.gmatch( forumPage, forumPageThreadPattern ) do
  223. local thread = thread.new()
  224. thread.threadID = tonumber( threadID )
  225. thread.threadIconURL = threadIconURL
  226. thread.threadIcon = threadIcon
  227. thread.threadTitle = threadTitle
  228. thread.hasImages = string.find( hasImages, "images" ) and true or false
  229. thread.authorID = tonumber( authorID )
  230. thread.authorName = authorName
  231. thread.threadReaders = string.match( threadReaders, "(%d-) read" )
  232. thread.lastPostDate = lastPostDate
  233. thread.lastPosterID = tonumber( lastPosterID )
  234. thread.lastPosterName = lastPosterName
  235. thread.lastPostURL = lastPostURL
  236. thread.replyCount = tonumber( string.gsub( replies, ",", "" ), 10 )
  237. thread.viewCount = tonumber( string.gsub( views, ",", "" ), 10 )
  238. thread.newPosts = tonumber( string.match( hasImages, "(%d-) new posts" ) )
  239. table.insert(t, thread)
  240. end
  241. return t
  242. end