I'm working on building a small script that searches for the 5 most recent pictures tweeted by a service, isolates the URL and puts that URL into an array.
def grabTweets(linkArray) #brings in empty array
tweets = Twitter.search("[pic] "+" url.com/r/", :rpp => 2, :result_type => "recent").map do |status|
tweets = "#{status.text}" #class = string
url_regexp = /http:\/\/\w/ #isolates link
url = tweets.split.grep(url_regexp).to_s #chops off link, turns link to string from an array
#add link to url array
#print linkArray #prints []
linkArray.push(url)
print linkArray
end
end
x = []
timelineTweets = grabTweets(x)
The function is returning things like this: ["[\"http://t.co/6789\"]"]["[\"http://t.co/12345\"]"]
I'm trying to get it to return ["http://t.co/6789", "http://t.co/1245"] but it's not managing that.
Any help here would be appreciated. I'm not sure what I'm doing wrong.
To strip a url out a string and push into urls array, you can do:
grep
returns an array:So your odd output is coming from the
to_s
call the follows yourgrep
. You're probably looking for this:or if you only want the first URL:
You could also skip the
split.grep
and usescan
:The easiest way to grab URLs in Ruby is to use the
URI::extract
method. It's a pre-existing wheel that works:Which returns:
Once you have the array you can filter for what you want, or you can give it a list of schemes to extract.