-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathGGTwitter.lua
More file actions
327 lines (241 loc) · 8.58 KB
/
GGTwitter.lua
File metadata and controls
327 lines (241 loc) · 8.58 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
-- Project: GGTwitter
--
-- Date: October 30, 2012
--
-- Version: 0.1
--
-- File name: GGTwitter.lua
--
-- Author: Graham Ranson of Glitch Games - www.glitchgames.co.uk
--
-- Update History:
--
-- 0.1 - Initial release
--
-- Comments:
--
-- GGTwitter makes it very easy to authorise your player with Twitter and post messages.
-- Authorisation data is stored so that the user only has to login the first time.
--
-- Requirements:
--
-- oAuth.lua from here - https://github.com/breinhart/Corona-SDK-Tweet-Media/blob/master/utils/oAuth.lua
-- multipartForm.lua from here - https://github.com/breinhart/Corona-SDK-Tweet-Media/blob/master/utils/multipartForm.lua
--
-- Copyright (C) 2012 Graham Ranson, Glitch Games Ltd.
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy of this
-- software and associated documentation files (the "Software"), to deal in the Software
-- without restriction, including without limitation the rights to use, copy, modify, merge,
-- publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
-- to whom the Software is furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in all copies or
-- substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
-- INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
-- PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
-- FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
-- OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
-- DEALINGS IN THE SOFTWARE.
--
----------------------------------------------------------------------------------------------------
local GGTwitter = {}
local GGTwitter_mt = { __index = GGTwitter }
local json = require( "json" )
local oAuth = require( "oAuth" )
--- Initiates a new GGTwitter object.
-- @param consumerKey The consumer key of your app.
-- @param consumerSecret The consumer secret of your app.
-- @param listener A listener function to be called on specific events such as login/logout.
-- @param url The twitter callback url?
-- @return The new object.
function GGTwitter:new( consumerKey, consumerSecret, listener, url )
local self = {}
setmetatable( self, GGTwitter_mt )
self.consumerKey = consumerKey
self.consumerSecret = consumerSecret
self.listener = listener
self.url = url or "http://www.google.co.uk"
self.accessToken = nil
self.accessTokenSecret = nil
self.userID = nil
self.screenName = nil
self:load()
return self
end
--- Converts a string into a table. Used internally.
-- @param str The string to convert.
-- @param delimeters A table of delimeters to split on.
-- @return The converted table.
function GGTwitter:responseToTable( str, delimeters )
local obj = {}
while str:find( delimeters[ 1 ] ) ~= nil do
if #delimeters > 1 then
local key_index = 1
local val_index = str:find( delimeters[ 1 ] )
local key = str:sub(key_index, val_index - 1 )
str = str:sub( ( val_index + delimeters[ 1 ]:len() ) )
local end_index
local value
if str:find( delimeters[ 2 ] ) == nil then
end_index = str:len()
value = str
else
end_index = str:find( delimeters[ 2 ] )
value = str:sub( 1, ( end_index - 1 ) )
str = str:sub( ( end_index + delimeters[ 2 ]:len() ), str:len() )
end
obj[ key ] = value
else
local val_index = str:find( delimeters[ 1 ] )
str = str:sub( ( val_index + delimeters[ 1 ]:len() ) )
local end_index
local value
if str:find( delimeters[ 1 ] ) == nil then
end_index = str:len()
value = str
else
end_index = str:find( delimeters[ 1 ] )
value = str:sub( 1, ( end_index - 1 ) )
str = str:sub( end_index, str:len() )
end
obj[ #obj + 1 ] = value
end
end
return obj
end
--- Deauthorises the user.
function GGTwitter:deauthorise()
self.accessToken = nil
self.accessTokenSecret = nil
self.userID = nil
self.screenName = nil
os.remove( system.pathForFile( "twitter.dat", system.DocumentsDirectory ) )
if self.listener then
self.listener{ phase = "deauthorised" }
end
end
--- Authorises the user.
function GGTwitter:authorise()
if self:isAuthorised() then
if self.listener then
self.listener{ phase = "authorised" }
end
return
end
local twitterRequest = ( oAuth.getRequestToken( self.consumerKey, self.url, "http://twitter.com/oauth/request_token", self.consumerSecret ) )
local twitterRequestToken = twitterRequest.token
local twitterRequestTokenSecret = twitterRequest.token_secret
local listener = function( event )
local remainOpen = true
local url = event.url
if url:find( "oauth_token" ) and url:find( self.url ) then
native.showAlert( "a", event.phase, { "OK" } )
url = url:sub( url:find( "?" ) + 1, url:len() )
local authoriseResponse = self:responseToTable( url, { "=" , "&" } )
remainOpen = false
local accessResponse = self:responseToTable( oAuth.getAccessToken( authoriseResponse.oauth_token, authoriseResponse.oauth_verifier, twitterRequestToken, self.consumerKey, self.consumerSecret, "https://api.twitter.com/oauth/access_token"), { "=", "&" } )
self.accessToken = accessResponse.oauth_token
self.accessTokenSecret = accessResponse.oauth_token_secret
self.userID = accessResponse.user_id
self.screenName = accessResponse.screen_name
if self.listener then
self.listener{ phase = "authorised" }
end
self:save()
elseif url:find( self.url ) then
if self.listener then
self.listener{ phase = "failed" }
end
remainOpen = false
end
return remainOpen
end
if not twitterRequestToken then
if self.listener then
self.listener{ phase = "failed" }
end
return
end
local fullX = display.viewableContentWidth + -1 * ( display.screenOriginX * 2 )
local fullY = display.viewableContentHeight + -1 * ( display.screenOriginY * 2 )
native.showWebPopup( display.screenOriginX, display.screenOriginY, fullX, fullY, "http://api.twitter.com/oauth/authorize?oauth_token=" .. twitterRequestToken, { urlRequest = listener } )
end
--- Checks if the user is authorised.
-- @return True if the user is currently authorised, false otherwise.
function GGTwitter:isAuthorised()
if self.accessToken then
return true
end
return false
end
--- Loads the authorisation data from disk. Called internally.
function GGTwitter:load()
local path = system.pathForFile( "twitter.dat", system.DocumentsDirectory )
local file = io.open( path, "r" )
if file then
local data = json.decode( file:read( "*a" ) )
io.close( file )
if data then
self.accessToken = data.accessToken
self.accessTokenSecret = data.accessTokenSecret
end
end
end
--- Saves the authorisation data to disk. Called internally.
function GGTwitter:save()
local data = { accessToken = self.accessToken, accessTokenSecret = self.accessTokenSecret }
local path = system.pathForFile( "twitter.dat", system.DocumentsDirectory )
local file = io.open( path, "w" )
if file then
file:write( json.encode( data ) )
io.close( file )
file = nil
end
end
--- Post a message to the users Twitter feed.
-- @param message The message to post.
-- @param image The filename of an image in system.DocumentsDirectory to post. Optional.
function GGTwitter:post( message, image )
local params = {}
params[ 1 ] =
{
key = 'status',
value = message
}
if image then
oAuth.makeRequestWithMedia( "http://upload.twitter.com/1/statuses/update_with_media.json", params, image, self.consumerKey, self.accessToken, self.consumerSecret, self.accessTokenSecret, "POST" )
print("A")
else
oAuth.makeRequest( "http://api.twitter.com/1/statuses/update.json", params, self.consumerKey, self.accessToken, self.consumerSecret, self.accessTokenSecret, "POST" )
end
if self.listener then
self.listener{ phase = "posted" }
end
end
--- Follow a user.
-- @param name The name of the user to follow.
function GGTwitter:follow( name )
local params = {}
params[ 1 ] =
{
key = 'screen_name',
value = name
}
params[ 2 ] =
{
key = 'follow',
value = "true"
}
oAuth.makeRequest( "http://api.twitter.com/1/friendships/create.json", params, self.consumerKey, self.accessToken, self.consumerSecret, self.accessTokenSecret, "POST" )
if self.listener then
self.listener{ phase = "followed" }
end
end
--- Destroys the Twitter object.
function GGTwitter:destroy()
self:deauthorise()
end
return GGTwitter