Jump to content

Topic Saver


Uten
 Share

Recommended Posts

Considering the speed of growth on the Jokes II topic. I have cleaned up my topic saver code a little and here it is. It ain't pretty and you can probably make it neater. But it worked on the jokes thread and it does on the jokes II thread.

NB: I have not done any extensive testing after I added the gui and cleaned it up a bit so there might be glitches in there.:P

http://www.autoitscript.com/forum/index.ph...=36941&st=0

Use the number after showtopic= of the topic you want to save.

If you specify 0 in last page you will get all the pages.

Happy laughing..:D

EDIT: Obviously there had to sneak in bug. Hope this quick fix fixed it.

EDIT2: Removed code in favor of the new version found in post 3

Edited by Uten
Link to comment
Share on other sites

  • 1 year later...

Updated version due to popular demand and small forum changes. Not to forget that the script did not give any warning if your not logged in as a user. You have to be to get the jokes (default) topic from chat.

#Region Compiler directives section
#AutoIt3Wrapper_compression = 4
#AutoIt3Wrapper_OutFile_Type=exe
#AutoIt3Wrapper_Outfile=TopicSaver.exe
#AutoIt3Wrapper_run_after=move %out% c:\
#endregion
; === Clean with CleanScript /striponly (or was it /onlystrip ?)
; 2008.08.14: Added dbg, dbgerr, labels and tips in GUI. Fixed StringRegExp to match all(?) images.
#include <GUIConstants.au3>
Global $gPages = 0
Global $gPageSteps = 40
Global $gTopicId = "34748" ;Jokes II "36941", 34748=Chat test topic
Global $gRootUrl = "http://www.autoitscript.com/forum/index.php?showtopic=" 
Global $gIndexUrl = "http://www.autoitscript.com/forum/index.php"
Local $gPath = @TempDir & "\" & $gTopicID

If MyGui($gPages, $gPageSteps, $gTopicId, $gPath) Then
    If CheckLoginStatus($gIndexUrl) Then
       GetThread($gRootUrl & $gTopicId, $gPath, $gPages, $gPageSteps)
       GetThreadImages($gPath)
       UpdateThreadImageReferences($gPath)
       Run("explorer.exe " & $gPath)
    Endif
EndIf
Exit
func dbg($msg, $erl=@ScriptLineNumber, $err=@error, $ext=@extended)
    If not @Compiled Then 
        ConsoleWrite("->[" & $erl & "][" & $err & "] : " & $msg & @crlf)
    EndIf
    If $err <> 0 Then SetError($err,$ext)
Endfunc
func dbgerr($msg="", $erl=@ScriptLineNumber, $err=@error, $ext=@extended)
    If $err > 0 then 
        dbg("DBGERR: " & $msg, $erl, $err, $ext)
    EndIf
    return SetError($err,$ext, $err)
EndFunc
Func CheckLoginStatus($indexurl)
    ; Basic login check by theguy0000
    Local $pathname = @TempDir & "\indexurl" & Random(1,9999) & ".html"
    INetGet ($IndexUrl,$pathname)
    If dbgerr("INetGet (" & $IndexUrl& "," & $pathname & ")") Then
        MsgBox (16, "ERROR", "Could not get source of index page" & @CRLF & "Probably due to network failure" )
        If FileExists($pathname) Then FileDelete($pathname)
        Return SetError(1,0,0)
    EndIf
    $indexSource = FileRead($pathname)
    If StringInStr($indexSource, "Welcome Guest") Then
        MsgBox (16, "ERROR", "You either tryed to download a nonexisting topic." & @CRLF & "Or you are not logged in to www.autoitscript.com."& @CRLF &"Please verify that you are loged in using Internet Explorer and that the topic exists, then try again.")
        If FileExists($pathname) Then FileDelete($pathname)
        Return SetError(2,0,0)
    EndIf
    If FileExists($pathname) Then FileDelete($pathname)
    Return 1
EndFunc
Func GetThread($rootUrl, $rootpath, $pages = 0, $pageSteps = 40, $pre = "ID")
   Local $i = 0, $ret = 1
   If StringRight($rootpath, 1) <> "\" Then $rootpath &= "\"
   If Not FileExists($rootpath) Then DirCreate($rootpath)
   dbg("InetGet(" & $rootUrl  & "[, " & $rootpath & $pre & $i & ".html)")
    InetGet( $rootUrl , $rootpath & $pre & $i & ".html")
    $rootUrl = $rootUrl & "&st="
    
   If $pages = 0 Then
      $data = FileRead($rootpath & $pre & $i & ".html")
      $arr = StringRegExp($data, "(\d+) Pages ",3)
      If IsArray($arr) Then
         $pages = $arr[0]
      Else
         dbg("NO MATCH in GetThread")
         dbg("RETURN DATA:")
         dbg($data)
         MsgBox(16, "ERROR", "Could not determine number of pages" & @CRLF & "Are you loged in through IExplore?")
         $ret =  0
      EndIf
   EndIf
   If $ret <> 0 Then
      For $i = $pageSteps to $pages*$pageSteps step $pageSteps
         If (Not FileExists($rootpath & $pre & $i & ".html")) AND ($i <> $pages*$pageSteps) Then
            InetGet( $rootUrl & $i,$rootpath & $pre & $i & ".html")
            sleep(Random(1,40,1)*250)
         EndIf
      Next
   EndIf
   Return $ret
EndFunc
Func GetThreadImages($path)
   Local $search, $imgs, $imgname, $data,  $imgPath
   If StringRight($path, 1) <> "\" Then $path &= "\"
   $imgPath = $path & "images\"
   If Not FileExists($imgPath) Then DirCreate($imgPath)
   $search = FileFindFirstFile($path & "*.html")
   If $search = -1 Then
       MsgBox(0, "Error", "No files/directories matched the search pattern")
       Return 0
   EndIf
   While 1
       $file = FileFindNextFile($search)
       If @error Then ExitLoop
       $data = FileRead($path & $file)
       ConsoleWrite($file & @LF)
       ;$imgs = StringRegExp($data, 'img src="(http[\w:/\.]+)"', 3)
       ;$imgs = StringRegExp($data, 'img src=["' & "'" & '](http[\w:/\.]+)["' & "'" & ']', 3)
       $imgs = StringRegExp($data, '=["' & "'" & '](http[\w:/\.]+)["' & "'" & ']', 3)
       If Not dbgerr("StringRegExp failed") Then
         For $i = 0 to UBound($imgs) -1
            $imgname = StringRight($imgs[$i],StringLen($imgs[$i]) -  StringInStr($imgs[$i],"/",0,-1))
            If not FileExists($imgPath & $imgname) Then
               dbg("GET: " & $imgname & " - " & $imgs[$i])
               InetGet($imgs[$i], $imgPath & $imgname)
            EndIf
         Next
       EndIF
   WEnd
   FileClose($search)
EndFunc

Func UpdateThreadImageReferences($path)
   Local $search, $imgs, $imgname, $data
   If StringRight($path, 1) <> "\" Then $path &= "\"
   $search = FileFindFirstFile($path & "*.html")
   If $search = -1 Then
       MsgBox(0, "Error", "No files/directories matched the search pattern")
       Return 0
   EndIf
   While 1
       $file = FileFindNextFile($search)
       If @error Then ExitLoop
       $data = FileRead($path & $file)
       dbg($file )
       ;$imgs = StringRegExp($data, 'img src="(http[\w:/\.]+)"', 3)
       ;$imgs = StringRegExp($data,'img src=["' & "'" & '](http[\w:/\.]+)["' & "'" & ']')
       $imgs = StringRegExp($data,'=["' & "'" & '](http[\w:/\.]+)["' & "'" & ']')
       If not dbgerr("StringRegExp failed") Then
         For $i = 0 to UBound($imgs) -1
            $imgname = StringRight($imgs[$i],StringLen($imgs[$i]) -  StringInStr($imgs[$i],"/",0,-1))
            If FileExists($path & "images\" & $imgname) Then
               dbg("MODIFY Reference: " & $imgname & " - " & $imgs[$i] )
               $data = StringReplace($data, $imgs[$i],"images\" & $imgname)
            EndIf
         Next
         FileDelete($path & $file)
         FileWrite($path & $file,$data)
       EndIF
   WEnd
   FileClose($search)
EndFunc
Func MyGui(ByRef $pages, ByRef $pagesSteps, ByRef $topicID, ByRef $path)
   Local $ret = 0
   Local $Form1 = GUICreate("Topic Saver", 457, 126, 193, 115)
   GUICtrlCreateLabel("NOTE: You need to be loged into the forum in IExplore!",10, 5, 437, 21)
   ; ~ Layout line 1
   GUICtrlCreateLabel("Topic ID",10, 36, 81, 21)
   Local $txtTopicID = GUICtrlCreateInput($topicID, 96, 35, 57, 21)
   GUICtrlSetTip ($txtTopicID, "Enter topic ID. This number of the URL: showtopic=38184.")
   GUICtrlCreateLabel("Posts in each page",158, 35, 107, 21)
   Local $txtPageSteps = GUICtrlCreateInput($pagesSteps, 270, 35, 27, 21)
   GUICtrlSetTip ($txtPageSteps, "Number of posts in a page: 40 is maximum")
   GUICtrlCreateLabel("Pages to download:",303, 35, 100, 21)
   Local $txtLastPage = GUICtrlCreateInput($pages, 408, 35, 25, 21)
   GUICtrlSetTip ($txtLastPage, "How many pages do you want downloaded? 0=all.")
   ; ~ Layout line 2
   GUICtrlCreateLabel("Path to save downloads", 10, 65, 120, 21)
   Local $txtPath = GUICtrlCreateInput($path, 135, 65, 298, 21)
   GUICtrlSetTip ($txtPath, "Root folder where you want the downloaded topic to be stored. Normally your temp folder")   
   ; ~Layout line 2
   Local $btnOK = GUICtrlCreateButton("OK", 368, 100, 65, 25, 0)
   Local $btnCancel = GUICtrlCreateButton("Cancel", 296, 100, 65, 25, 0)   
   GUISetState(@SW_SHOW)
   While 1
       $nMsg = GUIGetMsg()
       Switch $nMsg
         Case 0
            Sleep(100)
         Case $GUI_EVENT_CLOSE, $btnCancel
            ExitLoop
         Case $btnOK
            $ret = 1
            ExitLoop
       EndSwitch
   WEnd
   $pages = GUICtrlRead($txtLastPage)
   $pagesSteps = GUICtrlRead($txtPageSteps)
   $topicID = GuiCtrlRead($txtTopicID)
   dbg("$topicID:=" & $topicID)
   $path = GUICtrlRead($txtPath)
   GUIDelete($Form1)
   dbg("$pages:=" & $pages )
   dbg("$pagesSteps:=" & $pagesSteps )
   dbg("$topicID:=" & $topicID )
   dbg("$path:=" & $path )
   Return $ret
EndFunc

EDIT: Obviously some bug had to sneak in this time to *sigh*

Edited by Uten
Link to comment
Share on other sites

Lookin good.

Just to point out a couple other things.

It still errors (but still works) if there is only one page in the thread.

I fixed this using:

ElseIf StringInStr($data, 'nowrap="nowrap"><div>&nbsp;</div>') Then
    $pages = 1oÝ÷ Ù§íz»az'쵫^éír)à~È°
ëk-7éªë[(¢|¨º¹bî

right after $data = FileRead(...)

The cake is a lie.www.theguy0000.com is currentlyUP images.theguy0000.com is currentlyUP all other *.theguy0000.com sites are DOWN

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...