#include <IE.au3>
#include <Encoding.au3>;http://autoit-script.ru/index.php/topic,510.0.html
#include <WinAPIEx.au3>;Yashied, http://autoit-script.ru/index.php/topic,47.0.html
Opt('MustDeclareVars', 1)
Opt('TrayMenuMode', 1)
Global $sTitle = 'Сохранение документов www.krd.ru'
If WinExists('{[/@$@\]}' & $sTitle) Then Exit
AutoItWinSetTitle('{[/@$@\]}' & $sTitle)
HotKeySet('^{F8}', '_Get_Info_IE');Ctrl+F8
HotKeySet('+{Esc}', '_Exit');Shift+Esc
TraySetToolTip('Shift + Esc - выход' & @LF & 'Ctrl + F8 - сохранить документы')
_WinAPI_EmptyWorkingSet()
While 1
Sleep(50)
WEnd
Func _Get_Info_IE()
Local $a_ListIE, $h_Win, $o_IE, $o_Tags, $o_Links, $s_Text_Doc, $s_Control_Url = 'www.krd.ru', _
$a_Links, $i_Count_Links, $s_Name, $s_Text_Doc_Temp, $s_Root_Folder = @ScriptDir & '\Документы', _
$s_Ext = 'doc', $i_Error = 1
HotKeySet('^{F8}')
HotKeySet('+{Esc}')
$a_ListIE = WinList('[CLASS:IEFrame]')
If Not $a_ListIE[0][0] Then
TrayTip($sTitle, 'Отсутствуют окна Internet Explorer', 5, 3)
AdlibRegister('_Kill_TrayTip', 5000)
HotKeySet('^{F8}', '_Get_Info_IE')
HotKeySet('+{Esc}', '_Exit')
_WinAPI_EmptyWorkingSet()
Return
EndIf
For $i = 1 To $a_ListIE[0][0]
If StringInStr(WinGetText($a_ListIE[$i][1]), $s_Control_Url) Then
$h_Win = $a_ListIE[$i][1]
ExitLoop
EndIf
Next
If Not $h_Win Then
TrayTip($sTitle, 'Отсутствует окно Internet Explorer' & @LF & _
'с адресом, содержащим <' & $s_Control_Url & '>', 5, 3)
AdlibRegister('_Kill_TrayTip', 5000)
HotKeySet('^{F8}', '_Get_Info_IE')
HotKeySet('+{Esc}', '_Exit')
_WinAPI_EmptyWorkingSet()
Return
EndIf
$o_IE = _IEAttach($h_Win, 'HWND')
If @error Then
TrayTip($sTitle, 'Ошибка присоединения к окну Internet Explorer' & @LF & _
'Попробуйте еще раз', 5, 3)
AdlibRegister('_Kill_TrayTip', 5000)
HotKeySet('^{F8}', '_Get_Info_IE')
HotKeySet('+{Esc}', '_Exit')
_WinAPI_EmptyWorkingSet()
Return
EndIf
For $i = 1 To 1
$o_Tags = _IETagNameGetCollection($o_IE, 'div')
If @error Or Not @extended Then ExitLoop
For $o_Tag In $o_Tags
If $o_Tag.classname == 'doc' Then
$s_Text_Doc = $o_Tag.innertext
$o_Links = _IETagNameGetCollection($o_Tag, 'a')
If @error Or Not @extended Then ExitLoop
Dim $a_Links[@extended + 1][2]
For $o_Link In $o_Links
If $o_Link.href Then
$i_Count_Links += 1
$a_Links[$i_Count_Links][0] = _Encoding_HexToURL($o_Link.href)
$a_Links[$i_Count_Links][1] = StringRegExpReplace($a_Links[$i_Count_Links][0], '^.*/', '')
If @extended <> 1 Then _
$a_Links[$i_Count_Links][1] = 'Прил_' & @MDAY & '_' & @MON & '_' & @HOUR & '_' & @MIN & '_' & @SEC
EndIf
Next
If $i_Count_Links Then ReDim $a_Links[$i_Count_Links + 1][2]
ExitLoop
EndIf
Next
$i_Error = 0
Next
If $i_Error Or Not $s_Text_Doc Then
TrayTip($sTitle, 'Ошибка получения текста документа.' & @LF & _
'Попробуйте еще раз', 5, 3)
AdlibRegister('_Kill_TrayTip', 5000)
HotKeySet('^{F8}', '_Get_Info_IE')
HotKeySet('+{Esc}', '_Exit')
_WinAPI_EmptyWorkingSet()
Return
EndIf
$s_Text_Doc_Temp = StringRegExpReplace($s_Text_Doc, '[\r\n]{6}.*$', '')
If @extended = 1 Then $s_Text_Doc = $s_Text_Doc_Temp
$s_Name = StringRegExpReplace($s_Text_Doc, '(?s).*\d{1,2}\.\d{1,2}\.\d{2,4}.*(№\s?\d+)\r\n.*', '$1')
If @extended <> 1 Then $s_Name = @MDAY & '_' & @MON & '_' & @HOUR & '_' & @MIN & '_' & @SEC
_Save_Word($s_Text_Doc, $s_Name, $s_Root_Folder, $s_Ext)
If @error Then
TrayTip($sTitle, 'Ошибка сохранения файла.' & @LF & _
'Попробуйте еще раз', 5, 3)
AdlibRegister('_Kill_TrayTip', 5000)
HotKeySet('^{F8}', '_Get_Info_IE')
HotKeySet('+{Esc}', '_Exit')
_WinAPI_EmptyWorkingSet()
Return
EndIf
If $i_Count_Links Then
For $i = 1 To $i_Count_Links
_Inet_Get_Doc($a_Links[$i][0], $s_Root_Folder & '\' & $s_Name, $a_Links[$i][1])
If @error Then
TrayTip($sTitle, 'Ошибка закачки файла.' & @LF & _
'Попробуйте еще раз', 5, 3)
AdlibRegister('_Kill_TrayTip', 5000)
HotKeySet('^{F8}', '_Get_Info_IE')
HotKeySet('+{Esc}', '_Exit')
_WinAPI_EmptyWorkingSet()
Return
EndIf
Next
EndIf
TrayTip($sTitle, 'Все операции успешно выполнены.', 5, 1)
AdlibRegister('_Kill_TrayTip', 5000)
HotKeySet('^{F8}', '_Get_Info_IE')
HotKeySet('+{Esc}', '_Exit')
_WinAPI_EmptyWorkingSet()
Return
EndFunc ;==>_Get_Info_IE
Func _Save_Word($s_Text_To_Word, $s_Name_Dir_File, $s_Root_Folder_Save, $s_Ext_Word)
Local $o_WordApp, $o_Doc, $i_Error = 1, $o_Error, $f_Copy
If Not FileExists($s_Root_Folder_Save & '\' & $s_Name_Dir_File) _
Or Not StringInStr(FileGetAttrib($s_Root_Folder_Save & '\' & $s_Name_Dir_File), 'D') Then
If Not DirCreate($s_Root_Folder_Save & '\' & $s_Name_Dir_File) Then Return SetError(1)
EndIf
If FileExists($s_Root_Folder_Save & '\' & $s_Name_Dir_File & '\' & $s_Name_Dir_File & '.' & $s_Ext_Word) Then
If MsgBox(36, $sTitle, 'Файл <' & $s_Root_Folder_Save & '\' & $s_Name_Dir_File & '\' & _
$s_Name_Dir_File & '.' & $s_Ext_Word & '>' & @LF & 'уже есть. Перезаписать его?') = 7 Then
Return
Else
If FileCopy($s_Root_Folder_Save & '\' & $s_Name_Dir_File & '\' & $s_Name_Dir_File & '.' & $s_Ext_Word, _
$s_Root_Folder_Save & '\' & $s_Name_Dir_File & '\' & $s_Name_Dir_File & '.' & $s_Ext_Word & '.bak', 1) _
Then $f_Copy = True
FileDelete($s_Root_Folder_Save & '\' & $s_Name_Dir_File & '\' & $s_Name_Dir_File & '.' & $s_Ext_Word)
EndIf
EndIf
$o_Error = ObjEvent('AutoIt.Error', '_Save_Word')
$o_WordApp = ObjCreate('Word.Application')
If Not IsObj($o_WordApp) Then Return SetError(1)
$o_WordApp.Visible = False
For $i = 1 To 1
$o_Doc = $o_WordApp.Documents.Add
If $o_Error.number Then ExitLoop
$o_Doc.Range.Text = $s_Text_To_Word
If $o_Error.number Then ExitLoop
$o_Doc.SaveAs($s_Root_Folder_Save & '\' & $s_Name_Dir_File & '\' & $s_Name_Dir_File & '.' & $s_Ext_Word)
If $o_Error.number Then ExitLoop
$i_Error = 0
Next
If $i_Error Then
If $f_Copy Then
FileCopy($s_Root_Folder_Save & '\' & $s_Name_Dir_File & '\' & $s_Name_Dir_File & '.' & $s_Ext_Word & '.bak', _
$s_Root_Folder_Save & '\' & $s_Name_Dir_File & '\' & $s_Name_Dir_File & '.' & $s_Ext_Word, 1)
EndIf
Else
If $f_Copy Then
FileDelete($s_Root_Folder_Save & '\' & $s_Name_Dir_File & '\' & $s_Name_Dir_File & '.' & $s_Ext_Word & '.bak')
EndIf
EndIf
$o_WordApp.Quit
If $o_Error.number Then $i_Error = 1
Return SetError($i_Error)
EndFunc ;==>_Save_Word
Func _Inet_Get_Doc($s_Url, $s_Folder, $s_File_Name)
Local $h_Download, $i_Error, $f_Copy
If FileExists($s_Folder & '\' & $s_File_Name) Then
If MsgBox(36, $sTitle, 'Файл <' & $s_Folder & '\' & $s_File_Name & '>' & @LF & _
'уже закачен. Перезаписать его?') = 7 Then
Return
Else
If FileCopy($s_Folder & '\' & $s_File_Name, $s_Folder & '\' & $s_File_Name & '.bak', 1) _
Then $f_Copy = True
FileDelete($s_Folder & '\' & $s_File_Name)
EndIf
EndIf
$h_Download = InetGet($s_Url, $s_Folder & '\' & $s_File_Name, 1, 1)
Do
Sleep(10)
Until InetGetInfo($h_Download, 2)
If InetGetInfo($h_Download, 4) Then $i_Error = 1
InetClose($h_Download)
If $i_Error Then
If $f_Copy Then
FileCopy($s_Folder & '\' & $s_File_Name & '.bak', $s_Folder & '\' & $s_File_Name, 1)
EndIf
Else
If $f_Copy Then
FileDelete($s_Folder & '\' & $s_File_Name & '.bak')
EndIf
EndIf
Return SetError($i_Error)
EndFunc ;==>_Inet_Get_Doc
Func _Kill_TrayTip()
AdlibUnRegister('_Kill_TrayTip')
TrayTip('', '', 0)
EndFunc ;==>_Kill_TrayTip
Func _Exit()
Exit
EndFunc ;==>_Exit