Connect to the Twitter Streaming API using R

后端 未结 2 1689
终归单人心
终归单人心 2021-01-06 22:30

I just started playing around with the Twitter Streaming API and using the command line, redirect the raw JSON reponses to a file using the command below:

cu         


        
2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-06 23:04

    I was able to figure out the basics, hopefully this helps.

    #==============================================================================
    # Streaming twitter using RCURL
    #==============================================================================
    library(RCurl)
    library(rjson)
    
    # set the directory
    setwd("C:\\")
    
    #### redirects output to a file
    WRITE_TO_FILE <- function(x) {
    
         if (nchar(x) >0 ) {
              write.table(x, file="Twitter Stream Capture.txt", append=T, row.names=F, col.names=F)
         }
    
    }
    
    ### windows users will need to get this certificate to authenticate
    download.file(url="http://curl.haxx.se/ca/cacert.pem", destfile="cacert.pem")
    
    ### write the raw JSON data from the Twitter Firehouse to a text file
    getURL("https://stream.twitter.com/1/statuses/sample.json", 
           userpwd=USER:PASSWORD,
           cainfo = "cacert.pem", 
           write=WRITE_TO_FILE)
    

提交回复
热议问题