Skip to content

Instantly share code, notes, and snippets.

@timelyportfolio
Created January 8, 2015 19:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timelyportfolio/faa39b7a1741006c6e44 to your computer and use it in GitHub Desktop.
Save timelyportfolio/faa39b7a1741006c6e44 to your computer and use it in GitHub Desktop.
use twitter text javascript in R with V8
# devtools::install_github("jeroenooms/curl")
# devtools::install_github("jeroenooms/V8")
library(V8)
ct = new_context()
# get source and remove anonymous function wrapper
t = readLines(
"https://raw.githubusercontent.com/twitter/twitter-text/master/js/twitter-text.js"
)[-1]
t = t[-length(t)]
# save it local
# make sure to change file name to desired spot
cat( paste0(t,collapse="\n"), file = "c:/twitter.js")
# make sure to change file name to desired spot
ct$source( "c:/twitter.js" )
#see if it is there
ct$eval("twttr.txt")
# check for functions and keys within twttr.txt
ct$eval(
'Object.keys(twttr.txt)'
)
ct$eval(
'twttr.txt.extractMentions("Mentioning @twitter and @jack")'
)
ct$eval(
'twttr.txt.autoLink("link @user, please #request")'
)
ct$eval(
'twttr.txt.autoLink("link @user, and expand url... http://t.co/0JG5Mcq", {
urlEntities: [
{
"url": "http://t.co/0JG5Mcq",
"display_url": "blog.twitter.com/2011/05/twitte…",
"expanded_url": "http://blog.twitter.com/2011/05/twitter-for-mac-update.html",
"indices": [ 30, 48 ]
}
]
})'
)
ct$eval(
'var tweet = "This is a test tweet"
var remainingCharacters = 140 - twttr.txt.getTweetLength(tweet)
remainingCharacters'
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment