Advertisement
wuuyi123

DemoWeather

Nov 10th, 2017
474
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 3.48 KB | None | 0 0
  1. #include <String.au3>
  2. #include <Array.au3>
  3. #include <WindowsConstants.au3>
  4. #include <GDIPlus.au3>
  5.  
  6. ;### LƯU Ý, NHẬP TÊN THÀNH PHỐ CÓ DẤU DỄ GÂY LỖI.
  7.  
  8. _GDIPlus_Startup()
  9. Global $API = '8cc0cf33ff7c6ac14e22653a3f1eca9b' ;API Public
  10. Global $Weather = GetWeatherHTML("Hồ Chí Minh", $API)
  11.  
  12. $hGUI = GUICreate("Simple Weather", 222, 176, 192, 124)
  13. GUISetBkColor(0xcee1ff)
  14. GUISetFont(10, 400, 0, "Segoe UI", $hGUI, 5)
  15. $Input = GUICtrlCreateInput($Weather[0], 16, 8, 113, 21, 0x0001)
  16. $Search = GUICtrlCreateButton("Tìm", 136, 7, 59, 23)
  17. $Icon = GUICtrlCreatePic("", 128, 102, 50, 50)
  18. $Tinh = GUICtrlCreateLabel($Weather[0], 10, 48, 200, 32, BitOR(0x01, 0x200))
  19. GUICtrlSetFont(-1, 15, 400, 0, "Segoe UI")
  20. $Nhietdo = GUICtrlCreateLabel("Nhiệt độ " & @CR & $Weather[1], 125, 80, 115, 40)
  21. $May = GUICtrlCreateLabel("Mây " & $Weather[3], 24, 80, 98, 17)
  22. $Doam = GUICtrlCreateLabel("Độ ẩm " & $Weather[4], 24, 96, 99, 17)
  23. $Gio = GUICtrlCreateLabel("Gió " & $Weather[5], 24, 112, 95, 17)
  24. $Apsuat = GUICtrlCreateLabel("Áp suất " & $Weather[6], 24, 128, 96, 17)
  25. SetWeatherIcon($Icon, $Weather[2])
  26. GUISetState(@SW_SHOW)
  27.  
  28. While 1
  29.     $nMsg = GUIGetMsg()
  30.     Switch $nMsg
  31.         Case -3
  32.             Exit
  33.         Case $Search
  34.             GetWeather(GUICtrlRead($Input))
  35.     EndSwitch
  36. WEnd
  37.  
  38. Func GetWeather($sLocation)
  39.     Local $aRet = GetWeatherHTML($sLocation, $API)
  40.     If Not IsArray($aRet) Then Return
  41.     GUICtrlSetData($Tinh, $aRet[0])
  42.     GUICtrlSetData($Nhietdo, "Nhiệt độ" & @CR & $aRet[1])
  43.     SetWeatherIcon($Icon, $aRet[2])
  44.     GUICtrlSetData($May, "Mây " & $aRet[3])
  45.     GUICtrlSetData($Doam, "Độ ẩm " & $aRet[4])
  46.     GUICtrlSetData($Gio, "Gió " & $aRet[5])
  47.     GUICtrlSetData($Apsuat, "Áp suất" & $aRet[6])
  48. EndFunc
  49.  
  50. Func SetWeatherIcon($hPic, $sCode, $iLeft = -1, $iTop = -1, $iWidth = -1, $iHeight = -1)
  51.     Local Const $BinaryImg = InetRead("http://openweathermap.org/img/w/" & $sCode &".png", 1)
  52.     If @error Then Return
  53.     Local Const $aBmp = _GDIPlus_BitmapCreateFromMemory($BinaryImg)
  54.     Local Const $hBmp = _GDIPlus_BitmapCreateDIBFromBitmap($aBmp)
  55.     If Not $iLeft = -1 Then GUICtrlSetPos($hPic, $iLeft)
  56.     If Not $iTop = -1 Then GUICtrlSetPos($hPic, $iLeft, $iTop)
  57.     If $iWidth = -1 Then
  58.         $iWidth = _GDIPlus_ImageGetWidth($aBmp)
  59.     Else
  60.         GUICtrlSetPos($hPic, $iLeft, $iTop, $iWidth)
  61.     EndIf
  62.     If $iHeight = -1 Then
  63.         $iHeight = _GDIPlus_ImageGetHeight($aBmp)
  64.     Else
  65.         GUICtrlSetPos($hPic, $iLeft, $iTop, $iWidth, $iHeight)
  66.     EndIf
  67.     _WinAPI_DeleteObject(GUICtrlSendMsg($hPic, 0x0172, 0, 0))
  68.     _WinAPI_DeleteObject(GUICtrlSendMsg($hPic, 0x0172, 0, $hBmp))
  69.     _GDIPlus_BitmapDispose($aBmp)
  70.     _WinAPI_DeleteObject($hBmp)
  71. EndFunc
  72.  
  73. Func GetWeatherHTML($sLocation, $sAPIKey)
  74.     Local $aRet[7]
  75.     $aRet[0] = StringUpper($sLocation)
  76.     $sLocation = StringReplace($sLocation, " ", "%20")
  77.     Local $sReq = 'http://api.openweathermap.org/data/2.5/' _
  78.                 & 'weather?q=' &        $sLocation _
  79.                 & '&mode=html&appid=' & $sAPIKey
  80.     Local $sRet = InetRead($sReq, 1)
  81.     If @error Then Return False
  82.     $sRet = BinaryToString($sRet, 4)
  83.     If StringInStr($sRet, ":404") Then Return False
  84.     $aRet[1] = _StringBetween($sRet, '"Current Temperature">', '</div>')[0]
  85.     $aRet[2] = _StringBetween($sRet, 'org/img/w/', '.png&quot;')[0]
  86.     If @HOUR > 18 Then $aRet[2] = StringReplace($aRet[2], "d", "n")
  87.     $aRet[3] = _StringBetween($sRet, '>Clouds:', '</div>')[0]
  88.     $aRet[4] = _StringBetween($sRet, '>Humidity:', '</div>')[0]
  89.     $aRet[5] = _StringBetween($sRet, '>Wind:', '</div>')[0]
  90.     $aRet[6] = _StringBetween($sRet, '>Pressure:', '</div>')[0]
  91.     Return $aRet
  92. EndFunc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement