Twitter Client Script to Retrieves All the Unique Http Links in the Last Recent Tweets

Code to fetch link from tweets matched by given hash_tag

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
require 'rubygems'
require 'twitter'


hash_tag = ARGV.first

if hash_tag.nil?
  puts "Please provide tag you want to fetch. Example 'rails', '#rails'"
  exit
end
puts 'fetching......'
begin
  tweets = Twitter::Search.new.hashtag(hash_tag)
rescue Exception => e
  # Time out or any other error can occur here
  puts "Errors in fetching tweets #{e}"
end
links = []
tweets.each do |tweet|
  links << tweet.text.match(/\bhttps?:\/\/\S+\b/)
end

links = links.uniq
puts links