Trở về
Tham gia nhóm m Autoit đ đưc hưng dn và gii đáp trc tiếp : http://fb.com/groups/autoitscript
Tin tức công nghệ  -  Thủ thuật lập trình

Thursday, November 28, 2013

Kiểm tra thư mới trong hộp thư Gmail với UDF _CheckMail (Autoit)


Toàn màn hìnhIn bài viết




Chú thích:  Các bạn thay "username" & "password" bằng username & password gmail của bạn rồi chạy thử để xem kết quả chương trình


Mã nguồn:

#include <string.au3>
#include <Array.au3>
#include <GuiListView.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Local $avArray[10]

 $gui=GUICreate("ListView Add Item", 800, 600 ,-1,-1, BitOR($WS_MINIMIZEBOX,$WS_MAXIMIZEBOX,$WS_SIZEBOX,$WS_THICKFRAME,$WS_SYSMENU,$WS_GROUP,$WS_CLIPSIBLINGS))
$hListView = GUICtrlCreateListView("Title|Name|Email|Summary", -1, -1, 800,600)
_GUICtrlListView_SetColumnWidth($hListView,0,200)
_GUICtrlListView_SetColumnWidth($hListView,1,20)
_GUICtrlListView_SetColumnWidth($hListView,2,50)
_GUICtrlListView_SetColumnWidth($hListView,3,430)
Global $eReturn = _CheckMail("username", "password")
Global $Emails = @extended
Switch @error
    Case 0
        Switch @extended
            Case True
                ;MsgBox(0,"","   "&$eReturn[0][0] )
                ;MsgBox(0,"","   "& $Emails & " " & $eReturn[0][1] )
    _GUICtrlListView_BeginUpdate($hListView)
                 _GUICtrlListView_DeleteAllItems($hListView)
                For $x = 1 To $Emails
     $item1 = GUICtrlCreateListViewItem(_Ansi2Uni($eReturn[$x][0])& "|" &_Ansi2Uni($eReturn[$x][1])& "|" &_Ansi2Uni($eReturn[$x][2])& "|" &_Ansi2Uni($eReturn[$x][3]), $hlistview)
;~                     MsgBox(0,"","!>Title   - " & $eReturn[$x][0] & @CR)
;~                     MsgBox(0,"","+>Name    - " & $eReturn[$x][1] & @CR)
;~                     MsgBox(0,"","->Email   - " & $eReturn[$x][2] & @CR)
;~                     MsgBox(0,"",">summary - " & $eReturn[$x][3] & @CR & @CR)
                Next
    _GUICtrlListView_EndUpdate($hListView)
            GUISetState()
             Case Else
                 MsgBox(64,"success!","Success! But you have no new emails :(")
         EndSwitch
    Case 1
        MsgBox(0,"Error!","Couldn't get your new emails for some reason...")
    Case 2,3
        MsgBox(0,"Error!","you need to enter an user name and password!")
    Case 4
        MsgBox(16,"Error!","Error Getting URL Source!")
    Case 5
        MsgBox(16,"Error!","No response?")
    Case 6
        MsgBox(16,"Error!","Unauthorized access, possibly a wrong username or password!")
EndSwitch
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $GUI_EVENT_MAXIMIZE
   $gsize = WinGetClientSize($gui)
   $size = $gsize[0]
_GUICtrlListView_SetColumnWidth($hListView,0,$size/4)
_GUICtrlListView_SetColumnWidth($hListView,1,$size/10)
_GUICtrlListView_SetColumnWidth($hListView,2,$size/7)
_GUICtrlListView_SetColumnWidth($hListView,3,$size-($size/4+$size/10+$size/7))
    EndSwitch
WEnd

; #FUNCTION# ====================================================================================================================
; Name ..........: _CheckMail
; Description ...: Checks a Google Email for new emails.
; Syntax ........: _CheckMail($UserName, $Pswd[, $UserAgentString = ""])
; Parameters ....: $UserName            - An unknown value.
;                 $Pswd             - An unknown value.
;                 $UserAgentString   - [optional] An unknown value. Default is "".
; Return values .: A 2d array with email information as follows~
;                   1|Title
;                   2|Name
;                   3|Email
;                   4|Summary
;                   5|Date
;                   6|Time
;
; Author ........: dantay9
; Modified ......: THAT1ANONYMOUSDUDE
; Remarks .......:
; Related .......:
; Link ..........: http://www.autoitscript.com/forum/topic/...-checker/page__view__findpost_
; Example .......: Yes
; ===============================================================================================================================
Func _CheckMail($UserName, $Pswd, $UserAgentString = "")
    If Not $UserName Then Return SetError(2,0,0)
    If Not $Pswd Then Return SetError(3,0,0)
    If $UserAgentString Then HttpSetUserAgent($UserAgentString)
    Local $source = InetRead("https://" & $UserName & ":" & $Pswd & "@gmail.google.com/gmail/feed/atom",1)
    If @error Then
        ConsoleWrite("!>Error Getting URL Source!" & @CR & "     404>@Error =" & @error & @CR & "    404>@Extended =" & @extended & @CR)
        Return SetError(4,0,0)
    EndIf
    If $source Then
        $source = BinaryToString($source)
    Else
        Return SetError(5,0,0)
    EndIf
    If StringLeft(StringStripWS($source, 8), 46) == "<HTML><HEAD><TITLE>Unauthorized</TITLE></HEAD>" Then  Return SetError(6, 0, 0)
    If Not Number(StringBetween($source, "<fullcount>", "</fullcount>")) Then Return SetError(0,0,0)
    Local $Email = _StringBetween($source, "<entry>", "</entry>")
    If @error Then Return SetError(1, 0, 0)
    Local $Time
    Local $Count = UBound($Email)
    Local $Datum[$Count + 1][6]
    $Datum[0][0] = StringBetween($source, "<title>", "</title>")
    $Datum[0][1] = StringBetween($source, "<tagline>", "</tagline>")
    For $i = 0 To $Count - 1
        $Datum[$i+1][0] = StringBetween($Email[$i], "<title>", "</title>")
        If Not $Datum[$i+1][0] Then $Datum[$i][0] = "(no subject)"
        $Datum[$i+1][1] = StringBetween($Email[$i], "<name>", "</name>")
        $Datum[$i+1][2] = StringBetween($Email[$i], "<email>", "</email>")
        $Datum[$i+1][3] = StringBetween($Email[$i], "<summary>", "</summary>")
        $Time = StringBetween($Email[$i], "<issued>", "</issued>")
        $Datum[$i+1][4] = DateFromTimeDate($Time)
        $Datum[$i+1][5] = TimeFromTimeDate($Time)
    Next
    Return SetError(0,$Count,$Datum)
EndFunc
Func StringBetween($Str, $S, $E)
    Local $B = _StringBetween($Str, $S, $E)
    If @error Then Return SetError(1,0,0)
    Return SetError(0,0,$B[0])
EndFunc   ;==>StringBetween
; #FUNCTION# ====================================================================================================================
; Name ..........: DateFromTimeDate
; Description ...: Returns email sent date.
; Syntax ........: DateFromTimeDate($String)
; Parameters ....: $String            - A gmail date string
; Return values .: None
; Author ........: ???
; Example .......: No
; ===============================================================================================================================
Func DateFromTimeDate($String)
    Local $RegEx = StringRegExp($String, "(?<Year>\d{2}|\d{4})(?:-)(?<Month>\d{1,2})(?:-)(?<Day>\d{1,2})", 1)
    If IsArray($RegEx) Then
        Return Int($RegEx[0]) & "/" & Int($RegEx[1]) & "/" & Int($RegEx[2])
    Else
        Return SetError(1, 0, 0)
    EndIf
EndFunc   ;==>DateFromTimeDate
; #FUNCTION# ====================================================================================================================
; Name ..........: TimeFromTimeDate
; Description ...: Returns the email sent time.
; Syntax ........: TimeFromTimeDate($String)
; Parameters ....: $String            - An unknown value.
; Return values .: None
; Author ........: ???
; Example .......: No
; ===============================================================================================================================
Func TimeFromTimeDate($String)
    Local $RegEx = StringRegExp($String, "(?<Hour>\d{1,2})(?::)(?<Minute>\d{1,2})(?::)(?<Second>\d{1,2})", 1)
    If IsArray($RegEx) Then
        Return (Int($RegEx[0]) - 4) & ":" & Int($RegEx[1]) & ":" & Int($RegEx[2]) ;don't know why I have to subtract 4
    Else
        Return SetError(1, 0, 0)
    EndIf
EndFunc   ;==>TimeFromTimeDate

Func _Uni2Ansi($Unicode)
    $Binary = StringToBinary($Unicode, 4)
    $Hex = StringReplace($Binary, '0x', '', 1)
    $BinaryLength = StringLen($Hex)
    Local $ANSI
    For $i = 1 To $BinaryLength Step 2
        $Char = StringMid($Hex, $i, 2)
        $ANSI &= BinaryToString('0x' & $Char)
    Next
    Return $ANSI
EndFunc   ;==>_Uni2Ansi
Func _Ansi2Uni($ANSI)
$Binary = StringToBinary($ANSI)
$Unicode = BinaryToString($Binary, 4)
Return $Unicode
EndFunc   ;==>_Ansi2Uni