-
Notifications
You must be signed in to change notification settings - Fork 0
/
BingLoggerHTR.Main.VBS
184 lines (147 loc) · 10.8 KB
/
BingLoggerHTR.Main.VBS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
Option Explicit
Const AppTitle = "AJPapps - Bing logger HTR ver."
Const AppCopyright = "Линда Кайе 2013-2024. Посвящается Ариэль"
Const ProxyValue = 0 ' psatPreconfigByIE
'Const ProxyValue = 1 ' psatDirect
Dim Path
Dim PictureURL
Dim NewPictureURLs
Dim Description
Dim PictureData
Dim UserAgent
Dim TMP
'====================================================================
Public Sub Main()
' Заранее получаем юзерагент, чтобы он не менялся при запросах...
UserAgent = GetUserAgent()
Path = GetPathFromArgs()
PictureURL = GetPictureURL(Description)
If PictureURL = "" Then Exit Sub
' Заполняем массив URL'ами с известными размерами, а последним
' элементом ставим тот, что выдал нам сам Bing. Так мы боремся
' с умной автоматикой, детектящей размеры экрана по IP.
NewPictureURLs = GetNewPictureURLs(PictureURL)
' Пытаемся получить файл с каждого URL пока не найдём чего
' существующего...
For TMP = LBound(NewPictureURLs) To UBound(NewPictureURLs)
PictureURL = NewPictureURLs(TMP)
PictureData = GetJPGFile(PictureURL, CBool(TMP = UBound(NewPictureURLs)))
If Not IsEmptyData(PictureData) Then Exit For
Next
If IsEmptyData(PictureData) Then Exit Sub
SaveJPGFile Path, PictureURL, PictureData, Description
End Sub
'====================================================================
Private Function GetPictureURL(ByRef Description)
Dim HTR 'As ajpHTTPRequest
Dim Proxy 'As ProxySettings
Dim TXT
GetPictureURL = ""
On Error Resume Next
Set HTR = CreateObject("CmnFuncs0301.ajpHTTPRequest")
Set Proxy = CreateObject("CmnFuncs0301.ProxySettings")
If Err.Number <> 0 Then
MsgBox "Не удалось получить главную страницу Bing." & _
vbCrLf & vbCrLf & _
"Не удаётся создать объект ajpHTTPRequest.", _
vbCritical, AppTitle
Exit Function
End If
On Error Goto 0
Proxy.HTTP_AccessType = ProxyValue
Set HTR.ProxySettings = Proxy
HTR.Execute "www.bing.com", , , _
"/", , , , , , _
UserAgent, , , True, True, True
If HTR.ErrorCode <> 0 Then
MsgBox "Не удалось получить главную страницу Bing." & _
vbCrLf & vbCrLf & _
"Ошибка отправки HTTP запроса.", _
vbCritical, AppTitle
Exit Function
End If
If HTR.ResponseCode <> 200 Then
MsgBox "Не удалось получить главную страницу Bing." & _
vbCrLf & vbCrLf & _
"Сервер вернул: " & HTR.ResponseCode & " " & _
HTR.ResponseText & ".", vbCritical, AppTitle
Exit Function
End If
' Сервер возвращает нам UTF-8, поэтому перегоняем его в UTF-16...
TXT = Utf8ToChar(HTR.ReceivedData)
GetPictureURL = ParseHTMLAndGetPictureURL(TXT)
Description = ParseHTMLAndGetDescription(TXT)
End Function
'====================================================================
Private Function GetJPGFile(ByVal URL, ByVal IsLastOne)
Dim HTR 'As ajpHTTPRequest
Dim Proxy 'As ProxySettings
Dim Protocol
Dim Host
Dim Port
Dim SearchObject
GetJPGFile = ""
On Error Resume Next
Set HTR = CreateObject("CmnFuncs0301.ajpHTTPRequest")
Set Proxy = CreateObject("CmnFuncs0301.ProxySettings")
If Err.Number <> 0 Then
MsgBox "Не удалось получить картинку с Bing." & _
vbCrLf & vbCrLf & _
"Не удаётся создать объект ajpHTTPRequest.", _
vbCritical, AppTitle
Exit Function
End If
On Error Goto 0
If Not CrackURL(URL, Protocol, Host, Port, SearchObject) Then
MsgBox "Не удалось получить картинку с Bing." & _
vbCrLf & vbCrLf & _
"Ошибка парсинга URL." & _
vbCrLf & vbCrLf & URL, _
vbCritical, AppTitle
Exit Function
End If
Proxy.HTTP_AccessType = ProxyValue
Set HTR.ProxySettings = Proxy
HTR.Execute Host, Port, , _
SearchObject, , "http://www.bing.com/", , , , _
UserAgent, , , True, True, True
If HTR.ErrorCode <> 0 Then
MsgBox "Не удалось получить картинку с Bing." & _
vbCrLf & vbCrLf & _
"Ошибка отправки HTTP запроса." & _
vbCrLf & vbCrLf & URL, _
vbCritical, AppTitle
Exit Function
End If
If HTR.ResponseCode <> 200 Then
' Если файл не существует, и это не последний URL, то
' не сообщать ничего, а просто выйти!
If Not (Not IsLastOne And HTR.ResponseCode = 404) Then _
MsgBox "Не удалось получить картинку с Bing." & _
vbCrLf & vbCrLf & _
"Сервер вернул: " & HTR.ResponseCode & " " & _
HTR.ResponseText & ".", vbCritical, AppTitle
Exit Function
End If
GetJPGFile = HTR.ReceivedData
End Function
' Этот велосипед необходим, поскольку функция библиотеки ждёт Long
' и работать с вариантом не будет =_=
Private Function CrackURL(ByVal URL, ByRef Protocol, ByRef Host, _
ByRef Port, ByRef SearchObject)
Dim RE
Dim MC
On Error Resume Next
Set RE = New RegExp
RE.Global = True
RE.IgnoreCase = True
RE.MultiLine = True
RE.Pattern = "(.*?)://(.*?)(:(.*?))?(/.*)"
Set MC = RE.Execute(URL)
Protocol = MC(0).SubMatches(0)
Host = MC(0).SubMatches(1)
Port = MC(0).SubMatches(3)
If Port = "" Then Port = 80 Else Port = CLng(Port)
SearchObject = MC(0).SubMatches(4)
CrackURL = CBool(Host <> "")
End Function