-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwitterTools.xojo_code
273 lines (221 loc) · 6.93 KB
/
TwitterTools.xojo_code
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#tag Module
Protected Module TwitterTools
#tag Method, Flags = &h0
Function twGetProfileImage(URL As String) As Picture
//Get the name of the image
Dim ImageNameParts(-1) As String = Split(URL, "/")
Dim ImageName As String = ImageNameParts(ImageNameParts.Ubound)
//Get the extension because we need to know if it is a jpeg or png image
Dim ExtensionParts(-1) As String = Split(ImageName, ".")
Dim Extension As String = ExtensionParts(ExtensionParts.Ubound)
//Check if the picture has allready been downloaded
If twProfileImages.Ubound > - 1 Then
for i As Integer = 0 to twProfileImages.Ubound
if twProfileImages(i).twProfileImagePath = URL Then
Dim ImageFile As FolderItem
ImageFile = SpecialFolder.Temporary.Child(ImageName)
If ImageFile <> Nil Then
Dim p As Picture
p = Picture.Open(ImageFile)
Return p
End If
Exit Function
End if
Next
End if
//Else download it and add it to the twProfileImages array and return the picture
Dim socket As New twSocket
socket.Yield = True
Dim data As String = socket.Get(URL, 5)
If socket.HTTPStatusCode = 200 Then
Dim p As Picture = Picture.FromData(data)
If p <> Nil Then
//Save as PNG
If Extension = "png" Then
If p.IsExportFormatSupported(Picture.FormatPNG) Then
//Save the file
Dim f As FolderItem
f = SpecialFolder.Temporary.Child(ImageName)
p.Save(f, Picture.SaveAsPNG)
End if
else
//Save as jpg
If p.IsExportFormatSupported(Picture.FormatJPEG) Then
//Save the file
Dim f As FolderItem
f = SpecialFolder.Temporary.Child(ImageName)
p.Save(f, Picture.SaveAsJPEG)
End if
//And add it to twProfileImages()
Dim newImage As New twProfileImage
newImage.twProfileImagePath = URL
twProfileImages.Append(newImage)
End if
Return p
Else
MsgBox("Could not get picture")
End If
Else
MsgBox("HTTP Status: " + Str(socket.HTTPStatusCode))
End If
End Function
#tag EndMethod
#tag Method, Flags = &h0
Function twGetToken(ck As String, cs As String) As String
//https://dev.twitter.com/oauth/application-only
Dim twTokenSocket As New twSocket
Dim s As String
s = ck + ":" + cs
s = EncodeBase64(s, 0)
twTokenSocket.SetRequestHeader("Content-Type:", "application/x-www-form-urlencoded;charset=UTF-8")
twTokenSocket.SetRequestHeader("Authorization:", "Basic " + s)
twTokenSocket.SetRequestContent("grant_type=client_credentials", "application/x-www-form-urlencoded")
Dim Response As String
Response = twTokenSocket.Post("https://api.twitter.com/oauth2/token", 10)
Try
Dim js As New JSONItem(Response)
Return js.Value("access_token")
Catch JSONException
If Response = "" Then
MsgBox("No Response")
Else
Msgbox(Response)
End if
End Try
End Function
#tag EndMethod
#tag Method, Flags = &h0
Sub twGetTweets(twQ As String, twCount As String)
Dim twTweetsSocket As New twSocket
Dim s As String
Dim t As Text
twTweetsSocket.SetRequestHeader("Authorization:", "Bearer " + twAccessToken)
s = twTweetsSocket.Get("https://api.twitter.com/1.1/search/tweets.json?q="+ EncodeURLComponent(twQ) + "&count=" + twCount, 30)
If s = "" Then
MsgBox("No Response!")
Return
End if
s = DefineEncoding(s, Encodings.UTF8)
'Dim c As New Clipboard
'c.text = s
t = s.ToText
Try
Dim js As Xojo.Core.Dictionary = Xojo.Data.ParseJSON(t)
Dim results() As Auto = js.Value("statuses")
Dim n As Xojo.Core.Dictionary
ReDim twTweets(-1)
Dim twTweetsNumber As Integer
twTweetsNumber = results.Ubound
Window1.NumberField.Text = Str(twTweetsNumber + 1)
For i As Integer = 0 to twTweetsNumber
Dim NewTweet As New twTweetObject
n = results(i)
If n.HasKey("created_at") Then
NewTweet.TwDate =n.Value("created_at")
End if
if n.HasKey("user") Then
Dim user As xojo.Core.Dictionary
user = n.Value("user")
if user.HasKey("name") Then
NewTweet.TwUserName = user.Value("name")
End if
if user.HasKey("profile_image_url_https") Then
NewTweet.twProfileImagePath = user.Value("profile_image_url_https")
NewTweet.twProfileImage = TwitterTools.twGetProfileImage(user.Value("profile_image_url_https"))
End if
End If
If n.HasKey("text") Then
NewTweet.twMessage = n.Value("text")
End if
If n.HasKey("id_str") Then
NewTweet.twId = n.Value("id_str")
End if
twTweets.Append(NewTweet)
twCounter = i
Next
Catch JSONException
Msgbox(t)
End Try
End Sub
#tag EndMethod
#tag Property, Flags = &h0
twAccessToken As String
#tag EndProperty
#tag Property, Flags = &h0
twConsumerKey As String
#tag EndProperty
#tag Property, Flags = &h0
twConsumerSecret As String
#tag EndProperty
#tag Property, Flags = &h0
twCounter As Integer = 0
#tag EndProperty
#tag Property, Flags = &h0
twProfileImages() As twProfileImage
#tag EndProperty
#tag Property, Flags = &h0
twTweets() As twTweetObject
#tag EndProperty
#tag ViewBehavior
#tag ViewProperty
Name="Index"
Visible=true
Group="ID"
InitialValue="-2147483648"
Type="Integer"
#tag EndViewProperty
#tag ViewProperty
Name="Left"
Visible=true
Group="Position"
InitialValue="0"
Type="Integer"
#tag EndViewProperty
#tag ViewProperty
Name="Name"
Visible=true
Group="ID"
Type="String"
#tag EndViewProperty
#tag ViewProperty
Name="Super"
Visible=true
Group="ID"
Type="String"
#tag EndViewProperty
#tag ViewProperty
Name="Top"
Visible=true
Group="Position"
InitialValue="0"
Type="Integer"
#tag EndViewProperty
#tag ViewProperty
Name="twAccessToken"
Group="Behavior"
Type="String"
EditorType="MultiLineEditor"
#tag EndViewProperty
#tag ViewProperty
Name="twConsumerKey"
Group="Behavior"
InitialValue="zvtlEof0yvz1koj1Ld7X7yXpA"
Type="String"
EditorType="MultiLineEditor"
#tag EndViewProperty
#tag ViewProperty
Name="twConsumerSecret"
Group="Behavior"
InitialValue="NpGzOfkvPB77d5GGk1oBGf4Y4gC8we7j5GUcLfHxRHf3YJSICm"
Type="String"
EditorType="MultiLineEditor"
#tag EndViewProperty
#tag ViewProperty
Name="twCounter"
Group="Behavior"
InitialValue="0"
Type="Integer"
#tag EndViewProperty
#tag EndViewBehavior
End Module
#tag EndModule