-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_tweets
executable file
·74 lines (61 loc) · 1.82 KB
/
get_tweets
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env ruby
require 'rubygems'
require 'json'
require 'net/http'
require 'uri'
require 'time'
module TwitterAPI
def api_call(method,page,username,password)
url = URI::parse('http://twitter.com')
req = Net::HTTP::Get.new("#{method}?screen_name=#{username}&page=#{page}&count=200")
req.basic_auth(username, password) if password
res = Net::HTTP.start(url.host, url.port) {|http| http.request(req) }
JSON.parse(res.body)
end
def all_tweets(method,username,password=nil)
page = 1
loop do
begin
tweets = api_call(method,page,username,password)
break if tweets.length == 0
tweets.each { |tweet| yield tweet }
page += 1
sleep 5
rescue
$stderr.puts "Error getting page #{page}: will retry in 30secs"
sleep 30
end
end
end
end
class TwitterBackup
include TwitterAPI
def tweets(username,password=nil)
all_tweets('/statuses/user_timeline.json',username,password) do |tweet|
print(tweet)
end
end
def retweets(username,password=nil)
all_tweets('/statuses/retweeted_by_me.json',username,password) do |tweet|
print_rt(tweet)
end
end
protected
def clean_up(tweet)
tweet.gsub("\n"," ").gsub("\r"," ").gsub("\t"," ")
end
def print_rt(tweet)
text = "RT @"+tweet['retweeted_status']['user']['screen_name']+": "+clean_up(tweet['retweeted_status']['text'])
put_tweet(tweet,text)
end
def print(tweet)
put_tweet(tweet,clean_up(tweet['text']))
end
def put_tweet(tweet,text)
id, date = tweet['id'], Time.parse(tweet['created_at']).strftime("%Y-%m-%dT%H:%M:%S%z")
puts [id,date,text].join("\t")
end
end
username,password = ARGV[0],ARGV[1]
TwitterBackup.new.tweets(username,password)
TwitterBackup.new.retweets(username,password)