For my winter games entry, I made the ability to get and display tweets on each player’s screen. The code below allows you to get the most recent tweets using the hashtag of your choice, and puts them in a table. The scripted cap is 3 tweets at a time, but you can unlock that for a maximum twitter api allowance of 5 tweets.
local hashtagsearch = "rbxgames2014"--put your tag in there, sans hashtag
httpservice = game:GetService("HttpService")
local tweetjson = httpservice:GetAsync(
"http://programmerssyndicate.guildhome.co.uk/ROBLOX/searchTweets.php?screenname=%23"..hashtagsearch,true)
pattern = [["id_str":"%d*","text":.-,"screen_name":.-",]]
authpattern = [[.-"]]
textpattern = [["text":".-",]]
local tweets = {}
local nu = 1
for w in string.gmatch(tweetjson,pattern) do
local discard = false
local author = "@"..string.sub(string.reverse(string.match(string.sub(string.reverse(w),3),authpattern)),2)
local tweettext = string.sub(string.match(w,textpattern),9)
tweettext = string.sub(tweettext,1,string.len(tweettext)-2)
print(author..": " ..tweettext)
if nu >3 then discard = true end
if not discard then
tweets[nu] ={["auth"]=author,["text"]=tweettext}
nu = nu+1
end
end
you get something like this:
the tweets are stored in a table formatted like this:
If anything, I should also give my thanks to As8D who helped me set up the code. I have had no idea how to use PHP until now, so he is the one who should be mostly credited for his work.
Did you know, he’s also working on a twitter interface for ROBLOX? So far you can do pretty much what you can with my code, just with a fancy interface to it. He’s currently working on making it so you can POST tweets using your own twitter account, but obviously he has to go through the usual junk; ‘we will not use your password for anything bad, blah blah blah’
EDIT: Also, you could have made your searcher a little bit more efficient; something like:
local api = assert(LoadLibrary("RbxUtility"))
local http = game:GetService("HttpService")
local jsondata = http:GetAsync("http://programmerssyndicate.guildhome.co.uk/ROBLOX/searchTweets.php?screenname=%23RbxGames2014") --let's just use this as an example
local data = api.DecodeJSON(jsondata)
local tweets = { }
for _, tweet in pairs(data.statuses) do
table.insert(tweets, {auth=tweet.user.screen_name, text=tweet.text})
end
local data = api.DecodeJSON(jsondata)
local tweets = { }
for _, tweet in pairs(data.statuses) do
table.insert(tweets, {auth=tweet.user.screen_name, text=tweet.text})
end
[/code] [/quote]
Hmm, well asking for twitter passwords on roblox is really bad idea, and I don’t think roblox will like that stuff. Even if you don’t use the passwords for bad, exploiters could find way how to steal them. Of course, if you keep the passwords on client and localscript, it should be safe, but you’d need to get somekind of roblox admin review and aprooval, so users could actually trust it. Tho it’s still really messy stuff.
local data = api.DecodeJSON(jsondata)
local tweets = { }
for _, tweet in pairs(data.statuses) do
table.insert(tweets, {auth=tweet.user.screen_name, text=tweet.text})
end
[/code] [/quote]
Is the JSONEncode and Decode under httpservice somehow different than the one in RbxUtility? The only reason I switched to string manipulation was because it kept erroring when using httpservice:JSONDecode(tweetjson)
You can see more about the issue in the other thread about this.
Something I just thought about, which may make putting twitter service in a public Winter Games place a bad idea. If I’ve got the hashtag, can’t I tweet something anti-child friendly so that every player playing your game reads it?
local data = api.DecodeJSON(jsondata)
local tweets = { }
for _, tweet in pairs(data.statuses) do
table.insert(tweets, {auth=tweet.user.screen_name, text=tweet.text})
end
[/code] [/quote]
Is the JSONEncode and Decode under httpservice somehow different than the one in RbxUtility? The only reason I switched to string manipulation was because it kept erroring when using httpservice:JSONDecode(tweetjson)
You can see more about the issue in the other thread about this.[/quote]