skip to Main Content

So I’m working with the twitter API of a soccer team, and I want to write to a file only the tweets tweeted during a live game, where the tweets are minute by minute events of the game. For instance, “25′ This player got a yellow card” is an example of a tweet im looking for. All the live game tweets start with a minute, hence an integer, and I use the following regex to obtains such tweets, and write them to an text file

tweets = client.user_timeline('ManUtd', count: 3200)
tweets_file = File.open("tweets_file.txt", 'w')

tweets.each do |tweet| 
    if /[0-9]/.match(tweet.full_text[0])
        tweets_file.write(tweet.full_text + "n")
    end
end

The thing is, my text file looks exactly like I want it to look like after I add in those tweets, but, the File.size() is 0, and I can’t perform a code block on it either, that is, I cant do File.Open(tweets_file).eachand evaluate each line or word in the file, because, as the file size indicates, there’s nothing in it, but the actual file has a bunch of tweets written in it, so I’m pretty confused.

Can anyone shed some light on whats going on here?

EDIT: I forgot to mention, if I remove the regular expression, and simply add all the tweets form the timeline to my file, or if i replace the regex by [A-Z], then the File.Size works fine and i can use the each method on File.Open so it seems it has something to do with the integers at the beginning of a sentence

2

Answers


  1. Most likely you have a whitespace character or some kind of prefix string in the beginning of the tweet line.
    Use:

    tweet.full_text.strip[0]
    

    This will strip away whitespaces. Else you should find the prefix and strip IT out.

    Login or Signup to reply.
  2. I would recommend separating the types of operations (and I’m pretty sure this would solve your problem), something like this:

    #!/usr/bin/env ruby
    
    tweets = client.user_timeline('ManUtd', count: 3200)
    
    texts = tweets.map(&:full_text)
    
    live_game_texts = texts.select do |text|
      text.start_with?(/d/)
    end
    
    File.write('live_game_tweets.txt', live_game_texts.join("n"))
    

    Using File.write and not using File.open greatly simplifies the task since you don’t have to worry about closing the file.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search