How would you go to handle displaying tweets with working entities?
in
Contributed Library Questions
•
1 year ago
EDIT: code added as a reply in this thread.
When you got a tweet (aka status) you get those like this:
Text: a string containing all the tweet text, including entities text.
Hashtag entities: an array containing the first and one after last indexes of the hashtag in the above string.
also, you have the text (only hash text) accessible by hashtag object.
this is the JSON:
"hashtags":[{"indices":[32,36],"text":"lol"}] //
URL entities: Similar to hashtags, but also containing both, expanded and shorted URL text in the object.
"urls":[{"indices":[32,52], "url":"http:\/\/t.co\/IOwBrTZR", "display_url":"youtube.com\/watch?v=oHg5SJ\u2026", "expanded_url":"http:\/\/www.youtube.com\/watch?v=oHg5SJYRHA0"}]
Mentions entities: Are the users mentioned in tweet, they must be linked to users pages. They came like this;
"user_mentions"
:
[
{
"name"
:
"Twitter API"
,
"indices"
:
[
4
,
15
]
,
"screen_name"
:
"twitterapi"
,
"id"
:
6253282
,
"id_str"
:
"6253282"
}
]
The media stuff is similar, but i'm not concerned with them now, I'll leave it out for now.
So I need to display this, changing colors and linking accordingly. I started by hashtags, and i managed to split the text string in the pieces that are not hashtags, like:
"this is a #test to display tweets. #hashtag. ok."
became:
piece 1: this is a
piece 2: to display tweets.
piece 3: . ok.
Remember that those can came in any order and amount in a tweet.
Than i started thinking that this may not be the best approach. If i repeat this for the other entities i'll end with a lot of pieces to assemble back together... Looks messy. And I thought to use text(x, y, width, height) but to use that I need all the text in the same string... and won't be able to change colors, right? I'll have to handle line brakes manually?
Than i thought, i may convert the text to a char array and work only with the indexes of entities. I could use textWidth(text preceding the hashtag) to create a bounding box for linking, but any way I'll have to make several calls to text() to change colors... (edit) Maybe an array of words would be best...
Is any of those the way to go?
thanks
EDIT: I'm asking about handling the string, coloring, linking and spacing it to display. I got the JSON reading stuff and bounding box linking working already. actually twitter4j has those working for me ;-)
I can provide a text file with some JSONs from twitter and the code for reading them if some one wants.
I don't have the code here, but i'll post it latter. I leave the code, but the code doesn't leaves me...
1